diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index a1ce039..4d38309 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -37,7 +37,7 @@ public class Database { final String tableUserAddress = "user_address"; final String tableAddress = "address"; final String tableCartBooks = "cart_books"; - final String tableOrder = "order"; + final String tableOrder = "`order`"; final String tableOrderBook = "order_book"; final String tablePaymentType = "payment_type"; final String tablePaymentPayPal = "payment_paypal"; diff --git a/src/main/java/de/hsel/itech/db/OrderDB.java b/src/main/java/de/hsel/itech/db/OrderDB.java index 981c2fd..1ad30f6 100644 --- a/src/main/java/de/hsel/itech/db/OrderDB.java +++ b/src/main/java/de/hsel/itech/db/OrderDB.java @@ -23,13 +23,18 @@ public class OrderDB { assert entry != null; try { + Order order = null; ResultSet rs = entry.getKey(); - List items = getItems(id); - User user = database.user().get(rs.getLong("user")); - Payment payment = database.payment().get(rs.getLong("payment"), user); - Address address = database.address().get(rs.getLong("address"), user); - Order order = new Order(id, user, items, rs.getDate("date"), rs.getLong("price"), payment, address); + rs.beforeFirst(); + + while (rs.next()) { + List items = getItems(id); + User user = database.user().get(rs.getLong("user")); + Payment payment = database.payment().get(rs.getLong("payment"), user); + Address address = database.address().get(rs.getLong("address"), user); + order = new Order(id, user, items, rs.getDate("date"), rs.getLong("price"), payment, address); + } entry.getValue().close(); return order; @@ -39,8 +44,25 @@ public class OrderDB { return null; } + public List getAll(@NotNull User user) { + Map.Entry entry = database.getResultSetByValue(database.tableOrder, "user", user.getId()); + assert entry != null; + + List list = new ArrayList<>(); + try { + ResultSet rs = entry.getKey(); + rs.beforeFirst(); + while (rs.next()) { + list.add(get(rs.getLong("id"))); + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return list; + } + private List getItems(long id) { - Map.Entry entry = database.getResultSetsById(database.tableOrderBook, "order", id); + Map.Entry entry = database.getResultSetsById(database.tableOrderBook, database.tableOrder, id); assert entry != null; List items = new ArrayList<>(); diff --git a/src/main/java/de/hsel/itech/db/PaymentDB.java b/src/main/java/de/hsel/itech/db/PaymentDB.java index 425baa7..d41464f 100644 --- a/src/main/java/de/hsel/itech/db/PaymentDB.java +++ b/src/main/java/de/hsel/itech/db/PaymentDB.java @@ -55,6 +55,7 @@ public class PaymentDB { try { ResultSet rs = entry.getKey(); rs.beforeFirst(); + System.out.println(user); while(rs.next()) { list.add(get(rs.getLong("id"), user)); } @@ -68,10 +69,12 @@ public class PaymentDB { } @Nullable - public Payment get(long id, User user) { + public Payment get(long id, @NotNull User user) { Map.Entry metadata = getMetadata(id); assert metadata != null; + System.out.println(user); + switch (metadata.getValue().getName()) { case "PayPal": return getPayPal(id, user); diff --git a/src/main/java/de/hsel/itech/db/pojo/Order.java b/src/main/java/de/hsel/itech/db/pojo/Order.java index 98295c8..52a26c0 100644 --- a/src/main/java/de/hsel/itech/db/pojo/Order.java +++ b/src/main/java/de/hsel/itech/db/pojo/Order.java @@ -6,6 +6,7 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; import java.sql.Date; +import java.util.ArrayList; import java.util.List; @@ -32,4 +33,13 @@ public class Order { @NonNull private long price; @NonNull private Payment payment; @NonNull private Address address; + + public List getItemNames() { + List titles = new ArrayList<>(); + for(OrderItem item : items) { + titles.add(item.getBook().getTitle()); + } + + return titles; + } } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/jsf/OrderBean.java b/src/main/java/de/hsel/itech/jsf/OrderBean.java new file mode 100644 index 0000000..9546222 --- /dev/null +++ b/src/main/java/de/hsel/itech/jsf/OrderBean.java @@ -0,0 +1,31 @@ +package de.hsel.itech.jsf; + +import de.hsel.itech.db.Database; +import de.hsel.itech.db.pojo.Order; +import de.hsel.itech.db.pojo.User; + +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; +import javax.faces.context.FacesContext; +import java.util.List; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 1.0 + **/ + +@ManagedBean +@RequestScoped +public class OrderBean { + + public User getUser() { + FacesContext context = FacesContext.getCurrentInstance(); + return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class); + } + + public List getOrders() { + System.out.println(getUser()); + return Database.getInstance().order().getAll(getUser()); + } +} \ No newline at end of file diff --git a/src/main/webapp/orders.xhtml b/src/main/webapp/orders.xhtml new file mode 100644 index 0000000..9016bd6 --- /dev/null +++ b/src/main/webapp/orders.xhtml @@ -0,0 +1,27 @@ + + + + + + + + + +
+
+
+ #{order.itemNames} +
+
+
+
+ +
+ +
+
+