From 5fe9880ffe6dfad431036f16f1ef496ed7786e59 Mon Sep 17 00:00:00 2001 From: joethei Date: Fri, 31 May 2019 11:44:51 +0200 Subject: [PATCH] ~ database handling for payments stuff complete --- src/main/java/de/hsel/itech/db/OrderDB.java | 12 +- src/main/java/de/hsel/itech/db/PaymentDB.java | 157 +++++++++++++++++- .../hsel/itech/db/pojo/CreditCardPayment.java | 4 +- .../hsel/itech/db/pojo/DebitCardPayment.java | 4 +- .../de/hsel/itech/db/pojo/InvoicePayment.java | 6 +- .../de/hsel/itech/db/pojo/PayPalPayment.java | 4 +- .../java/de/hsel/itech/db/pojo/Payment.java | 1 + src/main/resources/database.sql | 4 +- 8 files changed, 164 insertions(+), 28 deletions(-) diff --git a/src/main/java/de/hsel/itech/db/OrderDB.java b/src/main/java/de/hsel/itech/db/OrderDB.java index 8030536..f85705a 100644 --- a/src/main/java/de/hsel/itech/db/OrderDB.java +++ b/src/main/java/de/hsel/itech/db/OrderDB.java @@ -26,8 +26,7 @@ public class OrderDB { ResultSet rs = entry.getKey(); List items = getItems(id); - //TODO: change this once database handling for payment stuff is done - Payment payment = null; + Payment payment = database.payment().get(rs.getLong("payment")); 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); @@ -66,13 +65,12 @@ public class OrderDB { int insertCount = 0; try { - PreparedStatement statement = connection.prepareStatement("INSERT INTO `order` (user, price, date, paymentType, payment, address) VALUES (?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); + PreparedStatement statement = connection.prepareStatement("INSERT INTO `order` (user, price, payment, date, 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()); + statement.setLong(3, order.getPayment().getId()); + statement.setDate(4, order.getDate()); + statement.setLong(5, order.getAddress().getId()); insertCount += statement.executeUpdate(); ResultSet idResultSet = statement.getGeneratedKeys(); diff --git a/src/main/java/de/hsel/itech/db/PaymentDB.java b/src/main/java/de/hsel/itech/db/PaymentDB.java index 9865c6e..0f87a51 100644 --- a/src/main/java/de/hsel/itech/db/PaymentDB.java +++ b/src/main/java/de/hsel/itech/db/PaymentDB.java @@ -5,10 +5,10 @@ import de.hsel.itech.db.pojo.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.List; import java.util.Map; /** @@ -42,6 +42,17 @@ public class PaymentDB { return null; } + @NotNull + public List get(@NotNull User user) { + List list = new ArrayList<>(); + + List ids = database.getIds(database.tableUserPayment); + for(long id : ids) { + list.add(get(id)); + } + return list; + } + @Nullable public Payment get(long id) { Map.Entry metadata = getMetadata(id); @@ -124,7 +135,10 @@ public class PaymentDB { try { ResultSet rs = entry.getKey(); - InvoicePayment payment = new InvoicePayment(id, user); + InvoicePayment payment = null; + if(rs.getLong("id") == id && rs.getLong("user") == user.getId()) { + payment = new InvoicePayment(id, user); + } entry.getValue().close(); return payment; @@ -135,6 +149,141 @@ public class PaymentDB { return null; } + public int insert(@NotNull PayPalPayment payment) { + int insertCount = 0; + Connection connection = database.getConnection(); + assert connection != null; + + try { + long id = insertType(payment); + + PreparedStatement statement = connection.prepareStatement("INSERT INTO payment_paypal (id, mail, auth) VALUES (?, ?, ?)"); + statement.setLong(1, id); + statement.setString(2, payment.getEmail()); + statement.setString(3, payment.getAuthCode()); + insertCount += statement.executeUpdate(); + + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return insertCount; + } + + public int insert(@NotNull CreditCardPayment payment) { + int insertCount = 0; + Connection connection = database.getConnection(); + assert connection != null; + + try { + long id = insertType(payment); + + PreparedStatement statement = connection.prepareStatement("INSERT INTO payment_credit (id, number, owner, expiration, checksum) VALUES (?, ?, ?, ?, ?)"); + statement.setLong(1, id); + statement.setInt(2, payment.getNumber()); + statement.setString(3, payment.getOwner()); + statement.setDate(4, payment.getExpiration()); + statement.setInt(5, payment.getChecksum()); + + insertCount += statement.executeUpdate(); + + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + return insertCount; + } + + public int insert(@NotNull DebitCardPayment payment) { + int insertCount = 0; + Connection connection = database.getConnection(); + assert connection != null; + + try { + long id = insertType(payment); + + PreparedStatement statement = connection.prepareStatement("INSERT INTO payment_debit (id, iban, bic, owner) VALUES (?, ?, ?, ?)"); + statement.setLong(1, id); + statement.setString(2, payment.getIban()); + statement.setString(3, payment.getBic()); + statement.setString(4, payment.getOwner()); + + insertCount += statement.executeUpdate(); + + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + return insertCount; + } + + public int insert(@NotNull InvoicePayment payment) { + int insertCount = 0; + Connection connection = database.getConnection(); + assert connection != null; + + try { + long id = insertType(payment); + + PreparedStatement statement = connection.prepareStatement("INSERT INTO payment_invoice (id) VALUES (?)"); + statement.setLong(1, id); + + insertCount += statement.executeUpdate(); + + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + return insertCount; + } + + private long insertType(@NotNull Payment payment) { + Connection connection = database.getConnection(); + assert connection != null; + + try { + PreparedStatement statement = connection.prepareStatement("INSERT INTO user_payment (type, user) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS); + statement.setLong(1, payment.getType().getId()); + statement.setLong(2, payment.getUser().getId()); + + statement.executeUpdate(); + + long id = statement.getGeneratedKeys().getLong("id"); + + connection.close(); + + return id; + } catch (SQLException e) { + e.printStackTrace(); + } + return -1; + } + + public int delete(@NotNull Payment payment) { + int deleteCount = 0; + + deleteCount += database.delete(payment.getId(), database.tableUserPayment); + String type = payment.getType().getName(); + switch (type) { + case "CreditCard": + deleteCount += database.delete(payment.getId(), database.tablePaymentCredit); + break; + case "DebitCard": + deleteCount += database.delete(payment.getId(), database.tablePaymentDebit); + break; + case "Invoice": + deleteCount += database.delete(payment.getId(), database.tablePaymentInvoice); + break; + case "PayPal": + deleteCount += database.delete(payment.getId(), database.tablePaymentPayPal); + break; + } + return deleteCount; + } + @Nullable private Map.Entry getMetadata(long id) { Map.Entry entry = database.getResultSetById(database.tableUserPayment, id); 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 783c0dc..694ec88 100644 --- a/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java @@ -18,15 +18,13 @@ import java.sql.Date; @EqualsAndHashCode(callSuper = true) public class CreditCardPayment extends Payment{ - @NonNull private User user; @NonNull private int number; @NonNull private String owner; @NonNull private Date expiration; @NonNull private int checksum; public CreditCardPayment(long id, User user, int number, String owner, Date expiration, int checksum) { - super(id, new PaymentType("CreditCard")); - this.user = user; + super(id, new PaymentType(1, "CreditCard"), user); this.number = number; this.owner = owner; this.expiration = expiration; 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 ee2a385..e9f5810 100644 --- a/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java @@ -16,14 +16,12 @@ import lombok.NonNull; @EqualsAndHashCode(callSuper = true) public class DebitCardPayment extends Payment{ - @NonNull private User user; @NonNull private String iban; @NonNull private String bic; @NonNull private String owner; public DebitCardPayment(long id, User user, String iban, String bic, String owner) { - super(id, new PaymentType("DebitCard")); - this.user = user; + super(id, new PaymentType(2, "DebitCard"), user); this.iban = iban; this.bic = bic; this.owner = owner; 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 e925b97..20a07ed 100644 --- a/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java @@ -2,7 +2,6 @@ package de.hsel.itech.db.pojo; import lombok.Data; import lombok.EqualsAndHashCode; -import lombok.NonNull; /** * POJO for Invoice Payment. @@ -16,10 +15,7 @@ import lombok.NonNull; @EqualsAndHashCode(callSuper = true) public class InvoicePayment extends Payment{ - @NonNull private User user; - public InvoicePayment(long id, User user) { - super(id, new PaymentType("Invoice")); - this.user = user; + super(id, new PaymentType(3, "Invoice"), user); } } \ 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 163b76d..1d6f65c 100644 --- a/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java @@ -16,13 +16,11 @@ import lombok.NonNull; @EqualsAndHashCode(callSuper = true) public class PayPalPayment extends Payment{ - @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; + super(id, new PaymentType(4, "PayPal"), user); this.email = email; this.authCode = authCode; } diff --git a/src/main/java/de/hsel/itech/db/pojo/Payment.java b/src/main/java/de/hsel/itech/db/pojo/Payment.java index 17d76df..ef573e5 100644 --- a/src/main/java/de/hsel/itech/db/pojo/Payment.java +++ b/src/main/java/de/hsel/itech/db/pojo/Payment.java @@ -11,4 +11,5 @@ public class Payment { private long id; @NonNull private PaymentType type; + @NonNull private User user; } \ No newline at end of file diff --git a/src/main/resources/database.sql b/src/main/resources/database.sql index 35b769b..5c58543 100644 --- a/src/main/resources/database.sql +++ b/src/main/resources/database.sql @@ -1,5 +1,3 @@ -CREATE TABLE IF NOT EXISTS test(id bigint primary key auto_increment, hello varchar(25)); -INSERT IGNORE INTO test(hello) values ('Welt'); CREATE TABLE IF NOT EXISTS book (id bigint primary key auto_increment, isbn bigint unique, title varchar(50), description varchar(500), price int, year year, publisher bigint, category bigint, image varchar(11)); CREATE TABLE IF NOT EXISTS publisher(id bigint primary key auto_increment, name varchar(50) unique); CREATE TABLE IF NOT EXISTS author(id bigint primary key auto_increment, name varchar(50) unique); @@ -18,6 +16,6 @@ CREATE TABLE IF NOT EXISTS payment_credit(id bigint primary key, number varchar( CREATE TABLE IF NOT EXISTS payment_invoice(id bigint primary key); CREATE TABLE IF NOT EXISTS payment_paypal(id bigint primary key, mail varchar(255), auth varchar(255)); CREATE TABLE IF NOT EXISTS user_payment(id bigint primary key auto_increment, type bigint, user bigint, foreign key (type) references payment_type(id), foreign key (user) references user(id)); -CREATE TABLE IF NOT EXISTS `order`(id bigint primary key auto_increment, user bigint, price bigint); +CREATE TABLE IF NOT EXISTS `order`(id bigint primary key auto_increment, user bigint, price bigint, payment bigint, date date, address bigint, foreign key (user) references user(id), foreign key (payment) references user_payment(id), foreign key (address) references address(id)); 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 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