+working shopping-cart

This commit is contained in:
Julian Hinxlage 2019-06-05 13:11:36 +02:00
parent 1a115e00a9
commit d1a45681a2
4 changed files with 71 additions and 29 deletions

View File

@ -36,6 +36,15 @@ public class Book {
@NonNull private String description;
@NonNull private String image;
public String getPriceString() {
String res = "";
res += getPrice() / 100;
res += ",";
if (getPrice() % 100 < 10) {
res += "0";
}
res += getPrice() % 100;
return res;
}
}

View File

@ -4,7 +4,6 @@ import de.hsel.itech.db.Database;
import de.hsel.itech.db.pojo.Book;
import de.hsel.itech.db.pojo.ShoppingCartItem;
import de.hsel.itech.db.pojo.User;
import lombok.Getter;
import lombok.Setter;
import javax.faces.annotation.ManagedProperty;
@ -22,32 +21,40 @@ public class CartBean {
@ManagedProperty("#{userBean.user}")
private User user;
@Getter
private ArrayList<Book> bookList;
public User getUser() {
FacesContext context = FacesContext.getCurrentInstance();
return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class);
}
public String add(Book book){
if(user != null){
Database.getInstance().shoppingCart().insert(new ShoppingCartItem(user,book,1));
}else{
if(bookList == null){
bookList = new ArrayList<Book>();
if(getUser() != null){
for(ShoppingCartItem item : getItems()){
if(item.getArticle().getId() == book.getId()){
Database.getInstance().shoppingCart().delete(item);
item.setCount(item.getCount() + 1);
Database.getInstance().shoppingCart().insert(item);
return "cart.jsf";
}
}
bookList.add(book);
Database.getInstance().shoppingCart().insert(new ShoppingCartItem(getUser(),book,1));
}
return "cart.jsf";
}
public String remove(ShoppingCartItem item){
if(getUser() != null){
Database.getInstance().shoppingCart().delete(item);
}
return "cart.jsf";
}
public List<ShoppingCartItem> getItems(){
Database db = Database.getInstance();
if(user==null){
if(getUser()!=null){
return db.shoppingCart().get(getUser());
}else{
return new ArrayList<ShoppingCartItem>();
}
return db.shoppingCart().get(user);
}
public void setItems(List<ShoppingCartItem> list) {
@ -55,11 +62,22 @@ public class CartBean {
}
public int getItemCount(){
int count = getItems().size();
if(bookList != null){
count += bookList.size();
return getItems().size();
}
public String getSum(){
int sum = 0;
for(ShoppingCartItem item : getItems()){
sum += item.getCount() * item.getArticle().getPrice();
}
return count;
String res = "";
res += sum / 100;
res += ",";
if (sum % 100 < 10) {
res += "0";
}
res += sum % 100;
return res;
}
}

View File

@ -19,6 +19,6 @@ public class SettingsBean {
@Getter
@Setter
private boolean darkTheme = false;
private boolean darkTheme = true;
}

View File

@ -8,41 +8,56 @@
<f:view>
<ui:composition template="template.xhtml">
<ui:define name="content">
<div class="m-row">
<h1 class="m-text-center">Warenkorb</h1>
</div>
<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"/>
<img src="https://source.unsplash.com/#{item.article.image}/100x100" class="m-image" alt="Buchcover"/>
</div>
<div class="m-col-t-4">
<h3>#{book.title}</h3>
<h3>#{item.article.title}</h3>
</div>
<div class="m-col-t-4">
<div class="m-row">
<h4 class="m-text-right">#{book.price} (in cent)&euro;</h4>
<h4 class="m-text-right">#{item.article.priceString}&#8364;</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"/>
<input type="number" name="quantity" value="#{item.count}" 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>
<h:form>
<h:commandLink action="#{cartBean.remove(item)}">
Entfernen
</h:commandLink>
</h:form>
</div>
</div>
</div>
</div>
</ui:repeat>
<h:panelGroup type="block" styleClass="m-row" rendered="#{cartBean.itemCount != 0}">
<div class="m-col-t-5 m-push-t-7">
<h1 class="m-text-right">Summe(#{cartBean.itemCount} Items): #{cartBean.sum}&#8364;</h1>
</div>
</h:panelGroup>
<h:panelGroup type="block" styleClass="m-row" rendered="#{cartBean.itemCount == 0}">
<h1 class="m-text-center">Der Warenkorb ist Leer</h1>
</h:panelGroup>
<div class="m-row">
<div class="m-col-t-3 m-push-t-5 m-button m-success">
<div class="m-col-t-4 m-push-t-4 m-button m-success">
<a href="#">Jetzt Kaufen</a>
</div>
</div>