From aa54118786d87054b7784269880a03f201d507ca Mon Sep 17 00:00:00 2001 From: Eren Asker Date: Fri, 10 Nov 2023 17:10:24 +0100 Subject: [PATCH] Add customer overview prototype --- src/main/java/catering/customer/Customer.java | 40 ++++++++++++ .../catering/customer/CustomerController.java | 34 +++++++++- .../customer/CustomerDataInitializer.java | 35 ++++++++++ .../catering/customer/CustomerRepository.java | 53 +++++++++++++++ .../resources/css/customer/edit-style.css | 21 ++++++ .../static/resources/css/customer/style.css | 64 +++++++++++++++++++ src/main/resources/templates/customer.html | 43 +++++++++++++ .../resources/templates/edit-customer.html | 33 ++++++++++ 8 files changed, 321 insertions(+), 2 deletions(-) create mode 100644 src/main/java/catering/customer/Customer.java create mode 100644 src/main/java/catering/customer/CustomerDataInitializer.java create mode 100644 src/main/java/catering/customer/CustomerRepository.java create mode 100644 src/main/resources/static/resources/css/customer/edit-style.css create mode 100644 src/main/resources/static/resources/css/customer/style.css create mode 100644 src/main/resources/templates/customer.html create mode 100644 src/main/resources/templates/edit-customer.html diff --git a/src/main/java/catering/customer/Customer.java b/src/main/java/catering/customer/Customer.java new file mode 100644 index 0000000..f33ca1d --- /dev/null +++ b/src/main/java/catering/customer/Customer.java @@ -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; + } + +} diff --git a/src/main/java/catering/customer/CustomerController.java b/src/main/java/catering/customer/CustomerController.java index 4241a6f..2252ca9 100644 --- a/src/main/java/catering/customer/CustomerController.java +++ b/src/main/java/catering/customer/CustomerController.java @@ -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"; + } } diff --git a/src/main/java/catering/customer/CustomerDataInitializer.java b/src/main/java/catering/customer/CustomerDataInitializer.java new file mode 100644 index 0000000..f437a9a --- /dev/null +++ b/src/main/java/catering/customer/CustomerDataInitializer.java @@ -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"); + + } +} diff --git a/src/main/java/catering/customer/CustomerRepository.java b/src/main/java/catering/customer/CustomerRepository.java new file mode 100644 index 0000000..1983072 --- /dev/null +++ b/src/main/java/catering/customer/CustomerRepository.java @@ -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 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 getCustomers() { + return new ArrayList<>(this.customers); + } + + public Optional findById(int id) { + return this.customers.stream().filter(customer -> customer.getId() == id).findFirst(); + } + +} diff --git a/src/main/resources/static/resources/css/customer/edit-style.css b/src/main/resources/static/resources/css/customer/edit-style.css new file mode 100644 index 0000000..f102890 --- /dev/null +++ b/src/main/resources/static/resources/css/customer/edit-style.css @@ -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; +} diff --git a/src/main/resources/static/resources/css/customer/style.css b/src/main/resources/static/resources/css/customer/style.css new file mode 100644 index 0000000..9aeb7b4 --- /dev/null +++ b/src/main/resources/static/resources/css/customer/style.css @@ -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; +} diff --git a/src/main/resources/templates/customer.html b/src/main/resources/templates/customer.html new file mode 100644 index 0000000..384ce24 --- /dev/null +++ b/src/main/resources/templates/customer.html @@ -0,0 +1,43 @@ + + + + + + + + +
+

Kundenverwaltung

+

Kundenübersicht

+ + + + + + + + + + + + + + + +
IDNachnameVornameAufträge
01MusterkochMaxAufträge + Bearbeiten + +
+ + +
+
+
+ + diff --git a/src/main/resources/templates/edit-customer.html b/src/main/resources/templates/edit-customer.html new file mode 100644 index 0000000..41a44c8 --- /dev/null +++ b/src/main/resources/templates/edit-customer.html @@ -0,0 +1,33 @@ + + + + + Kunde Bearbeiten + + + + +
+

Kunde Bearbeiten

+
+ + + + + + + + + +
+ Cancel +
+ +