diff --git a/pom.xml b/pom.xml index cc3a8ab..79f2417 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ de.hsel itech - 0.1.0-SNAPSHOT + 0.5.0-SNAPSHOT war diff --git a/src/main/java/de/hsel/itech/db/AddressDB.java b/src/main/java/de/hsel/itech/db/AddressDB.java index 7c1e18a..dbe969c 100644 --- a/src/main/java/de/hsel/itech/db/AddressDB.java +++ b/src/main/java/de/hsel/itech/db/AddressDB.java @@ -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
getAll(@NotNull User user) { Map.Entry 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 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 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); } diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index 0fb4b6d..af3748c 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -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; + } + /** diff --git a/src/main/java/de/hsel/itech/db/ShoppingCartDB.java b/src/main/java/de/hsel/itech/db/ShoppingCartDB.java new file mode 100644 index 0000000..78775dc --- /dev/null +++ b/src/main/java/de/hsel/itech/db/ShoppingCartDB.java @@ -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 get(@NotNull User user) { + Map.Entry entry = database.getResultSetsById(database.tableCartBooks, "user", user.getId()); + assert entry != null; + + List 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; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/UserDB.java b/src/main/java/de/hsel/itech/db/UserDB.java index dd562ac..36bbd15 100644 --- a/src/main/java/de/hsel/itech/db/UserDB.java +++ b/src/main/java/de/hsel/itech/db/UserDB.java @@ -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(); diff --git a/src/main/java/de/hsel/itech/db/pojo/ShoppingCart.java b/src/main/java/de/hsel/itech/db/pojo/ShoppingCartItem.java similarity index 88% rename from src/main/java/de/hsel/itech/db/pojo/ShoppingCart.java rename to src/main/java/de/hsel/itech/db/pojo/ShoppingCartItem.java index c7fd9ec..91bca48 100644 --- a/src/main/java/de/hsel/itech/db/pojo/ShoppingCart.java +++ b/src/main/java/de/hsel/itech/db/pojo/ShoppingCartItem.java @@ -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; diff --git a/src/main/resources/database.sql b/src/main/resources/database.sql index 38d4af4..f0d0f96 100644 --- a/src/main/resources/database.sql +++ b/src/main/resources/database.sql @@ -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)); \ No newline at end of file