diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index af3748c..7999bc0 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -37,6 +37,8 @@ public class Database { final String tableUserAddress = "user_address"; final String tableAddress = "address"; final String tableCartBooks = "cart_books"; + final String tableOrder = "order"; + final String tableOrderBook = "order_book"; private AuthorDB authorDB; private BookDB bookDB; @@ -45,6 +47,7 @@ public class Database { private UserDB userDB; private AddressDB addressDB; private ShoppingCartDB shoppingCartDB; + private OrderDB orderDB; private static Database instance; @@ -158,10 +161,16 @@ public class Database { return shoppingCartDB; } + @Contract + public OrderDB order() { + if(orderDB == null) + orderDB = new OrderDB(this); + return orderDB; + } /** - * deletes id from table. + * deletes entry from table. * * @param id database id * @param table table name @@ -184,6 +193,31 @@ public class Database { return deleteCount; } + /** + * deletes entry from table. + * + * @param id id of entry to delete + * @param table table to delete from + * @param column column to delete id from + * @return deletion count + */ + int delete(long id, @NotNull String table, @NotNull String column) { + Connection connection = getConnection(); + assert connection != null; + + int deleteCount = 0; + try { + PreparedStatement statement = connection.prepareStatement("DELETE FROM " + table + " WHERE " + column + "=?"); + statement.setLong(1, id); + + deleteCount = +statement.executeUpdate(); + + } catch (SQLException ex) { + ex.printStackTrace(); + } + return deleteCount; + } + /** * getAll all ids from specified table. * diff --git a/src/main/java/de/hsel/itech/db/OrderDB.java b/src/main/java/de/hsel/itech/db/OrderDB.java new file mode 100644 index 0000000..8030536 --- /dev/null +++ b/src/main/java/de/hsel/itech/db/OrderDB.java @@ -0,0 +1,108 @@ +package de.hsel.itech.db; + +import de.hsel.itech.db.pojo.*; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class OrderDB { + + private Database database; + + OrderDB(@NotNull Database database) { + this.database = database; + } + + @Nullable + public Order get(long id) { + Map.Entry entry = database.getResultSetById(database.tableOrder, id); + assert entry != null; + + try { + + ResultSet rs = entry.getKey(); + List items = getItems(id); + //TODO: change this once database handling for payment stuff is done + Payment payment = null; + Address address = database.address().get(rs.getLong("address")); + Order order = new Order(id, database.user().get(rs.getLong("user")), items, rs.getDate("date"), rs.getLong("price"), payment, address); + + entry.getValue().close(); + return order; + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + private List getItems(long id) { + Map.Entry entry = database.getResultSetsById(database.tableOrderBook, "order", id); + assert entry != null; + + List items = new ArrayList<>(); + + try { + ResultSet rs = entry.getKey(); + while(rs.next()) { + items.add(new OrderItem(database.book().get(rs.getLong("book")), rs.getInt("count"))); + } + + entry.getValue().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + return items; + } + + public int insert(@NotNull Order order) { + Connection connection = database.getConnection(); + assert connection != null; + + int insertCount = 0; + + try { + PreparedStatement statement = connection.prepareStatement("INSERT INTO `order` (user, price, date, paymentType, payment, address) VALUES (?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); + statement.setLong(1, order.getUser().getId()); + statement.setLong(2, order.getPrice()); + statement.setDate(3, order.getDate()); + statement.setLong(4, order.getPayment().getType().getId()); + statement.setLong(5, order.getPayment().getId()); + statement.setLong(6, order.getAddress().getId()); + + insertCount += statement.executeUpdate(); + ResultSet idResultSet = statement.getGeneratedKeys(); + long lastId = -1; + while (idResultSet.next()) { + lastId = idResultSet.getLong("id"); + } + + for(OrderItem item : order.getItems()) { + PreparedStatement itemStatement = connection.prepareStatement("INSERT INTO order_book (`order`, book, count) VALUES (?, ?, ?)"); + itemStatement.setLong(1, lastId); + itemStatement.setLong(2, item.getBook().getId()); + itemStatement.setInt(3, item.getCount()); + + insertCount += itemStatement.executeUpdate(); + } + + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return insertCount; + } + + public int delete(@NotNull Order order) { + int deleteCount = 0; + + deleteCount += database.delete(order.getId(), database.tableOrder); + deleteCount += database.delete(order.getId(), database.tableOrderBook, "order"); + + return deleteCount; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java b/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java index 0d31b17..f47f23d 100644 --- a/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java @@ -18,9 +18,8 @@ import java.time.LocalDate; @Data @AllArgsConstructor @RequiredArgsConstructor -public class CreditCardPayment { +public class CreditCardPayment extends Payment{ - private long id; @NonNull private User user; @NonNull private int number; @NonNull private String owner; diff --git a/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java b/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java index 35e3985..9079997 100644 --- a/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java @@ -16,9 +16,8 @@ import lombok.RequiredArgsConstructor; @Data @AllArgsConstructor @RequiredArgsConstructor -public class DebitCardPayment { +public class DebitCardPayment extends Payment{ - private long id; @NonNull private User user; @NonNull private String iban; @NonNull private String bic; diff --git a/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java b/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java index 6e1411b..3f6ffaf 100644 --- a/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java @@ -16,8 +16,7 @@ import lombok.RequiredArgsConstructor; @Data @AllArgsConstructor @RequiredArgsConstructor -public class InvoicePayment { +public class InvoicePayment extends Payment{ - private long id; @NonNull private User user; } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/Order.java b/src/main/java/de/hsel/itech/db/pojo/Order.java index 203a8eb..98295c8 100644 --- a/src/main/java/de/hsel/itech/db/pojo/Order.java +++ b/src/main/java/de/hsel/itech/db/pojo/Order.java @@ -5,7 +5,8 @@ import lombok.Data; import lombok.NonNull; import lombok.RequiredArgsConstructor; -import java.time.LocalDateTime; +import java.sql.Date; +import java.util.List; /** @@ -22,13 +23,13 @@ import java.time.LocalDateTime; public class Order { private long id; - @NonNull private LocalDateTime date; - @NonNull private Book book; - + @NonNull private User user; + @NonNull private List items; + @NonNull private Date date; /** * price in cents. */ - @NonNull private int price; - @NonNull private PaymentType paymentType; + @NonNull private long price; + @NonNull private Payment payment; @NonNull private Address address; } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/OrderItem.java b/src/main/java/de/hsel/itech/db/pojo/OrderItem.java new file mode 100644 index 0000000..f4ced0a --- /dev/null +++ b/src/main/java/de/hsel/itech/db/pojo/OrderItem.java @@ -0,0 +1,19 @@ +package de.hsel.itech.db.pojo; + +import lombok.*; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.5 + */ + +@Data +@AllArgsConstructor +@RequiredArgsConstructor +public class OrderItem { + + private Order order; + @NonNull private Book book; + @NonNull private int count; +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java b/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java index 0c4044b..082dc3d 100644 --- a/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java @@ -1,9 +1,8 @@ package de.hsel.itech.db.pojo; -import lombok.AllArgsConstructor; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NonNull; -import lombok.RequiredArgsConstructor; /** * POJO for PayPal Payment. @@ -13,13 +12,18 @@ import lombok.RequiredArgsConstructor; * @since 0.1 **/ +@EqualsAndHashCode(callSuper = true) @Data -@AllArgsConstructor -@RequiredArgsConstructor -public class PayPalPayment { +public class PayPalPayment extends Payment{ - private long id; @NonNull private User user; @NonNull private String email; @NonNull private String authCode; + + public PayPalPayment(long id, User user, String email, String authCode) { + super(id, new PaymentType(2, "PayPal")); + this.user = user; + this.email = email; + this.authCode = authCode; + } } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/Payment.java b/src/main/java/de/hsel/itech/db/pojo/Payment.java new file mode 100644 index 0000000..17d76df --- /dev/null +++ b/src/main/java/de/hsel/itech/db/pojo/Payment.java @@ -0,0 +1,14 @@ +package de.hsel.itech.db.pojo; + + +import lombok.*; + +@Data +@NoArgsConstructor +@RequiredArgsConstructor +@AllArgsConstructor +public class Payment { + + private long id; + @NonNull private PaymentType type; +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/ShoppingCartItem.java b/src/main/java/de/hsel/itech/db/pojo/ShoppingCartItem.java index 91bca48..270cf25 100644 --- a/src/main/java/de/hsel/itech/db/pojo/ShoppingCartItem.java +++ b/src/main/java/de/hsel/itech/db/pojo/ShoppingCartItem.java @@ -1,6 +1,5 @@ package de.hsel.itech.db.pojo; -import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; import lombok.RequiredArgsConstructor; @@ -14,7 +13,6 @@ import lombok.RequiredArgsConstructor; **/ @Data -@AllArgsConstructor @RequiredArgsConstructor public class ShoppingCartItem {