From 1a115e00a9a9a70ea7acdbb0fc7c398efdbc71a5 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 4 Jun 2019 23:39:40 +0200 Subject: [PATCH] +shopping cart --- src/main/java/de/hsel/itech/jsf/BookBean.java | 4 +-- src/main/java/de/hsel/itech/jsf/CartBean.java | 24 +++++++++------- .../java/de/hsel/itech/jsf/CategoryBean.java | 4 +-- src/main/webapp/bookinfo.xhtml | 10 +------ src/main/webapp/cart.xhtml | 28 +++++++++++++++++-- 5 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/main/java/de/hsel/itech/jsf/BookBean.java b/src/main/java/de/hsel/itech/jsf/BookBean.java index e135261..b087d8c 100644 --- a/src/main/java/de/hsel/itech/jsf/BookBean.java +++ b/src/main/java/de/hsel/itech/jsf/BookBean.java @@ -7,12 +7,12 @@ import lombok.Setter; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; -import javax.faces.bean.RequestScoped; +import javax.faces.bean.ViewScoped; import java.util.ArrayList; import java.util.List; @ManagedBean -@RequestScoped +@ViewScoped public class BookBean { @Getter diff --git a/src/main/java/de/hsel/itech/jsf/CartBean.java b/src/main/java/de/hsel/itech/jsf/CartBean.java index 6a38aa0..f278dfd 100644 --- a/src/main/java/de/hsel/itech/jsf/CartBean.java +++ b/src/main/java/de/hsel/itech/jsf/CartBean.java @@ -9,35 +9,35 @@ import lombok.Setter; import javax.faces.annotation.ManagedProperty; import javax.faces.bean.ManagedBean; -import javax.faces.bean.RequestScoped; +import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import java.util.ArrayList; import java.util.List; @ManagedBean -@RequestScoped +@SessionScoped public class CartBean { @Setter @ManagedProperty("#{userBean.user}") private User user; - @Setter @Getter - private long isbn; + private ArrayList bookList; public User getUser() { FacesContext context = FacesContext.getCurrentInstance(); return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class); } - public String add(){ - System.out.println("isbn: " + isbn); - if(Database.getInstance().book().exists(isbn)){ - Book book = Database.getInstance().book().get(isbn); + public String add(Book book){ + if(user != null){ Database.getInstance().shoppingCart().insert(new ShoppingCartItem(user,book,1)); }else{ - return "index.jsf"; + if(bookList == null){ + bookList = new ArrayList(); + } + bookList.add(book); } return "cart.jsf"; } @@ -55,7 +55,11 @@ public class CartBean { } public int getItemCount(){ - return getItems().size(); + int count = getItems().size(); + if(bookList != null){ + count += bookList.size(); + } + return count; } } diff --git a/src/main/java/de/hsel/itech/jsf/CategoryBean.java b/src/main/java/de/hsel/itech/jsf/CategoryBean.java index e61b49e..7a5d15b 100644 --- a/src/main/java/de/hsel/itech/jsf/CategoryBean.java +++ b/src/main/java/de/hsel/itech/jsf/CategoryBean.java @@ -7,12 +7,12 @@ import lombok.Getter; import lombok.Setter; import javax.faces.bean.ManagedBean; -import javax.faces.bean.ViewScoped; +import javax.faces.bean.SessionScoped; import java.io.Serializable; import java.util.List; @ManagedBean -@ViewScoped +@SessionScoped public class CategoryBean implements Serializable { @Getter diff --git a/src/main/webapp/bookinfo.xhtml b/src/main/webapp/bookinfo.xhtml index 971be90..d9acf2f 100644 --- a/src/main/webapp/bookinfo.xhtml +++ b/src/main/webapp/bookinfo.xhtml @@ -10,15 +10,9 @@ -
- - - - in den Warenkorb -
@@ -56,9 +50,7 @@
- - + in den Warenkorb diff --git a/src/main/webapp/cart.xhtml b/src/main/webapp/cart.xhtml index a8bc1ef..2105538 100644 --- a/src/main/webapp/cart.xhtml +++ b/src/main/webapp/cart.xhtml @@ -9,14 +9,38 @@ - #{cartBean.isbn} -
  • + +
    +
    + Buchcover +
    +
    +

    #{book.title}

    +
    +
    +
    +

    #{book.price} (in cent)€

    +
    +
    +
    + +
    +
    +
    +
    + Entfernen +
    +
    +
    +
    +
    +