+shopping cart

This commit is contained in:
Julian Hinxlage 2019-06-04 23:39:40 +02:00
parent 7412c45eca
commit 1a115e00a9
5 changed files with 45 additions and 25 deletions

View File

@ -7,12 +7,12 @@ import lombok.Setter;
import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty; import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped; import javax.faces.bean.ViewScoped;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ManagedBean @ManagedBean
@RequestScoped @ViewScoped
public class BookBean { public class BookBean {
@Getter @Getter

View File

@ -9,35 +9,35 @@ import lombok.Setter;
import javax.faces.annotation.ManagedProperty; import javax.faces.annotation.ManagedProperty;
import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped; import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ManagedBean @ManagedBean
@RequestScoped @SessionScoped
public class CartBean { public class CartBean {
@Setter @Setter
@ManagedProperty("#{userBean.user}") @ManagedProperty("#{userBean.user}")
private User user; private User user;
@Setter
@Getter @Getter
private long isbn; private ArrayList<Book> bookList;
public User getUser() { public User getUser() {
FacesContext context = FacesContext.getCurrentInstance(); FacesContext context = FacesContext.getCurrentInstance();
return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class); return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class);
} }
public String add(){ public String add(Book book){
System.out.println("isbn: " + isbn); if(user != null){
if(Database.getInstance().book().exists(isbn)){
Book book = Database.getInstance().book().get(isbn);
Database.getInstance().shoppingCart().insert(new ShoppingCartItem(user,book,1)); Database.getInstance().shoppingCart().insert(new ShoppingCartItem(user,book,1));
}else{ }else{
return "index.jsf"; if(bookList == null){
bookList = new ArrayList<Book>();
}
bookList.add(book);
} }
return "cart.jsf"; return "cart.jsf";
} }
@ -55,7 +55,11 @@ public class CartBean {
} }
public int getItemCount(){ public int getItemCount(){
return getItems().size(); int count = getItems().size();
if(bookList != null){
count += bookList.size();
}
return count;
} }
} }

View File

@ -7,12 +7,12 @@ import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped; import javax.faces.bean.SessionScoped;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
@ManagedBean @ManagedBean
@ViewScoped @SessionScoped
public class CategoryBean implements Serializable { public class CategoryBean implements Serializable {
@Getter @Getter

View File

@ -10,15 +10,9 @@
<ui:composition template="template.xhtml"> <ui:composition template="template.xhtml">
<ui:define name="content"> <ui:define name="content">
<h:form> <h:form>
<h:commandLink action="#{bookBean.back}" value="zurück zur Auswahl" rendered="#{not empty bookBean}"/> <h:commandLink action="#{bookBean.back}" value="zurück zur Auswahl" rendered="#{not empty bookBean}"/>
<br/> <br/>
<h:commandLink action="#{cartBean.add}">
<f:setPropertyActionListener target="#{cartBean.isbn}" value="#{bookBean.book.isbn}"/>
<i class="fas fa-cart-plus"/>
in den Warenkorb
</h:commandLink>
</h:form> </h:form>
<h:panelGroup layout="block" rendered="#{empty bookBean.book}"> <h:panelGroup layout="block" rendered="#{empty bookBean.book}">
@ -56,9 +50,7 @@
<br/> <br/>
<div class="m-button m-success"> <div class="m-button m-success">
<h:form> <h:form>
<h:commandLink action="#{cartBean.add}"> <h:commandLink action="#{cartBean.add(bookBean.book)}">
<f:setPropertyActionListener target="#{cartBean.isbn}"
value="#{bookBean.book.isbn}"/>
<i class="fas fa-cart-plus"/> <i class="fas fa-cart-plus"/>
in den Warenkorb in den Warenkorb
</h:commandLink> </h:commandLink>

View File

@ -9,14 +9,38 @@
<ui:composition template="template.xhtml"> <ui:composition template="template.xhtml">
<ui:define name="content"> <ui:define name="content">
#{cartBean.isbn}
<ui:repeat value="#{cartBean.items}" var="item"> <ui:repeat value="#{cartBean.items}" var="item">
<li> <li>
<h:outputLabel value="#{item.article.title}"/> <h:outputLabel value="#{item.article.title}"/>
</li> </li>
</ui:repeat> </ui:repeat>
<ui:repeat value="#{cartBean.bookList}" var="book">
<div class="m-row m-block">
<div class="m-col-t-4">
<img src="https://source.unsplash.com/#{book.image}/100x100" class="m-image" alt="Buchcover"/>
</div>
<div class="m-col-t-4">
<h3>#{book.title}</h3>
</div>
<div class="m-col-t-4">
<div class="m-row">
<h4 class="m-text-right">#{book.price} (in cent)&euro;</h4>
</div>
<div class="m-row">
<div class="m-col-t-4 m-push-t-6">
<input type="number" name="quantity" value="1" min="1" max="10"/>
</div>
</div>
<div class="m-row">
<div class="m-col-t-4 m-push-t-6 m-button m-small m-danger">
<a href="#">Entfernen</a>
</div>
</div>
</div>
</div>
</ui:repeat>
<div class="m-row"> <div class="m-row">
<div class="m-col-t-3 m-push-t-5 m-button m-success"> <div class="m-col-t-3 m-push-t-5 m-button m-success">
<a href="#">Jetzt Kaufen</a> <a href="#">Jetzt Kaufen</a>