Merge remote-tracking branch 'origin/julian5' into johannes5

# Conflicts:
#	src/main/webapp/template.xhtml
This commit is contained in:
Johannes Theiner 2019-06-05 18:02:12 +02:00
commit 88145df721
11 changed files with 289 additions and 13 deletions

View File

@ -180,6 +180,13 @@
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<!-- other -->
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency>
</dependencies>

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

@ -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

View File

@ -0,0 +1,102 @@
package de.hsel.itech.jsf;
import de.hsel.itech.db.Database;
import de.hsel.itech.db.pojo.Book;
import de.hsel.itech.db.pojo.OrderItem;
import de.hsel.itech.db.pojo.ShoppingCartItem;
import de.hsel.itech.db.pojo.User;
import lombok.Setter;
import javax.faces.annotation.ManagedProperty;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.List;
@ManagedBean
@SessionScoped
public class CartBean {
@Setter
@ManagedProperty("#{userBean.user}")
private User user;
public User getUser() {
FacesContext context = FacesContext.getCurrentInstance();
return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class);
}
public String add(Book 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";
}
}
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(getUser()!=null){
return db.shoppingCart().get(getUser());
}else{
return new ArrayList<ShoppingCartItem>();
}
}
/*
public void setItems(List<ShoppingCartItem> list) {
}*/
public String buy(){
ArrayList<OrderItem> orderList = new ArrayList<>();
for(ShoppingCartItem item : getItems()){
orderList.add(new OrderItem(item.getArticle(),item.getCount()));
}
//Database.getInstance().order().insert(new Order(getUser(),orderList, new Date(234235235),sum(),null,null));
for(ShoppingCartItem item : getItems()) {
Database.getInstance().shoppingCart().delete(item);
}
return "thanks.jsp";
}
public int getItemCount(){
return getItems().size();
}
public long sum(){
int sum = 0;
for(ShoppingCartItem item : getItems()){
sum += item.getCount() * item.getArticle().getPrice();
}
return sum;
}
public String getSum(){
long sum = sum();
String res = "";
res += sum / 100;
res += ",";
if (sum % 100 < 10) {
res += "0";
}
res += sum % 100;
return res;
}
}

View File

@ -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

View File

@ -35,7 +35,6 @@ public class UserBean implements Serializable {
@Getter
private boolean error = false;
public String login() {
Database db = Database.getInstance();
User user = db.user().get(username);
@ -58,6 +57,7 @@ public class UserBean implements Serializable {
public String logout() {
user = null;
loggedIn = false;
return "index.html";
}

View File

@ -10,9 +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:form>
<h:panelGroup layout="block" rendered="#{empty bookBean.book}">
@ -49,10 +49,12 @@
<br/>
<br/>
<div class="m-button m-success">
<a href="#">
<h:form>
<h:commandLink action="#{cartBean.add(bookBean.book)}">
<i class="fas fa-cart-plus"/>
in den Warenkorb
</a>
</h:commandLink>
</h:form>
</div>
</div>
</div>

View File

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<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">
<div class="m-row m-block">
<div class="m-col-t-4">
<img src="https://source.unsplash.com/#{item.article.image}/100x100" class="m-image" alt="Buchcover"/>
</div>
<div class="m-col-t-4">
<h3>#{item.article.title}</h3>
</div>
<div class="m-col-t-4">
<div class="m-row">
<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="#{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">
<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-4 m-push-t-4 m-button m-success">
<a href="confirmation.jsf">Jetzt Kaufen</a>
</div>
</div>
</ui:define>
</ui:composition>
</f:view>
</html>

View File

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view>
<ui:composition template="template.xhtml">
<ui:define name="content">
<div class="m-row">
<h1 class="m-text-center">Bestellbestätigung</h1>
</div>
<div class="m-row">
<div class="m-col-t-3 m-block">
<h4>Addresse:</h4>
Max Mustermann<br/>
Musterstraße 42<br/>
12345 Musterstadt
</div>
<div class="m-col-t-4 m-block m-push-t-5">
<h4>Konto</h4>
IBAN: 12345&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;111<br/>
Inhaber: Max Mustermann<br/>
</div>
</div>
<ui:repeat value="#{cartBean.items}" var="item">
<div class="m-row m-block">
<div class="m-col-t-4">
<img src="https://source.unsplash.com/#{item.article.image}/100x100" class="m-image" alt="Buchcover"/>
</div>
<div class="m-col-t-4">
<h3>#{item.article.title}</h3>
</div>
<div class="m-col-t-4">
<h4 class="m-text-right">#{item.count} x #{item.article.priceString}&#8364;</h4>
</div>
</div>
</ui:repeat>
<h:panelGroup type="block" styleClass="m-row">
<div class="m-col-t-5 m-push-t-7">
<h1 class="m-text-right">Summe: #{cartBean.sum}&#8364;</h1>
</div>
</h:panelGroup>
<div class="m-row">
<div class="m-col-t-3 m-push-t-3 m-button m-danger">
<a href="cart.jsf">Abbrechen</a>
</div>
<div class="m-col-t-3 m-push-t-3 m-button m-success">
<h:form>
<h:commandLink action="#{cartBean.buy}">Bestätigen</h:commandLink>
</h:form>
</div>
</div>
</ui:define>
</ui:composition>
</f:view>
</html>

View File

@ -37,8 +37,8 @@
<div id="m-navbar-collapse" class="m-col-t-12 m-show-m m-col-m-none m-right-m">
<div class="m-row">
<ol class="m-col-t-6 m-col-m-none">
<li><a href="category.jsf">Katalog</a></li>
<li><a href="#">(1) Warenkorb</a></li>
<li><a href="category.xhtml">Katalog</a></li>
<li><a href="cart.jsf">(#{cartBean.itemCount}) Warenkorb</a></li>
</ol>
<h:panelGroup layout="block" rendered="#{userBean.loggedIn}">
<ol class="m-col-t-6 m-col-m-none" start="4">

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view>
<ui:composition template="template.xhtml">
<ui:define name="content">
<div class="m-container">
<div class="m-row">
<h2 class="m-col-t-5 m-push-t-4">
Vielen Dank für Ihren Einkauf!
</h2>
</div>
</div>
</ui:define>
</ui:composition>
</f:view>
</html>