From f50fbb5ac4eb71e6aa0f8c0409be18ac9faa3a48 Mon Sep 17 00:00:00 2001 From: Mathis Kral Date: Sun, 26 Nov 2023 16:59:50 +0100 Subject: [PATCH] Add basic order-staff-relation Works on #73 This contains changes on multiple files to associate multiple employees with an order and make customers able to add employees to their order. Because of SQL JPA things, this will not be final. --- src/main/java/catering/order/CustomCart.java | 39 ++++++++++++-- src/main/java/catering/order/CustomOrder.java | 7 +-- .../java/catering/order/OrderController.java | 54 ++++++++++++++----- src/main/java/catering/staff/Staff.java | 8 +++ src/main/resources/templates/event.html | 40 +++++++++++++- src/main/resources/templates/orders.html | 8 ++- .../OrderControllerIntegrationTests.java | 19 ++++++- 7 files changed, 149 insertions(+), 26 deletions(-) diff --git a/src/main/java/catering/order/CustomCart.java b/src/main/java/catering/order/CustomCart.java index 8457371..b42fbf0 100644 --- a/src/main/java/catering/order/CustomCart.java +++ b/src/main/java/catering/order/CustomCart.java @@ -1,15 +1,17 @@ package catering.order; import catering.staff.Staff; +import org.javamoney.moneta.Money; import org.salespointframework.order.Cart; +import javax.money.MonetaryAmount; import java.time.Duration; import java.time.LocalDateTime; import java.util.HashSet; import java.util.Set; public class CustomCart extends Cart { - private Set staff; + private final Set staff; private OrderType orderType; private LocalDateTime start; private LocalDateTime finish; @@ -29,6 +31,11 @@ public class CustomCart extends Cart { * Adds an employee to the cart */ public boolean addStaff(Staff staff) { + for (Staff myStaff : this.staff) { + if (myStaff.equals(staff)) { + return false; + } + } return this.staff.add(staff); } @@ -37,7 +44,12 @@ public class CustomCart extends Cart { } public boolean removeStaff(Staff staff) { - return this.staff.remove(staff); + for (Staff myStaff : this.staff) { + if (myStaff.equals(staff)) { + return this.staff.remove(myStaff); + } + } + return false; } /** @@ -68,10 +80,10 @@ public class CustomCart extends Cart { } /** - * @return hours between start and finish + * @return hours between start and finish multiplied with a rate */ - public long getDurationInHours(LocalDateTime start, LocalDateTime finish) { - return Duration.between(start, finish).getSeconds() / 3600; + public double getDurationInHoursTimesRate(double rate) { + return (float) Duration.between(start, finish).getSeconds() / 3600 * rate; } public LocalDateTime getFinish() { @@ -85,4 +97,21 @@ public class CustomCart extends Cart { public String getFormatterPattern() { return formatterPattern; } + + @Override + public MonetaryAmount getPrice() { + MonetaryAmount total = super.getPrice(); + + for (int i = 0; i < staff.size(); i++) { // TODO: get this from staff itself + total = total.add(Money.of(getDurationInHoursTimesRate(12.0), "EUR")); + } + + return total; + } + + @Override + public void clear() { + super.clear(); + staff.forEach(staff::remove); + } } diff --git a/src/main/java/catering/order/CustomOrder.java b/src/main/java/catering/order/CustomOrder.java index 8e49d28..b5b9975 100644 --- a/src/main/java/catering/order/CustomOrder.java +++ b/src/main/java/catering/order/CustomOrder.java @@ -1,10 +1,7 @@ package catering.order; import catering.staff.Staff; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.OneToMany; +import jakarta.persistence.*; import org.javamoney.moneta.Money; import org.salespointframework.order.Order; import org.salespointframework.payment.Cash; @@ -23,7 +20,7 @@ public class CustomOrder extends Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @OneToMany + @ManyToMany private Set staff; private OrderType orderType = OrderType.SOMETHING_ELSE; private LocalDateTime start; diff --git a/src/main/java/catering/order/OrderController.java b/src/main/java/catering/order/OrderController.java index 78fa576..6f17b24 100644 --- a/src/main/java/catering/order/OrderController.java +++ b/src/main/java/catering/order/OrderController.java @@ -1,18 +1,16 @@ package catering.order; -import jakarta.persistence.PostPersist; +import catering.staff.Staff; +import catering.staff.StaffManagement; import org.salespointframework.catalog.Product; import org.salespointframework.inventory.UniqueInventory; import org.salespointframework.inventory.UniqueInventoryItem; import org.salespointframework.order.*; import org.salespointframework.quantity.Quantity; -import org.salespointframework.time.Interval; import org.salespointframework.useraccount.Role; import org.salespointframework.useraccount.UserAccount; import org.salespointframework.useraccount.web.LoggedIn; import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; -import org.springframework.data.jpa.repository.query.Procedure; import org.springframework.data.util.Streamable; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; @@ -23,8 +21,6 @@ import java.time.LocalDateTime; import java.time.LocalDate; import java.time.LocalTime; import java.time.temporal.ChronoUnit; -import java.time.format.DateTimeFormatter; -import java.time.temporal.TemporalUnit; import java.util.*; import java.util.stream.Collectors; @@ -36,16 +32,18 @@ public class OrderController { private final OrderManagement orderManagement; private final UniqueInventory inventory; + private final StaffManagement staffManagement; - public OrderController(OrderManagement orderManagement, UniqueInventory inventory) { + public OrderController(OrderManagement orderManagement, UniqueInventory inventory, StaffManagement staffManagement) { this.orderManagement = orderManagement; this.inventory = inventory; + this.staffManagement = staffManagement; } @GetMapping("/myOrders") @PreAuthorize("hasRole('CUSTOMER')") public String orders(Model model, @LoggedIn Optional userAccount) { - List myOrders = orderManagement.findBy(userAccount.get()).stream().collect(Collectors.toList()); // to be changed + List myOrders = orderManagement.findBy(userAccount.get()).stream().collect(Collectors.toList()); model.addAttribute("orders", myOrders); model.addAttribute("total", myOrders.size()); @@ -70,7 +68,7 @@ public class OrderController { @GetMapping("/allOrders/{day}") @PreAuthorize("hasRole('ADMIN')") public String orders(@PathVariable String day, Model model) { - // Obtains an instance of LocalDate from a text string such as 2007-12-03. (https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) + // Obtains an instance of LocalDate from a text string such as 2007-12-03. LocalDate date = LocalDate.parse(day); List myOrders = orderManagement.findAll(Pageable.unpaged()).stream().filter( @@ -86,7 +84,8 @@ public class OrderController { @ModelAttribute("event") CustomCart initializeCart() { - return new CustomCart(OrderType.SOMETHING_ELSE, LocalDateTime.now().plusDays(7), LocalDateTime.now().plusDays(7)); + return new CustomCart(OrderType.SOMETHING_ELSE, LocalDateTime.now().plusDays(7), + LocalDateTime.now().plusDays(7)); } @GetMapping("/event") @@ -95,9 +94,35 @@ public class OrderController { model.addAttribute("items", cart.stream().collect(Collectors.toList())); model.addAttribute("totalPrice", cart.getPrice()); model.addAttribute("invItems", inventory.findAll().stream().collect(Collectors.toList())); + Set myStaff = new HashSet<>(); + staffManagement.findAll().forEach(myStaff::add); + model.addAttribute("allStaff", myStaff); + return "event"; } + @PostMapping("/event/addStaff") + @PreAuthorize("hasRole('CUSTOMER')") + String addStaffToCart(@RequestParam("sid") long staffId, @ModelAttribute("event") CustomCart cart) { + Staff staff = staffManagement.findById(staffId).get(); + + if (cart.getStaff().contains(staff)) { + return "redirect:/event"; + } + + cart.addStaff(staff); + return "redirect:/event"; + } + + @PostMapping("/event/removeStaff") + @PreAuthorize("hasRole('CUSTOMER')") + String removeStaffFromCart(@RequestParam("sid") long staffId, @ModelAttribute("event") CustomCart cart) { + Staff staff = staffManagement.findById(staffId).get(); + cart.removeStaff(staff); + + return "redirect:/event"; + } + @PostMapping("/allOrders/remove") @PreAuthorize("hasRole('ADMIN')") public String removeOrder(@RequestParam Order.OrderIdentifier orderID, @LoggedIn Optional userAccount) { @@ -146,8 +171,10 @@ public class OrderController { int unwrappedFinishHour = finishHour.orElse(cart.getFinish().getHour()); finish = finish.isBefore(start) ? start : finish; - LocalDateTime startTime = LocalDateTime.of(start, LocalTime.of(unwrappedStartHour, 0)); // TODO - LocalDateTime finishTime = LocalDateTime.of(finish, LocalTime.of(unwrappedFinishHour <= unwrappedStartHour ? unwrappedStartHour+1 : unwrappedFinishHour, 0)); // TODO + LocalDateTime startTime = LocalDateTime.of(start, LocalTime.of(unwrappedStartHour, 0)); + LocalDateTime finishTime = LocalDateTime.of(finish, + LocalTime.of(unwrappedFinishHour <= unwrappedStartHour + && !finish.isAfter(start) ? unwrappedStartHour+1 : unwrappedFinishHour, 0)); cart.setStart(startTime); cart.setFinish(finishTime); @@ -158,13 +185,14 @@ public class OrderController { @PreAuthorize("hasRole('CUSTOMER')") public String checkout(@ModelAttribute("event") CustomCart cart, @LoggedIn Optional userAccount) { - if (cart.isEmpty()) { + if (cart.isEmpty() && cart.getStaff().isEmpty()) { return "redirect:/event"; } return userAccount.map(account -> { CustomOrder myOrder = new CustomOrder(account.getId(), cart); cart.addItemsTo(myOrder); + cart.addStaffTo(myOrder); orderManagement.payOrder(myOrder); // TODO: change this later orderManagement.completeOrder(myOrder); cart.clear(); diff --git a/src/main/java/catering/staff/Staff.java b/src/main/java/catering/staff/Staff.java index 93a4b0c..d0b36c2 100644 --- a/src/main/java/catering/staff/Staff.java +++ b/src/main/java/catering/staff/Staff.java @@ -40,4 +40,12 @@ public class Staff { public void setJob(JobType job) { this.job = job; } + + @Override + public boolean equals(Object obj) { + if (obj instanceof Staff other) { + return id.equals(other.id); + } + return false; + } } diff --git a/src/main/resources/templates/event.html b/src/main/resources/templates/event.html index 86e9004..9d63ebf 100644 --- a/src/main/resources/templates/event.html +++ b/src/main/resources/templates/event.html @@ -49,7 +49,7 @@ Sake Nigiri 200 - 10€ + 10€
@@ -57,6 +57,21 @@
+ Angestellter + Typ + Preis + + + Name + Job + Preis + +
+ + +
+ + Price @@ -91,5 +106,28 @@ + +

Angestellte hinzufügen

+ + + + + + + + + + + + + + + +
NameTypStundensatzVerfügbar
NameJob12€ Verfügbar +
+ + +
+
diff --git a/src/main/resources/templates/orders.html b/src/main/resources/templates/orders.html index dbae821..d5cbb47 100644 --- a/src/main/resources/templates/orders.html +++ b/src/main/resources/templates/orders.html @@ -10,7 +10,7 @@ Von Bis - Kunde + Kunde Produktdetails Rechnung Status @@ -31,6 +31,12 @@ +
    +
  • +
    + +
  • +
diff --git a/src/test/java/catering/order/OrderControllerIntegrationTests.java b/src/test/java/catering/order/OrderControllerIntegrationTests.java index bf956da..3364930 100644 --- a/src/test/java/catering/order/OrderControllerIntegrationTests.java +++ b/src/test/java/catering/order/OrderControllerIntegrationTests.java @@ -1,5 +1,8 @@ package catering.order; +import catering.staff.JobType; +import catering.staff.Staff; +import catering.staff.StaffManagement; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.salespointframework.catalog.Product; @@ -45,10 +48,13 @@ public class OrderControllerIntegrationTests { @Autowired UserAccountManagement userAccountManagement; + @Autowired + StaffManagement staffManagement; + UserAccount myUser; UserAccount admin; - CustomCart myCart; + Staff myStaff; @BeforeEach void setUp() { @@ -57,6 +63,9 @@ public class OrderControllerIntegrationTests { Password.UnencryptedPassword.of("12345"), Role.of("CUSTOMER")); } + myStaff = new Staff("Sabrina", JobType.SERVICE); + staffManagement.addStaff(myStaff); + myUser = userAccountManagement.findByUsername("andi").get(); admin = userAccountManagement.findByUsername("admin").get(); @@ -177,4 +186,12 @@ public class OrderControllerIntegrationTests { .andExpect(redirectedUrl("/event")); } + @Test + @WithMockUser(username = "andi", roles = "CUSTOMER") + void addStaff() throws Exception { + mvc.perform(post("/event/addStaff") + .param("sid", myStaff.getId().toString())) + .andExpect(redirectedUrl("/event")); + } + }