+ database handling for orders
This commit is contained in:
parent
1742ee4c14
commit
8ebff9a7f0
|
@ -37,6 +37,8 @@ public class Database {
|
||||||
final String tableUserAddress = "user_address";
|
final String tableUserAddress = "user_address";
|
||||||
final String tableAddress = "address";
|
final String tableAddress = "address";
|
||||||
final String tableCartBooks = "cart_books";
|
final String tableCartBooks = "cart_books";
|
||||||
|
final String tableOrder = "order";
|
||||||
|
final String tableOrderBook = "order_book";
|
||||||
|
|
||||||
private AuthorDB authorDB;
|
private AuthorDB authorDB;
|
||||||
private BookDB bookDB;
|
private BookDB bookDB;
|
||||||
|
@ -45,6 +47,7 @@ public class Database {
|
||||||
private UserDB userDB;
|
private UserDB userDB;
|
||||||
private AddressDB addressDB;
|
private AddressDB addressDB;
|
||||||
private ShoppingCartDB shoppingCartDB;
|
private ShoppingCartDB shoppingCartDB;
|
||||||
|
private OrderDB orderDB;
|
||||||
|
|
||||||
private static Database instance;
|
private static Database instance;
|
||||||
|
|
||||||
|
@ -158,10 +161,16 @@ public class Database {
|
||||||
return shoppingCartDB;
|
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 id database id
|
||||||
* @param table table name
|
* @param table table name
|
||||||
|
@ -184,6 +193,31 @@ public class Database {
|
||||||
return deleteCount;
|
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.
|
* getAll all ids from specified table.
|
||||||
*
|
*
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,9 +18,8 @@ import java.time.LocalDate;
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class CreditCardPayment {
|
public class CreditCardPayment extends Payment{
|
||||||
|
|
||||||
private long id;
|
|
||||||
@NonNull private User user;
|
@NonNull private User user;
|
||||||
@NonNull private int number;
|
@NonNull private int number;
|
||||||
@NonNull private String owner;
|
@NonNull private String owner;
|
||||||
|
|
|
@ -16,9 +16,8 @@ import lombok.RequiredArgsConstructor;
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class DebitCardPayment {
|
public class DebitCardPayment extends Payment{
|
||||||
|
|
||||||
private long id;
|
|
||||||
@NonNull private User user;
|
@NonNull private User user;
|
||||||
@NonNull private String iban;
|
@NonNull private String iban;
|
||||||
@NonNull private String bic;
|
@NonNull private String bic;
|
||||||
|
|
|
@ -16,8 +16,7 @@ import lombok.RequiredArgsConstructor;
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class InvoicePayment {
|
public class InvoicePayment extends Payment{
|
||||||
|
|
||||||
private long id;
|
|
||||||
@NonNull private User user;
|
@NonNull private User user;
|
||||||
}
|
}
|
|
@ -5,7 +5,8 @@ import lombok.Data;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.RequiredArgsConstructor;
|
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 {
|
public class Order {
|
||||||
|
|
||||||
private long id;
|
private long id;
|
||||||
@NonNull private LocalDateTime date;
|
@NonNull private User user;
|
||||||
@NonNull private Book book;
|
@NonNull private List<OrderItem> items;
|
||||||
|
@NonNull private Date date;
|
||||||
/**
|
/**
|
||||||
* price in cents.
|
* price in cents.
|
||||||
*/
|
*/
|
||||||
@NonNull private int price;
|
@NonNull private long price;
|
||||||
@NonNull private PaymentType paymentType;
|
@NonNull private Payment payment;
|
||||||
@NonNull private Address address;
|
@NonNull private Address address;
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
}
|
|
@ -1,9 +1,8 @@
|
||||||
package de.hsel.itech.db.pojo;
|
package de.hsel.itech.db.pojo;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* POJO for PayPal Payment.
|
* POJO for PayPal Payment.
|
||||||
|
@ -13,13 +12,18 @@ import lombok.RequiredArgsConstructor;
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
**/
|
**/
|
||||||
|
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
public class PayPalPayment extends Payment{
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class PayPalPayment {
|
|
||||||
|
|
||||||
private long id;
|
|
||||||
@NonNull private User user;
|
@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) {
|
||||||
|
super(id, new PaymentType(2, "PayPal"));
|
||||||
|
this.user = user;
|
||||||
|
this.email = email;
|
||||||
|
this.authCode = authCode;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
}
|
|
@ -1,6 +1,5 @@
|
||||||
package de.hsel.itech.db.pojo;
|
package de.hsel.itech.db.pojo;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
@ -14,7 +13,6 @@ import lombok.RequiredArgsConstructor;
|
||||||
**/
|
**/
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ShoppingCartItem {
|
public class ShoppingCartItem {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue