+ more validation

+ working dark theme
- some bugs

Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
Johannes Theiner 2019-06-07 08:14:24 +02:00
parent 5bc0162cf2
commit 48724a5853
9 changed files with 44 additions and 21 deletions

View File

@ -77,7 +77,7 @@ public class Database {
private Database() { private Database() {
Configuration config = Configuration.get(); Configuration config = Configuration.get();
dataSource = new MariaDbPoolDataSource("jdbc:mysql://" + config.getDatabase().getHostname() dataSource = new MariaDbPoolDataSource("jdbc:mysql://" + config.getDatabase().getHostname()
+ ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase() + "?useUnicode=true&characterEncoding=UTF-8"); + ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase() + "?useUnicode=true&characterEncoding=UTF-8&maxPoolSize=10&pool");
try { try {
dataSource.setUser(config.getDatabase().getUsername()); dataSource.setUser(config.getDatabase().getUsername());
dataSource.setPassword(config.getDatabase().getPassword()); dataSource.setPassword(config.getDatabase().getPassword());

View File

@ -6,6 +6,7 @@ import lombok.Setter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import javax.faces.annotation.ManagedProperty; import javax.faces.annotation.ManagedProperty;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped; import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
@ -45,31 +46,44 @@ public class CartBean {
if (getUser() != null && getUser().getId() != 0) { if (getUser() != null && getUser().getId() != 0) {
for (ShoppingCartItem item : getItems()) { for (ShoppingCartItem item : getItems()) {
if (item.getArticle().getId() == book.getId()) { if (item.getArticle().getId() == book.getId()) {
if(item.getCount() >= 127) {
items = getFreshItems();
FacesContext.getCurrentInstance().addMessage("count:count", new FacesMessage("Es können maximal 127 eines Artikels auf einmal gekauft werden."));
return "cart.jsf";
}
Database.getInstance().shoppingCart().delete(item); Database.getInstance().shoppingCart().delete(item);
item.setCount(item.getCount() + 1); item.setCount(item.getCount() + 1);
Database.getInstance().shoppingCart().insert(item); Database.getInstance().shoppingCart().insert(item);
items = getFreshItems();
return "cart.jsf"; return "cart.jsf";
} }
} }
Database.getInstance().shoppingCart().insert(new ShoppingCartItem(getUser(), book, 1)); Database.getInstance().shoppingCart().insert(new ShoppingCartItem(getUser(), book, 1));
} }
items = getFreshItems();
return "cart.jsf"; return "cart.jsf";
} }
public String remove(ShoppingCartItem item) { public String remove(ShoppingCartItem item) {
if (getUser() != null) { if (getUser() != null) {
Database.getInstance().shoppingCart().delete(item); Database.getInstance().shoppingCart().delete(item);
items = getFreshItems();
} }
return "cart.jsf"; return "cart.jsf";
} }
public List<ShoppingCartItem> getItems() { public List<ShoppingCartItem> getItems() {
if(items != null) return items; if(items == null)
items = getFreshItems();
return items;
}
private List<ShoppingCartItem> getFreshItems() {
Database db = Database.getInstance(); Database db = Database.getInstance();
if (getUser() != null) { if (getUser() != null) {
items = db.shoppingCart().get(getUser()); items = db.shoppingCart().get(getUser());
} else { } else {
items = new ArrayList<ShoppingCartItem>(); items = new ArrayList<>();
} }
return items; return items;
} }
@ -88,18 +102,23 @@ public class CartBean {
public String buy() { public String buy() {
ArrayList<OrderItem> orderList = new ArrayList<>(); ArrayList<OrderItem> orderList = new ArrayList<>();
for (ShoppingCartItem item : getItems()) { for (ShoppingCartItem item : getFreshItems()) {
orderList.add(new OrderItem(item.getArticle(), item.getCount())); orderList.add(new OrderItem(item.getArticle(), item.getCount()));
} }
Database.getInstance().order().insert(new Order(getUser(), orderList, new Date(System.currentTimeMillis()), sum(), getPayment(), getAddress())); Database.getInstance().order().insert(new Order(getUser(), orderList, new Date(System.currentTimeMillis()), sum(), getPayment(), getAddress()));
for (ShoppingCartItem item : getItems()) { for (ShoppingCartItem item : getFreshItems()) {
Database.getInstance().shoppingCart().delete(item); Database.getInstance().shoppingCart().delete(item);
} }
items = getFreshItems();
return "thanks.jsf"; return "thanks.jsf";
} }
public int getItemCount() { public int getItemCount() {
return getItems().size(); int count = 0;
for(ShoppingCartItem item : getItems()) {
count += item.getCount();
}
return count;
} }
public long sum() { public long sum() {

View File

@ -10,6 +10,7 @@ import lombok.Setter;
import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped; import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import javax.validation.constraints.Future;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
@ -38,6 +39,7 @@ public class CreditCardBean implements Serializable {
@Getter @Getter
@Setter @Setter
@Future
private Date expiration; private Date expiration;
@Getter @Getter

View File

@ -9,6 +9,7 @@ import lombok.Setter;
import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped; import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import javax.validation.constraints.Pattern;
import java.io.Serializable; import java.io.Serializable;
/** /**
@ -27,6 +28,7 @@ public class PayPalBean implements Serializable {
@Getter @Getter
@Setter @Setter
@Pattern(regexp = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")
private String mail; private String mail;
@Getter @Getter

View File

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

View File

@ -64,7 +64,8 @@ public class UserBean implements Serializable {
public String logout() { public String logout() {
user = null; user = null;
loggedIn = false; loggedIn = false;
return "index.html"; FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "/index.xhtml?faces-redirect=true";
} }
public String register() { public String register() {

View File

@ -38,9 +38,10 @@
</div> </div>
<div class="m-row"> <div class="m-row">
<div class="m-col-t-4 m-push-t-6"> <div class="m-col-t-4 m-push-t-6">
<h:form> <h:form id="count">
<h:inputText value="#{item.count}" valueChangeListener="#{cartBean.change}"> <h:inputText id="count" value="#{item.count}" valueChangeListener="#{cartBean.change}">
<f:validateLength minimum="1" maximum="3"/> <f:validateLength minimum="1" maximum="3"/>
<f:validateLongRange minimum="1" maximum="127"/>
<f:ajax /> <f:ajax />
</h:inputText> </h:inputText>
</h:form> </h:form>
@ -68,10 +69,14 @@
</div> </div>
</h:panelGroup> </h:panelGroup>
<h:panelGroup layout="block" styleClass="m-row" rendered="#{cartBean.itemCount == 0}"> <h:panelGroup layout="block" styleClass="m-row" rendered="#{not userBean.loggedIn}">
<h1 class="m-text-center">Bitte melden Sie sich an um Bücher in ihrem Warenkorb hinzuzufügen.</h1> <h1 class="m-text-center">Bitte melden Sie sich an um Bücher in ihrem Warenkorb hinzuzufügen.</h1>
</h:panelGroup> </h:panelGroup>
<h:panelGroup layout="block" styleClass="m-row" rendered="#{cartBean.itemCount == 0}">
<h1 class="m-text-center">Ihr Warenkorb ist leer.</h1>
</h:panelGroup>
<h:panelGroup layout="block" styleClass="m-row" rendered="#{cartBean.itemCount != 0}"> <h:panelGroup layout="block" styleClass="m-row" rendered="#{cartBean.itemCount != 0}">
<div class="m-col-t-4 m-push-t-4 m-button m-success"> <div class="m-col-t-4 m-push-t-4 m-button m-success">

View File

@ -27,7 +27,7 @@
<ul> <ul>
<ui:repeat value="#{order.items}" var="item"> <ui:repeat value="#{order.items}" var="item">
<li> <li>
#{item.book.title} - #{item.book.authorsString} #{item.count} x #{item.book.title} - #{item.book.authorsString}
</li> </li>
</ui:repeat> </ui:repeat>
</ul> </ul>

View File

@ -73,15 +73,9 @@
<li>Dark Theme <li>Dark Theme
<h:form> <h:form>
<div class="onoffswitch"> <div class="onoffswitch">
<h:selectBooleanCheckbox title="onoffswitch" onchange="changeTheme()" value="#{settingsBean.darkTheme}">
<h:selectBooleanCheckbox styleClass="onoffswitch-checkbox" <f:ajax/>
id="myonoffswitch" </h:selectBooleanCheckbox>
title="onoffswitch" onchange="changeTheme()"
value="#{settingsBean.darkTheme}"/>
<h:outputLabel styleClass="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"/>
<span class="onoffswitch-switch"/>
</h:outputLabel>
</div> </div>
</h:form> </h:form>
</li> </li>