+ database handling for orders

This commit is contained in:
Johannes Theiner 2019-05-22 15:22:18 +02:00
parent 1742ee4c14
commit 8ebff9a7f0
10 changed files with 196 additions and 21 deletions

View File

@ -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.
*

View File

@ -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<ResultSet, Connection> entry = database.getResultSetById(database.tableOrder, id);
assert entry != null;
try {
ResultSet rs = entry.getKey();
List<OrderItem> 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<OrderItem> getItems(long id) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetsById(database.tableOrderBook, "order", id);
assert entry != null;
List<OrderItem> 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;
}
}

View File

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

View File

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

View File

@ -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;
}

View File

@ -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<OrderItem> 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;
}

View File

@ -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;
}

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 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;
}
}

View File

@ -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;
}

View File

@ -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 {