mirror of
https://github.com/st-tu-dresden-praktikum/swt23w23
synced 2024-07-19 21:04:36 +02:00
Remove employee if associated with order
This commit is contained in:
parent
d75234d172
commit
f808c2497d
|
@ -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<Employee> getStaff() {
|
||||
return staff;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Employee> 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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue