From f808c2497d9eb7078beab450f797d61ce966800a Mon Sep 17 00:00:00 2001 From: Denis Natusch Date: Mon, 15 Jan 2024 14:18:52 +0100 Subject: [PATCH] Remove employee if associated with order --- src/main/java/catering/order/CustomOrder.java | 7 ++++++ .../java/catering/staff/StaffManagement.java | 8 ++++++ .../StaffControllerIntegrationTests.java | 25 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/main/java/catering/order/CustomOrder.java b/src/main/java/catering/order/CustomOrder.java index 76f503d..3c530cf 100644 --- a/src/main/java/catering/order/CustomOrder.java +++ b/src/main/java/catering/order/CustomOrder.java @@ -91,6 +91,13 @@ public class CustomOrder extends Order { return false; } + /** + * Removes an employee from all order. + */ + public boolean removeEmployee(Employee employee) { + return staff.remove(employee); + } + public Set getStaff() { return staff; } diff --git a/src/main/java/catering/staff/StaffManagement.java b/src/main/java/catering/staff/StaffManagement.java index acbdc5e..12126bc 100644 --- a/src/main/java/catering/staff/StaffManagement.java +++ b/src/main/java/catering/staff/StaffManagement.java @@ -44,6 +44,14 @@ public class StaffManagement { } public void delete(Long id) { + Employee e = staffRepository.findById(id).orElse(null); + if (e == null) return; + orderManagement.findAll(Pageable.unpaged()) + .stream() + .forEach(o -> { + o.removeEmployee(e); + orderManagement.save(o); + }); staffRepository.deleteById(id); } diff --git a/src/test/java/catering/staff/StaffControllerIntegrationTests.java b/src/test/java/catering/staff/StaffControllerIntegrationTests.java index 97569f5..1854ff4 100644 --- a/src/test/java/catering/staff/StaffControllerIntegrationTests.java +++ b/src/test/java/catering/staff/StaffControllerIntegrationTests.java @@ -18,6 +18,8 @@ import java.time.LocalDateTime; import java.time.YearMonth; import java.util.Set; +import javax.money.MonetaryAmount; + import org.javamoney.moneta.Money; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -68,6 +70,8 @@ class StaffControllerIntegrationTests { User defaultCustomer; Employee orderEmployee; CustomOrder defaultStaffOrder; + Employee removeEmployeeAssociatedWithOrder; + CustomOrder removeEmployeeAssociatedWithOrderCustomOrder; CustomOrder createCustomOrder(LocalDateTime start, LocalDateTime end, Set staff) { CustomOrder order = orderManagement.save(new CustomOrder(defaultCustomer.getUserAccount().getId(), @@ -92,6 +96,11 @@ class StaffControllerIntegrationTests { orderEmployee = staffManagement.save(new Employee("Tyler Baum", JobType.COOK, Money.of(10, EURO))); defaultStaffOrder = createCustomOrder(LocalDateTime.of(2023, 12, 20, 10, 0), LocalDateTime.of(2023, 12, 20, 21, 0), Set.of(orderEmployee)); + + removeEmployeeAssociatedWithOrder = staffManagement + .save(new Employee("Mark Baum", JobType.COOK, Money.of(10, EURO))); + removeEmployeeAssociatedWithOrderCustomOrder = createCustomOrder(LocalDateTime.of(2022, 12, 20, 10, 0), + LocalDateTime.of(2022, 12, 20, 21, 0), Set.of(removeEmployeeAssociatedWithOrder)); } @AfterEach @@ -230,4 +239,20 @@ class StaffControllerIntegrationTests { .andExpect(status().is3xxRedirection()) .andExpect(header().string(HttpHeaders.LOCATION, endsWith("/login"))); } + + @Test + @WithMockUser(username = "admin", password = "admin", roles = "ADMIN") + void removeStaffAssoicatetWithAnOrder() throws Exception { + MonetaryAmount price = removeEmployeeAssociatedWithOrderCustomOrder.getTotal(); + assertThat(removeEmployeeAssociatedWithOrderCustomOrder.getStaff().contains(removeEmployeeAssociatedWithOrder)) + .isTrue(); + assertThat(staffManagement.findAll().stream()) + .extracting("name") + .contains("Mark Baum"); + mvc.perform(post("/staff/remove").param("id", removeEmployeeAssociatedWithOrder.getId().toString())); + assertThat(staffManagement.findAll().stream()) + .extracting("name") + .doesNotContain("Mark Baum"); + assertThat(removeEmployeeAssociatedWithOrderCustomOrder.getTotal().isEqualTo(price)).isTrue(); + } }