+ database getters for payment types

This commit is contained in:
Johannes Theiner 2019-05-27 18:48:28 +02:00
parent 32b39e2db9
commit 8415eb3b24
7 changed files with 206 additions and 14 deletions

View File

@ -39,6 +39,12 @@ public class Database {
final String tableCartBooks = "cart_books";
final String tableOrder = "order";
final String tableOrderBook = "order_book";
final String tablePaymentType = "payment_type";
final String tablePaymentPayPal = "payment_paypal";
final String tablePaymentCredit = "payment_credit";
final String tablePaymentDebit = "payment_debit";
final String tablePaymentInvoice = "payment_invoice";
final String tableUserPayment = "user_payment";
private AuthorDB authorDB;
private BookDB bookDB;
@ -48,6 +54,7 @@ public class Database {
private AddressDB addressDB;
private ShoppingCartDB shoppingCartDB;
private OrderDB orderDB;
private PaymentDB paymentDB;
private static Database instance;
@ -168,6 +175,13 @@ public class Database {
return orderDB;
}
@Contract
public PaymentDB payment() {
if(paymentDB == null)
paymentDB = new PaymentDB(this);
return paymentDB;
}
/**
* deletes entry from table.

View File

@ -0,0 +1,155 @@
package de.hsel.itech.db;
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.util.AbstractMap;
import java.util.Map;
/**
*
* @author Johannes Theiner
* @version 0.1
* @since 0.5
*/
public class PaymentDB {
private Database database;
PaymentDB(Database database) {
this.database = database;
}
@Nullable
private PaymentType getType(long id) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tablePaymentType, id);
assert entry != null;
try {
ResultSet rs = entry.getKey();
PaymentType type = new PaymentType(id, rs.getString("name"));
entry.getValue().close();
return type;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Nullable
public Payment get(long id) {
Map.Entry<User, PaymentType> metadata = getMetadata(id);
assert metadata != null;
String typeName = metadata.getValue().getName();
if(typeName.equals("PayPal")) {
return getPayPal(id, metadata.getKey());
}
if(typeName.equals("CreditCard")) {
return getCreditCard(id, metadata.getKey());
}
if(typeName.equals("DebitCard")) {
return getDebitCard(id, metadata.getKey());
}
if(typeName.equals("Invoice")) {
return getInvoice(id, metadata.getKey());
}
return null;
}
@Nullable
private PayPalPayment getPayPal(long id, @NotNull User user) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tablePaymentPayPal, id);
assert entry != null;
try {
ResultSet rs = entry.getKey();
PayPalPayment payment = new PayPalPayment(id, user, rs.getString("email"), rs.getString("authCode"));
entry.getValue().close();
return payment;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Nullable
private CreditCardPayment getCreditCard(long id, @NotNull User user) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tablePaymentCredit, id);
assert entry != null;
try {
ResultSet rs = entry.getKey();
CreditCardPayment payment = new CreditCardPayment(id, user, rs.getInt("number"), rs.getString("owner"), rs.getDate("expiration"), rs.getInt("checksum"));
entry.getValue().close();
return payment;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Nullable
private DebitCardPayment getDebitCard(long id, @NotNull User user) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tablePaymentDebit, id);
assert entry != null;
try {
ResultSet rs = entry.getKey();
DebitCardPayment payment = new DebitCardPayment(id, user, rs.getString("iban"), rs.getString("bic"), rs.getString("owner"));
entry.getValue().close();
return payment;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Nullable
private InvoicePayment getInvoice(long id, @NotNull User user) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tablePaymentInvoice, id);
assert entry != null;
try {
ResultSet rs = entry.getKey();
InvoicePayment payment = new InvoicePayment(id, user);
entry.getValue().close();
return payment;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Nullable
private Map.Entry<User, PaymentType> getMetadata(long id) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableUserPayment, id);
assert entry != null;
try {
ResultSet rs = entry.getKey();
Map.Entry<User, PaymentType> metadata = new AbstractMap.SimpleEntry<>(database.user().get(rs.getLong("user")), getType(rs.getLong("type")));
entry.getValue().close();
return metadata;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -1,10 +1,10 @@
package de.hsel.itech.db.pojo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.time.LocalDate;
import java.sql.Date;
/**
* POJO for CreditCard Payment.
@ -15,12 +15,21 @@ import java.time.LocalDate;
**/
@Data
@RequiredArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class CreditCardPayment extends Payment{
@NonNull private User user;
@NonNull private int number;
@NonNull private String owner;
@NonNull private LocalDate expiration;
@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;
this.number = number;
this.owner = owner;
this.expiration = expiration;
this.checksum = checksum;
}
}

View File

@ -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 DebitCard Payment.
@ -14,11 +13,20 @@ import lombok.RequiredArgsConstructor;
**/
@Data
@RequiredArgsConstructor
@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;
this.iban = iban;
this.bic = bic;
this.owner = owner;
}
}

View File

@ -1,8 +1,8 @@
package de.hsel.itech.db.pojo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* POJO for Invoice Payment.
@ -13,8 +13,13 @@ import lombok.RequiredArgsConstructor;
**/
@Data
@RequiredArgsConstructor
@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;
}
}

View File

@ -12,8 +12,8 @@ import lombok.NonNull;
* @since 0.1
**/
@EqualsAndHashCode(callSuper = true)
@Data
@EqualsAndHashCode(callSuper = true)
public class PayPalPayment extends Payment{
@NonNull private User user;

View File

@ -13,10 +13,11 @@ INSERT IGNORE INTO payment_type (name) values ('CreditCard');
INSERT IGNORE INTO payment_type (name) values ('DebitCard');
INSERT IGNORE INTO payment_type (name) values ('Invoice');
INSERT IGNORE INTO payment_type (name) values ('PayPal');
CREATE TABLE IF NOT EXISTS payment_debit(id bigint primary key auto_increment, iban varchar(34), bic varchar(8), owner varchar(50));
CREATE TABLE IF NOT EXISTS payment_credit(id bigint primary key auto_increment, number varchar(19), owner varchar(50), expiration DATE, checksum tinyint);
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 payment_debit(id bigint primary key, iban varchar(34), bic varchar(8), owner varchar(50));
CREATE TABLE IF NOT EXISTS payment_credit(id bigint primary key, number varchar(19), owner varchar(50), expiration DATE, checksum tinyint);
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_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));