Add customer overview prototype

This commit is contained in:
Eren Asker 2023-11-10 17:10:24 +01:00 committed by Simon Bruder
parent 135f9b9e87
commit de9c20fd09
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
8 changed files with 321 additions and 2 deletions

View file

@ -0,0 +1,40 @@
package catering.customer;
public class Customer {
private int id;
private String surname;
private String name;
public Customer(String name, String surname) {
this.id = (int) (Math.random() * 10 * Math.random() * 10 + Math.random() * 10);
this.name = name;
this.surname = surname;
}
public int getId() {
return id;
}
public String getSurname() {
return surname;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void setName(String name) {
this.name = name;
}
}

View file

@ -3,17 +3,22 @@ package catering.customer;
import org.salespointframework.useraccount.Password;
import org.salespointframework.useraccount.UserAccountManagement;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class CustomerController {
private final CustomerRepository customerRepository;
private UserAccountManagement userAccountManagement;
UserAccountManagement userAccountManagement;
CustomerController(UserAccountManagement userAccountManagement) {
CustomerController(UserAccountManagement userAccountManagement, CustomerRepository customerRepository) {
this.userAccountManagement = userAccountManagement;
this.customerRepository = customerRepository;
}
@GetMapping("/register")
@ -32,4 +37,29 @@ public class CustomerController {
userAccountManagement.create(username, Password.UnencryptedPassword.of(password));
return "redirect:/";
}
@GetMapping("/customer")
public String getCustomer(Model model) {
model.addAttribute("title", "Kundenverwaltung");
model.addAttribute("customers", customerRepository.getCustomers());
return "customer";
}
@PostMapping("/customer/remove")
public String removeCustomer(@RequestParam("customerID") int customerID, Model model) {
customerRepository.removeCustomer(customerID);
return "redirect:/customer";
}
@GetMapping("/customer/edit/{id}")
public String editCustomer(@PathVariable("id") int id, Model model) {
customerRepository.findById(id).ifPresent(customer -> model.addAttribute("customer", customer));
return "edit-customer";
}
@PostMapping("/customer/update")
public String updateCustomer(@ModelAttribute Customer customer) {
customerRepository.save(customer);
return "redirect:/customer";
}
}

View file

@ -0,0 +1,35 @@
package catering.customer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import jakarta.annotation.PostConstruct;
@Component
public class CustomerDataInitializer {
private static final Logger LOG = LoggerFactory.getLogger(CustomerDataInitializer.class);
private final CustomerRepository customerRepository;
public CustomerDataInitializer(CustomerRepository customerRepository) {
Assert.notNull(customerRepository, "CustomerRepository must not be null!");
this.customerRepository = customerRepository;
}
@PostConstruct
public void initialize() {
LOG.info("Creating default customer.");
customerRepository.save(new Customer("Hans", "Essen"));
customerRepository.save(new Customer("Angela", "Cook"));
LOG.info("Default customer created");
}
}

View file

@ -0,0 +1,53 @@
package catering.customer;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
@Component
public class CustomerRepository {
private Set<Customer> customers = new HashSet<>();
public CustomerRepository() {
}
public boolean addCustomer(Customer customer) {
return this.customers.add(customer);
}
private int nextId = (int) (Math.random() * 10 * Math.random() * 10 + Math.random() * 10);
public void save(Customer customer) {
if (customer.getId() == 0) {
customer.setId(nextId++);
} else {
this.customers.removeIf(p -> p.getId() == customer.getId());
}
this.customers.add(customer);
}
public long count() {
return this.customers.size();
}
public boolean removeCustomer(int customerID) {
return this.customers.removeIf(customer -> customer.getId() == customerID);
}
public Collection<Customer> getCustomers() {
return new ArrayList<>(this.customers);
}
public Optional<Customer> findById(int id) {
return this.customers.stream().filter(customer -> customer.getId() == id).findFirst();
}
}

View file

@ -0,0 +1,21 @@
body {
font: 13px/22px Helvetica, Arial, sans-serif;
background: #f0f0f0;
}
h2 {
text-decoration: underline;
}
.button {
cursor: pointer;
font-size: 13px;
text-decoration: none;
background-color: #EEEEEE;
color: #333333;
padding: 2px 6px 2px 6px;
border-top: 1px solid #333333;
border-right: 1px solid #333333;
border-bottom: 1px solid #333333;
border-left: 1px solid #333333;
}

View file

@ -0,0 +1,64 @@
/* CSS RESET */
body {
margin: 0 auto;
width: 940px;
font: 13px/22px Helvetica, Arial, sans-serif;
background: #f0f0f0;
}
h1 {
font-size: 3em;
line-height: 3.2em;
text-align:center;
}
table {
border-collapse: collapse;
width: 95%;
margin-top: 20px;
}
th,
td {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #f2f2f2;
text-decoration: underline;
font-size: 19px;
}
tr{
font-size: 17px;
}
td:last-child,
td:nth-child(5) {
width: 50px;
text-align: center;
}
tr:nth-child(even) {
background-color: rgb(214, 236, 216);
}
button {
cursor: pointer;
font-size: 13px;
margin-left: 4px;
margin-right: 4px;
}
.button {
cursor: pointer;
font-size: 13px;
text-decoration: none;
background-color: #EEEEEE;
color: #333333;
padding: 2px 6px 2px 6px;
border-top: 1px solid #333333;
border-right: 1px solid #333333;
border-bottom: 1px solid #333333;
border-left: 1px solid #333333;
}

View file

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" th:href="@{/resources/css/customer/style.css}" />
<title th:text="${title}"></title>
</head>
<body>
<div class="verwaltung">
<h1>Kundenverwaltung</h1>
<h2>Kundenübersicht</h2>
<table>
<tr>
<th>ID</th>
<th>Nachname</th>
<th>Vorname</th>
<th>Aufträge</th>
</tr>
<tr th:each="customer : ${customers}">
<td th:text="${customer.id}">01</td>
<td th:text="${customer.surname}">Musterkoch</td>
<td th:text="${customer.name}">Max</td>
<td><a>Aufträge</a></td>
<td>
<a th:href="@{'/customer/edit/' + ${customer.id}}" class="button"
>Bearbeiten</a
>
</td>
<td>
<form th:action="@{/customer/remove}" method="post">
<input
type="hidden"
th:name="customerID"
th:value="${customer.id}"
/>
<button type="submit" class="delete-icon">Entfernen</button>
</form>
</td>
</tr>
</table>
</div>
</body>
</html>

View file

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Kunde Bearbeiten</title>
<link
rel="stylesheet"
th:href="@{/resources/css/customer/edit-style.css}"
/>
</head>
<body>
<div class="edit-customer-form">
<h2>Kunde Bearbeiten</h2>
<form
th:action="@{/customer/update}"
th:object="${customer}"
method="post"
>
<input type="hidden" th:field="*{id}" />
<label for="name">Name:</label>
<input type="text" th:field="*{name}" required />
<label for="surname">Nachname:</label>
<input type="text" th:field="*{surname}" required />
<button type="submit">Save</button>
</form>
<a th:href="@{/customer}" class="button">Cancel</a>
</div>
</body>
</html>