~ database handling for payments stuff complete

This commit is contained in:
Johannes Theiner 2019-05-31 11:44:51 +02:00
parent 8415eb3b24
commit 5fe9880ffe
8 changed files with 164 additions and 28 deletions

View File

@ -26,8 +26,7 @@ public class OrderDB {
ResultSet rs = entry.getKey(); ResultSet rs = entry.getKey();
List<OrderItem> items = getItems(id); List<OrderItem> items = getItems(id);
//TODO: change this once database handling for payment stuff is done Payment payment = database.payment().get(rs.getLong("payment"));
Payment payment = null;
Address address = database.address().get(rs.getLong("address")); 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); 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; int insertCount = 0;
try { 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(1, order.getUser().getId());
statement.setLong(2, order.getPrice()); statement.setLong(2, order.getPrice());
statement.setDate(3, order.getDate()); statement.setLong(3, order.getPayment().getId());
statement.setLong(4, order.getPayment().getType().getId()); statement.setDate(4, order.getDate());
statement.setLong(5, order.getPayment().getId()); statement.setLong(5, order.getAddress().getId());
statement.setLong(6, order.getAddress().getId());
insertCount += statement.executeUpdate(); insertCount += statement.executeUpdate();
ResultSet idResultSet = statement.getGeneratedKeys(); ResultSet idResultSet = statement.getGeneratedKeys();

View File

@ -5,10 +5,10 @@ import de.hsel.itech.db.pojo.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.sql.Connection; import java.sql.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -42,6 +42,17 @@ public class PaymentDB {
return null; return null;
} }
@NotNull
public List<Payment> get(@NotNull User user) {
List<Payment> list = new ArrayList<>();
List<Long> ids = database.getIds(database.tableUserPayment);
for(long id : ids) {
list.add(get(id));
}
return list;
}
@Nullable @Nullable
public Payment get(long id) { public Payment get(long id) {
Map.Entry<User, PaymentType> metadata = getMetadata(id); Map.Entry<User, PaymentType> metadata = getMetadata(id);
@ -124,7 +135,10 @@ public class PaymentDB {
try { try {
ResultSet rs = entry.getKey(); 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(); entry.getValue().close();
return payment; return payment;
@ -135,6 +149,141 @@ public class PaymentDB {
return null; 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 @Nullable
private Map.Entry<User, PaymentType> getMetadata(long id) { private Map.Entry<User, PaymentType> getMetadata(long id) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableUserPayment, id); Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableUserPayment, id);

View File

@ -18,15 +18,13 @@ import java.sql.Date;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class CreditCardPayment extends Payment{ public class CreditCardPayment extends Payment{
@NonNull private User user;
@NonNull private int number; @NonNull private int number;
@NonNull private String owner; @NonNull private String owner;
@NonNull private Date expiration; @NonNull private Date expiration;
@NonNull private int checksum; @NonNull private int checksum;
public CreditCardPayment(long id, User user, int number, String owner, Date expiration, int checksum) { public CreditCardPayment(long id, User user, int number, String owner, Date expiration, int checksum) {
super(id, new PaymentType("CreditCard")); super(id, new PaymentType(1, "CreditCard"), user);
this.user = user;
this.number = number; this.number = number;
this.owner = owner; this.owner = owner;
this.expiration = expiration; this.expiration = expiration;

View File

@ -16,14 +16,12 @@ import lombok.NonNull;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class DebitCardPayment extends Payment{ public class DebitCardPayment extends Payment{
@NonNull private User user;
@NonNull private String iban; @NonNull private String iban;
@NonNull private String bic; @NonNull private String bic;
@NonNull private String owner; @NonNull private String owner;
public DebitCardPayment(long id, User user, String iban, String bic, String owner) { public DebitCardPayment(long id, User user, String iban, String bic, String owner) {
super(id, new PaymentType("DebitCard")); super(id, new PaymentType(2, "DebitCard"), user);
this.user = user;
this.iban = iban; this.iban = iban;
this.bic = bic; this.bic = bic;
this.owner = owner; this.owner = owner;

View File

@ -2,7 +2,6 @@ package de.hsel.itech.db.pojo;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.NonNull;
/** /**
* POJO for Invoice Payment. * POJO for Invoice Payment.
@ -16,10 +15,7 @@ import lombok.NonNull;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class InvoicePayment extends Payment{ public class InvoicePayment extends Payment{
@NonNull private User user;
public InvoicePayment(long id, User user) { public InvoicePayment(long id, User user) {
super(id, new PaymentType("Invoice")); super(id, new PaymentType(3, "Invoice"), user);
this.user = user;
} }
} }

View File

@ -16,13 +16,11 @@ import lombok.NonNull;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class PayPalPayment extends Payment{ public class PayPalPayment extends Payment{
@NonNull private User user;
@NonNull private String email; @NonNull private String email;
@NonNull private String authCode; @NonNull private String authCode;
public PayPalPayment(long id, User user, String email, String authCode) { public PayPalPayment(long id, User user, String email, String authCode) {
super(id, new PaymentType(2, "PayPal")); super(id, new PaymentType(4, "PayPal"), user);
this.user = user;
this.email = email; this.email = email;
this.authCode = authCode; this.authCode = authCode;
} }

View File

@ -11,4 +11,5 @@ public class Payment {
private long id; private long id;
@NonNull private PaymentType type; @NonNull private PaymentType type;
@NonNull private User user;
} }

View File

@ -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 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 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); 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_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 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 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 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)); 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));