From d27c1958d7fa184341b139dfb3ed5a39bb995ffe Mon Sep 17 00:00:00 2001 From: Theo Reichert Date: Tue, 9 Jan 2024 18:04:25 +0100 Subject: [PATCH] Introduce new lines to reduce code smells --- .../java/catering/order/OrderController.java | 37 ++++++++++++++----- .../orderCatalog/CatalogController.java | 5 ++- .../java/catering/users/UserController.java | 5 ++- .../java/catering/users/UserManagement.java | 8 ++-- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/main/java/catering/order/OrderController.java b/src/main/java/catering/order/OrderController.java index 5883a63..601c212 100644 --- a/src/main/java/catering/order/OrderController.java +++ b/src/main/java/catering/order/OrderController.java @@ -41,10 +41,14 @@ public class OrderController { private final OrderManagement orderManagement; private final CustomOrderRepository customOrderRepository; private final UniqueInventory inventory; - private final CateringCatalog catalog; + private final CateringCatalog catalog; private final StaffManagement staffManagement; - public OrderController(OrderManagement orderManagement, CustomOrderRepository customOrderRepository, UniqueInventory inventory, CateringCatalog catalog, StaffManagement staffManagement) { + public OrderController(OrderManagement orderManagement, + CustomOrderRepository customOrderRepository, + UniqueInventory inventory, + CateringCatalog catalog, + StaffManagement staffManagement) { this.orderManagement = orderManagement; this.customOrderRepository = customOrderRepository; this.catalog = catalog; @@ -83,13 +87,16 @@ public class OrderController { @GetMapping("/allOrders/{day}") @PreAuthorize("hasRole('ADMIN')") - public String ordersForAdmin(@PathVariable String day, Model model, @Valid @ModelAttribute("form") OrderQueryForm form) { + public String ordersForAdmin(@PathVariable String day, + Model model, + @Valid @ModelAttribute("form") OrderQueryForm form) { // Obtains an instance of LocalDate from a text string such as 2007-12-03. LocalDate date = LocalDate.parse(day); List myOrders = customOrderRepository.findOrdersByInterval(date.atStartOfDay(), date.atStartOfDay().plusHours(23).withMinute(59).withSecond(59)) - .stream().filter(order -> order.getOrderStatus().equals(form.getOrderStatus().orElse(order.getOrderStatus()))).toList(); + .stream().filter(order -> order.getOrderStatus() + .equals(form.getOrderStatus().orElse(order.getOrderStatus()))).toList(); model.addAttribute("orders", myOrders); model.addAttribute("total", myOrders.size()); @@ -185,7 +192,9 @@ public class OrderController { @PostMapping("/event/addProduct") @PreAuthorize("hasRole('CUSTOMER')") - public String addProduct(@RequestParam("pid") Product product, @RequestParam("number") int number, @ModelAttribute("event") CustomCart cart) { + public String addProduct(@RequestParam("pid") Product product, + @RequestParam("number") int number, + @ModelAttribute("event") CustomCart cart) { // check if product is suitable if (product.getCategories().stream().noneMatch(c -> c.equals(cart.getOrderType().toString()))) { return "redirect:/event"; @@ -226,7 +235,11 @@ public class OrderController { @PostMapping("/event/changeDate") @PreAuthorize("hasRole('CUSTOMER')") - public String changeDate(@RequestParam("startDate") LocalDate start, @RequestParam("startHour") Optional startHour, @RequestParam("finishDate") LocalDate finish, @RequestParam("finishHour") Optional finishHour, @ModelAttribute("event") CustomCart cart) { + public String changeDate(@RequestParam("startDate") LocalDate start, + @RequestParam("startHour") Optional startHour, + @RequestParam("finishDate") LocalDate finish, + @RequestParam("finishHour") Optional finishHour, + @ModelAttribute("event") CustomCart cart) { int unwrappedStartHour = startHour.orElse(cart.getStart().getHour()); int unwrappedFinishHour = finishHour.orElse(cart.getFinish().getHour()); @@ -265,7 +278,8 @@ public class OrderController { @PostMapping("/event/changeOrderType") @PreAuthorize("hasRole('CUSTOMER')") - public String changeOrderType(@RequestParam(name = "type") String orderType, @ModelAttribute("event") CustomCart cart) { + public String changeOrderType(@RequestParam(name = "type") String orderType, + @ModelAttribute("event") CustomCart cart) { try { cart.setOrderType(OrderType.valueOf(orderType)); } catch (IllegalArgumentException e) { @@ -274,7 +288,11 @@ public class OrderController { return "redirect:/event"; } - public static Quantity findFreeAmountInInterval(Rentable product, LocalDateTime start, LocalDateTime finish, UniqueInventory inventory, CustomOrderRepository customOrderRepository) { + public static Quantity findFreeAmountInInterval(Rentable product, + LocalDateTime start, + LocalDateTime finish, + UniqueInventory inventory, + CustomOrderRepository customOrderRepository) { return inventory.findByProduct(product) .map(item -> item.getQuantity().subtract( @@ -304,7 +322,8 @@ public class OrderController { // add each order overlapping with the calendar to the days it overlaps with for (CustomOrder order : customOrderRepository.findOrdersByInterval(startDateTime, endDateTime)) { int start_index_inclusive = Math.max((int) startDate.until(order.getStart().toLocalDate(), ChronoUnit.DAYS),0); - int end_index_exclusive = Math.min((int) startDate.until(order.getFinish().toLocalDate(), ChronoUnit.DAYS), 27) + 1; + int end_index_exclusive = Math.min((int) startDate.until( + order.getFinish().toLocalDate(), ChronoUnit.DAYS), 27) + 1; String order_id = Objects.requireNonNull(order.getId()).toString(); for (int i = start_index_inclusive; i < end_index_exclusive; i++) { // FIXME: exchange order ids for a short name or a color diff --git a/src/main/java/catering/orderCatalog/CatalogController.java b/src/main/java/catering/orderCatalog/CatalogController.java index 6043f21..2be1640 100644 --- a/src/main/java/catering/orderCatalog/CatalogController.java +++ b/src/main/java/catering/orderCatalog/CatalogController.java @@ -1,5 +1,5 @@ // SPDX-License-Identifier: AGPL-3.0-or-later -// SPDX-FileCopyrightText: 2023 swt23w23 +// SPDX-FileCopyrightText: 2023-2024 swt23w23 package catering.orderCatalog; import catering.order.OrderType; @@ -23,7 +23,8 @@ public class CatalogController { private CustomCatalogEntry formCatalogEntry; private final UniqueInventory inventory; - public CatalogController(CustomCatalogEntryRepository catalogEntryRepository, UniqueInventory inventory) { + public CatalogController(CustomCatalogEntryRepository catalogEntryRepository, + UniqueInventory inventory) { productMap = new HashMap<>(); formCatalogEntry = new CustomCatalogEntry( OrderType.EVENT_CATERING, diff --git a/src/main/java/catering/users/UserController.java b/src/main/java/catering/users/UserController.java index 9c22567..5404c70 100644 --- a/src/main/java/catering/users/UserController.java +++ b/src/main/java/catering/users/UserController.java @@ -1,5 +1,5 @@ // SPDX-License-Identifier: AGPL-3.0-or-later -// SPDX-FileCopyrightText: 2023 swt23w23 +// SPDX-FileCopyrightText: 2023-2024 swt23w23 package catering.users; import static catering.users.UserForm.UserFormWithPassword.RegistrationForm; @@ -129,7 +129,8 @@ public class UserController { @PreAuthorize("hasRole('ADMIN')") public String getCustomer(Model model) { model.addAttribute("title", "Kundenverwaltung"); - model.addAttribute("customers", userManagement.getUsers().findAll().stream().filter(customer -> customer.getUserAccount().hasRole(Role.of("CUSTOMER"))).toList()); + model.addAttribute("customers", userManagement.getUsers().findAll().stream() + .filter(customer -> customer.getUserAccount().hasRole(Role.of("CUSTOMER"))).toList()); return "customers"; } diff --git a/src/main/java/catering/users/UserManagement.java b/src/main/java/catering/users/UserManagement.java index 4f95a2e..4bc3557 100644 --- a/src/main/java/catering/users/UserManagement.java +++ b/src/main/java/catering/users/UserManagement.java @@ -1,5 +1,5 @@ // SPDX-License-Identifier: AGPL-3.0-or-later -// SPDX-FileCopyrightText: 2023 swt23w23 +// SPDX-FileCopyrightText: 2023-2024 swt23w23 package catering.users; import java.util.Optional; @@ -31,11 +31,13 @@ public class UserManagement { String password, String fullName ) { - return users.save(new User(userAccounts.create(username,UnencryptedPassword.of(password),Role.of("CUSTOMER")),address,fullName)); + return users.save(new User(userAccounts.create( + username,UnencryptedPassword.of(password),Role.of("CUSTOMER")),address,fullName)); } public User createAdmin(String name, String address, String password) { - return users.save(new User(userAccounts.create(name,UnencryptedPassword.of(password),Role.of("ADMIN")), address, name)); + return users.save(new User(userAccounts.create( + name,UnencryptedPassword.of(password),Role.of("ADMIN")), address, name)); } public User save(User user) {