+ Shopping cart database handling

+ documentation
This commit is contained in:
Johannes Theiner 2019-05-21 13:04:31 +02:00
parent b2ed3d6a03
commit 1742ee4c14
7 changed files with 155 additions and 11 deletions

View File

@ -9,7 +9,7 @@
<groupId>de.hsel</groupId>
<artifactId>itech</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.5.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>

View File

@ -14,6 +14,8 @@ import java.util.List;
import java.util.Map;
/**
* handles all database stuff for addresses of users.
*
* @author Johannes Theiner
* @version 0.1
* @since 0.5
@ -26,6 +28,12 @@ public class AddressDB {
this.database = database;
}
/**
* get all addresses of specified user.
*
* @param user user to get addresses from
* @return list of users addresses
*/
@NotNull
public List<Address> getAll(@NotNull User user) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetsByValue(database.tableUserAddress, "user", String.valueOf(user.getId()));
@ -47,6 +55,12 @@ public class AddressDB {
return list;
}
/**
* get specific address.
*
* @param id address id
* @return specific address
*/
@Nullable
public Address get(long id) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableAddress, id);
@ -64,6 +78,12 @@ public class AddressDB {
return null;
}
/**
* get user from address id.
*
* @param id address id
* @return User
*/
@Nullable
private User getUserFromId(long id) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetByValue(database.tableUserAddress, "address", id);
@ -79,11 +99,16 @@ public class AddressDB {
return null;
}
/**
* insert address into database.
*
* @param address address to insert
* @return insert count
*/
public int insert(@NotNull Address address) {
Connection connection = database.getConnection();
int updateCount = 0;
int insertCount = 0;
try {
assert connection != null;
@ -94,21 +119,27 @@ public class AddressDB {
statement.setInt(4, address.getZipCode());
statement.setString(5, address.getCity());
updateCount += statement.executeUpdate();
insertCount += statement.executeUpdate();
PreparedStatement relationStatement = connection.prepareStatement("INSERT INTO user_address (user, address) VALUES (?, ?)");
relationStatement.setLong(1, address.getUser().getId());
relationStatement.setLong(2, address.getId());
updateCount += statement.executeUpdate();
insertCount += statement.executeUpdate();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
return updateCount;
return insertCount;
}
/**
* delete address from database.
*
* @param address address to delete
* @return deletion count
*/
public int delete(@NotNull Address address) {
return database.delete(address.getId(), database.tableUserAddress);
}

View File

@ -36,6 +36,7 @@ public class Database {
final String tableUser = "user";
final String tableUserAddress = "user_address";
final String tableAddress = "address";
final String tableCartBooks = "cart_books";
private AuthorDB authorDB;
private BookDB bookDB;
@ -43,6 +44,7 @@ public class Database {
private PublisherDB publisherDB;
private UserDB userDB;
private AddressDB addressDB;
private ShoppingCartDB shoppingCartDB;
private static Database instance;
@ -149,6 +151,13 @@ public class Database {
return addressDB;
}
@Contract
public ShoppingCartDB shoppingCart() {
if(shoppingCartDB == null)
shoppingCartDB = new ShoppingCartDB(this);
return shoppingCartDB;
}
/**

View File

@ -0,0 +1,106 @@
package de.hsel.itech.db;
import de.hsel.itech.db.pojo.ShoppingCartItem;
import de.hsel.itech.db.pojo.User;
import org.jetbrains.annotations.NotNull;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* handles all database stuff for the shopping cart.
*
* @author Johannes Theiner
* @version 0.1
* @since 0.5
*/
public class ShoppingCartDB {
private Database database;
ShoppingCartDB(Database database) {
this.database = database;
}
/**
* get all items from users shopping cart.
*
* @param user which users shopping cart to get.
* @return list of shopping cart items.
*/
@NotNull
public List<ShoppingCartItem> get(@NotNull User user) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetsById(database.tableCartBooks, "user", user.getId());
assert entry != null;
List<ShoppingCartItem> list = new ArrayList<>();
try {
ResultSet rs = entry.getKey();
while(rs.next()) {
list.add(new ShoppingCartItem(user, database.book().get(rs.getLong("book")), rs.getInt("count")));
}
entry.getValue().close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
/**
* insert new item to users shopping cart.
*
* @param item item to insert
* @return insert count
*/
public int insert(@NotNull ShoppingCartItem item) {
Connection connection = database.getConnection();
int insertCount = 0;
try {
assert connection != null;
PreparedStatement statement = connection.prepareStatement("INSERT INTO cart_books (user, book, count) VALUES (?, ?, ?)");
statement.setLong(1, item.getUser().getId());
statement.setLong(2, item.getArticle().getId());
statement.setInt(3, item.getCount());
insertCount += statement.executeUpdate();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
return insertCount;
}
/**
* delete item from shopping cart.
*
* @param item item to delete
* @return deletion count
*/
public int delete(@NotNull ShoppingCartItem item) {
Connection connection = database.getConnection();
int deleteCount = 0;
try {
assert connection != null;
PreparedStatement statement = connection.prepareStatement("DELETE FROM cart_books WHERE user = ? AND book = ?");
statement.setLong(1, item.getUser().getId());
statement.setLong(2, item.getArticle().getId());
deleteCount += statement.executeUpdate();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
return deleteCount;
}
}

View File

@ -63,7 +63,7 @@ public class UserDB {
PreparedStatement statement = connection.prepareStatement("INSERT INTO user (name, email, password, type) values (?, ?, ?, ?)");
statement.setString(1, user.getName());
statement.setString(2, user.getEmail());
statement.setString(3, user.getEmail());
statement.setString(3, user.getPassword());
statement.setShort(4, user.getType());
updateCount += statement.executeUpdate();

View File

@ -16,9 +16,8 @@ import lombok.RequiredArgsConstructor;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class ShoppingCart {
public class ShoppingCartItem {
private long id;
@NonNull private User user;
@NonNull private Book article;
@NonNull private int count;

View File

@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS publisher(id bigint primary key auto_increment, name
CREATE TABLE IF NOT EXISTS author(id bigint primary key auto_increment, name varchar(50) unique);
CREATE TABLE IF NOT EXISTS author_book(author bigint, book bigint, primary key(author, book), foreign key (author) REFERENCES author(id), foreign key (book) references book(id) on delete cascade);
CREATE TABLE IF NOT EXISTS category(id bigint primary key auto_increment, name varchar(25), image varchar(11));
CREATE TABLE IF NOT EXISTS user(id bigint primary key auto_increment, name varchar(50), email varchar(255), password binary(40), type tinyint);
CREATE TABLE IF NOT EXISTS user(id bigint primary key auto_increment, name varchar(50), email varchar(255), password binary(60), type tinyint);
CREATE TABLE IF NOT EXISTS address(id bigint primary key auto_increment, name varchar(100), street varchar(100), number smallint, zipCode smallint, city varchar(50));
CREATE TABLE IF NOT EXISTS user_address(id bigint primary key auto_increment, user bigint, address bigint, foreign key (user) REFERENCES user(id), foreign key (address) references address(id) on delete cascade);
CREATE TABLE IF NOT EXISTS payment_type(id bigint primary key auto_increment, name varchar(25) unique);
@ -19,5 +19,4 @@ CREATE TABLE IF NOT EXISTS payment_invoice(id bigint primary key auto_increment)
CREATE TABLE IF NOT EXISTS payment_paypal(id bigint primary key auto_increment, mail varchar(255), auth varchar(255));
CREATE TABLE IF NOT EXISTS `order`(id bigint primary key auto_increment, user bigint, price bigint);
CREATE TABLE IF NOT EXISTS order_book(`order` bigint, book bigint, count tinyint, primary key (`order`, book, count), foreign key (`order`) references `order`(id), foreign key (book) references `order`(id));
CREATE TABLE IF NOT EXISTS shopping_cart(id bigint primary key auto_increment);
CREATE TABLE IF NOT EXISTS cart_books(user bigint, book bigint, count tinyint, primary key(book, user), foreign key (book) REFERENCES book(id), foreign key (user) REFERENCES user(id));