+shopping cart
This commit is contained in:
parent
7412c45eca
commit
1a115e00a9
|
@ -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
|
||||
|
|
|
@ -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<Book> 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<Book>();
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -10,15 +10,9 @@
|
|||
<ui:composition template="template.xhtml">
|
||||
|
||||
<ui:define name="content">
|
||||
|
||||
<h:form>
|
||||
<h:commandLink action="#{bookBean.back}" value="zurück zur Auswahl" rendered="#{not empty bookBean}"/>
|
||||
<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:panelGroup layout="block" rendered="#{empty bookBean.book}">
|
||||
|
@ -56,9 +50,7 @@
|
|||
<br/>
|
||||
<div class="m-button m-success">
|
||||
<h:form>
|
||||
<h:commandLink action="#{cartBean.add}">
|
||||
<f:setPropertyActionListener target="#{cartBean.isbn}"
|
||||
value="#{bookBean.book.isbn}"/>
|
||||
<h:commandLink action="#{cartBean.add(bookBean.book)}">
|
||||
<i class="fas fa-cart-plus"/>
|
||||
in den Warenkorb
|
||||
</h:commandLink>
|
||||
|
|
|
@ -9,14 +9,38 @@
|
|||
<ui:composition template="template.xhtml">
|
||||
<ui:define name="content">
|
||||
|
||||
#{cartBean.isbn}
|
||||
|
||||
<ui:repeat value="#{cartBean.items}" var="item">
|
||||
<li>
|
||||
<h:outputLabel value="#{item.article.title}"/>
|
||||
</li>
|
||||
</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)€</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-col-t-3 m-push-t-5 m-button m-success">
|
||||
<a href="#">Jetzt Kaufen</a>
|
||||
|
|
Loading…
Reference in New Issue