+ Address pages

This commit is contained in:
Johannes Theiner 2019-06-04 17:43:05 +02:00
parent c8b6376284
commit 9f3b31113f
11 changed files with 189 additions and 50 deletions

View File

@ -161,6 +161,12 @@
<version>0.3m</version> <version>0.3m</version>
</dependency> </dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
</dependency>
<!--JUnit--> <!--JUnit-->
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>

View File

@ -5,10 +5,7 @@ import de.hsel.itech.db.pojo.User;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.sql.Connection; import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -34,17 +31,18 @@ public class AddressDB {
* @param user user to get addresses from * @param user user to get addresses from
* @return list of users addresses * @return list of users addresses
*/ */
@NotNull @Nullable
public List<Address> getAll(@NotNull User user) { public List<Address> getAll(User user) {
if(user == null) return null;
Map.Entry<ResultSet, Connection> entry = database.getResultSetsByValue(database.tableUserAddress, "user", String.valueOf(user.getId())); Map.Entry<ResultSet, Connection> entry = database.getResultSetsByValue(database.tableUserAddress, "user", String.valueOf(user.getId()));
assert entry != null; if(entry == null) return null;
List<Address> list = new ArrayList<>(); List<Address> list = new ArrayList<>();
try { try {
ResultSet rs = entry.getKey(); ResultSet rs = entry.getKey();
while(rs.next()) { while(rs.next()) {
list.add(new Address(user, rs.getLong("id"), rs.getString("name"), rs.getString("street"), rs.getString("number"), rs.getInt("zipCode"), rs.getString("city"))); list.add(get(rs.getLong("address"), user));
} }
entry.getValue().close(); entry.getValue().close();
@ -62,13 +60,13 @@ public class AddressDB {
* @return specific address * @return specific address
*/ */
@Nullable @Nullable
public Address get(long id) { public Address get(long id, User user) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableAddress, id); Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableAddress, id);
assert entry != null; assert entry != null;
try { try {
ResultSet rs = entry.getKey(); ResultSet rs = entry.getKey();
Address address = new Address(getUserFromId(id), id, rs.getString("name"), rs.getString("street"), rs.getString("number"), rs.getInt("zipCode"), rs.getString("city")); Address address = new Address(user, id, rs.getString("name"), rs.getString("street"), rs.getString("number"), rs.getInt("zipCode"), rs.getString("city"));
entry.getValue().close(); entry.getValue().close();
return address; return address;
@ -78,26 +76,6 @@ public class AddressDB {
return null; return null;
} }
/**
* get user from address id.
*
* @param id address id
* @return User
*/
@Nullable
private User getUserFromId(long id) {
Map.Entry<ResultSet, Connection> entry = database.getResultSetByValue(database.tableUserAddress, "address", id);
assert entry != null;
try {
User user = database.user().get(entry.getKey().getLong("user"));
entry.getValue().close();
return user;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/** /**
* insert address into database. * insert address into database.
@ -106,13 +84,12 @@ public class AddressDB {
* @return insert count * @return insert count
*/ */
public int insert(@NotNull Address address) { public int insert(@NotNull Address address) {
Connection connection = database.getConnection(); Connection connection = database.getConnection();
int insertCount = 0; int insertCount = 0;
try { try {
assert connection != null; assert connection != null;
PreparedStatement statement = connection.prepareStatement("INSERT INTO address (name, street, number, zipCode, city) VALUES (?, ?, ?, ?, ?)"); PreparedStatement statement = connection.prepareStatement("INSERT INTO address (name, street, number, zipCode, city) VALUES (?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
statement.setString(1, address.getName()); statement.setString(1, address.getName());
statement.setString(2, address.getStreet()); statement.setString(2, address.getStreet());
statement.setString(3, address.getNumber()); statement.setString(3, address.getNumber());
@ -121,11 +98,18 @@ public class AddressDB {
insertCount += statement.executeUpdate(); insertCount += statement.executeUpdate();
ResultSet keys = statement.getGeneratedKeys();
Long id = null;
while(keys.next()) {
id = keys.getLong("id");
}
if(id != null) {
PreparedStatement relationStatement = connection.prepareStatement("INSERT INTO user_address (user, address) VALUES (?, ?)"); PreparedStatement relationStatement = connection.prepareStatement("INSERT INTO user_address (user, address) VALUES (?, ?)");
relationStatement.setLong(1, address.getUser().getId()); relationStatement.setLong(1, address.getUser().getId());
relationStatement.setLong(2, address.getId()); relationStatement.setLong(2, id);
insertCount += statement.executeUpdate(); insertCount += relationStatement.executeUpdate();
}
connection.close(); connection.close();
} catch (SQLException e) { } catch (SQLException e) {

View File

@ -26,9 +26,10 @@ public class OrderDB {
ResultSet rs = entry.getKey(); ResultSet rs = entry.getKey();
List<OrderItem> items = getItems(id); List<OrderItem> items = getItems(id);
User user = database.user().get(rs.getLong("user"));
Payment payment = database.payment().get(rs.getLong("payment")); Payment payment = database.payment().get(rs.getLong("payment"));
Address address = database.address().get(rs.getLong("address")); Address address = database.address().get(rs.getLong("address"), user);
Order order = new Order(id, database.user().get(rs.getLong("user")), items, rs.getDate("date"), rs.getLong("price"), payment, address); Order order = new Order(id, user, items, rs.getDate("date"), rs.getLong("price"), payment, address);
entry.getValue().close(); entry.getValue().close();
return order; return order;

View File

@ -1,9 +1,6 @@
package de.hsel.itech.db.pojo; package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/** /**
* POJO for User. * POJO for User.
@ -14,6 +11,7 @@ import lombok.RequiredArgsConstructor;
*/ */
@Data @Data
@NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@RequiredArgsConstructor @RequiredArgsConstructor
public class User { public class User {

View File

@ -0,0 +1,78 @@
package de.hsel.itech.jsf;
import de.hsel.itech.db.Database;
import de.hsel.itech.db.pojo.Address;
import de.hsel.itech.db.pojo.User;
import lombok.Getter;
import lombok.Setter;
import javax.faces.annotation.ManagedProperty;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Johannes Theiner
* @version 1.0
*/
@ManagedBean
@RequestScoped
public class AddressBean {
@Setter
@ManagedProperty("#{userBean.user}")
private User user;
public User getUser() {
FacesContext context = FacesContext.getCurrentInstance();
return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class);
}
@Getter
@Setter
private String name;
@Getter
@Setter
private String street;
@Getter
@Setter
private String number;
@Getter
@Setter
private int zipCode = -1;
@Getter
@Setter
private String city;
@Getter
private boolean valid = true;
public List<Address> getAddressList() {
List<Address> list = Database.getInstance().address().getAll(getUser());
if(list == null) return new ArrayList<>();
return list;
}
public String add() {
if(getUser() != null && name != null && street != null && number != null && zipCode != -1 && city != null) {
Database.getInstance().address().insert(new Address(getUser(), name, street, number, zipCode, city));
name = null;
number = null;
zipCode = -1;
city = null;
valid = true;
return "address.xhtml";
}
valid = false;
return "";
}
}

View File

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

View File

@ -13,7 +13,8 @@ import javax.faces.bean.SessionScoped;
public class UserBean { public class UserBean {
@Getter @Getter
private User user; @Setter
private User user = new User();
@Getter @Getter
@Setter @Setter
@ -45,7 +46,7 @@ public class UserBean {
if (db.user().verify(user, password)) { if (db.user().verify(user, password)) {
loggedIn = true; loggedIn = true;
this.user = user; setUser(user);
password = null; password = null;
username = null; username = null;
return "index.xhtml"; return "index.xhtml";

View File

@ -0,0 +1,33 @@
<?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">
<div class="m-col-s-4 m-push-s-4">
<h:form>
<h:inputText id="name" value="#{addressBean.name}">Name</h:inputText>
<br/>
<h:inputText id="street" value="#{addressBean.street}">Straße</h:inputText>
<br/>
<h:inputText id="number" value="#{addressBean.number}">Hausnummer</h:inputText>
<br/>
<h:inputText id="zipCode" value="#{addressBean.zipCode}">Postleitzahl</h:inputText>
<br/>
<h:inputText id="city" value="#{addressBean.city}">Ort</h:inputText>
<br/>
<h:commandButton action="#{addressBean.add}" value="hinzufügen"/>
</h:form>
</div>
</div>
</ui:define>
</ui:composition>
</f:view>
</html>

View File

@ -0,0 +1,37 @@
<?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">
<h1 class="m-text-center">Adressen</h1>
<h:panelGroup layout="block" styleClass="m-row" rendered="#{userBean.loggedIn}">
<ui:repeat value="#{addressBean.addressList}" var="address">
<div class="m-col-t-3">
<div class="m-block m-primary">
#{address.name}<br/>
#{address.street} #{address.number}<br/>
#{address.zipCode} #{address.city}
</div>
</div>
</ui:repeat>
<div class="m-col-t-3">
<h:outputLink value="address-add.jsf">
<div class="m-block m-text-center">
<i class="fas fa-plus fa-4x"/>
</div>
</h:outputLink>
</div>
</h:panelGroup>
</ui:define>
</ui:composition>
</f:view>
</html>

View File

@ -10,7 +10,7 @@
<ui:define name="content"> <ui:define name="content">
<h:panelGroup layout="block" styleClass="row" rendered="#{userBean.error}"> <h:panelGroup layout="block" styleClass="m-row" rendered="#{userBean.error}">
<aside class="m-col-s-3 m-push-l-4 m-note m-danger"> <aside class="m-col-s-3 m-push-l-4 m-note m-danger">
<h3>Passwort oder Benutzername falsch</h3> <h3>Passwort oder Benutzername falsch</h3>
</aside> </aside>

View File

@ -37,7 +37,7 @@
<div id="m-navbar-collapse" class="m-col-t-12 m-show-m m-col-m-none m-right-m"> <div id="m-navbar-collapse" class="m-col-t-12 m-show-m m-col-m-none m-right-m">
<div class="m-row"> <div class="m-row">
<ol class="m-col-t-6 m-col-m-none"> <ol class="m-col-t-6 m-col-m-none">
<li><a href="category.xhtml">Katalog</a></li> <li><a href="category.jsf">Katalog</a></li>
<li><a href="#">(1) Warenkorb</a></li> <li><a href="#">(1) Warenkorb</a></li>
</ol> </ol>
<h:panelGroup layout="block" rendered="#{userBean.loggedIn}"> <h:panelGroup layout="block" rendered="#{userBean.loggedIn}">
@ -45,9 +45,10 @@
<li> <li>
<a href="#">#{userBean.user.name}</a> <a href="#">#{userBean.user.name}</a>
<ol> <ol>
<li><a href="orders.xhtml">Meine Bestellungen</a></li> <li><a href="orders.jsf">Meine Bestellungen</a></li>
<li><a href="">Adressen und Konten</a></li> <li><a href="address.jsf">Adressen</a></li>
<li><a href="logout.xhtml">Abmelden</a></li> <li><a href="payments.jsf">Zahlungsmöglichkeiten</a></li>
<li><a href="logout.jsf">Abmelden</a></li>
</ol> </ol>
</li> </li>
</ol> </ol>