From 691916894f12240767ff1d9953fb9aeaaae6b69a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20F=C3=BCrderer?= Date: Wed, 10 Jun 2020 11:48:38 +0200 Subject: [PATCH] Apply some changes according to comments on the pullrequest --- .../accounting/AccountingController.java | 19 ++++++------ .../ManualAccountingController.java | 31 +++++++++++++------ .../suppliers/SupplierIndexController.java | 2 +- .../intern/accounting/addManual.html | 2 +- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/intern/accounting/AccountingController.java b/prototype/src/main/java/org/hso/ecommerce/controller/intern/accounting/AccountingController.java index 92f08f3..c7f9b75 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/intern/accounting/AccountingController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/intern/accounting/AccountingController.java @@ -4,14 +4,13 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; -import javax.servlet.http.HttpServletRequest; - import org.hso.ecommerce.entities.booking.Booking; import org.hso.ecommerce.entities.booking.BookingAccountEntry; import org.hso.ecommerce.entities.booking.BookingReason; import org.hso.ecommerce.repos.booking.BookingRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller @@ -169,7 +168,7 @@ public class AccountingController { } @GetMapping("/intern/accounting/") - public String accounting(HttpServletRequest request) { + public String accounting(Model model) { List bookings = bookingRepository.allBookingsReverseChronologically(); List templateBookings = new ArrayList<>(); for (Booking booking : bookings) { @@ -200,25 +199,25 @@ public class AccountingController { templateBookings.add(tb); } - request.setAttribute("bookings", templateBookings); + model.addAttribute("bookings", templateBookings); return "intern/accounting/index"; } @GetMapping("/intern/accounting/vat") - public String accountingVat(HttpServletRequest request) { + public String accountingVat(Model model) { List bookings = bookingRepository.vatBookingsReverseChronologically(); ShortTemplateBookingResult result = buildShortTemplate(bookings, account -> account.isVATAccount); - request.setAttribute("balance", result.balance); - request.setAttribute("bookings", result.bookings); + model.addAttribute("balance", result.balance); + model.addAttribute("bookings", result.bookings); return "intern/accounting/vat"; } @GetMapping("/intern/accounting/main") - public String accountingIntern(HttpServletRequest request) { + public String accountingIntern(Model model) { List bookings = bookingRepository.mainBookingsReverseChronologically(); ShortTemplateBookingResult result = buildShortTemplate(bookings, account -> account.isMainAccount); - request.setAttribute("balance", result.balance); - request.setAttribute("bookings", result.bookings); + model.addAttribute("balance", result.balance); + model.addAttribute("bookings", result.bookings); return "intern/accounting/main"; } } diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/intern/accounting/ManualAccountingController.java b/prototype/src/main/java/org/hso/ecommerce/controller/intern/accounting/ManualAccountingController.java index 5376e25..fd820b1 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/intern/accounting/ManualAccountingController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/intern/accounting/ManualAccountingController.java @@ -1,5 +1,7 @@ package org.hso.ecommerce.controller.intern.accounting; +import java.util.Optional; + import org.hso.ecommerce.action.booking.CreateBookingAction; import org.hso.ecommerce.entities.booking.Booking; import org.hso.ecommerce.entities.booking.BookingAccountEntry; @@ -212,11 +214,15 @@ public class ManualAccountingController { } catch (NumberFormatException e) { throw new InvalidFormDataException("Die angegebene Kunden-Nr. ist ungültig."); } - return bookingAccountEntryRepository.getByUser(userId).or(() -> { - return userRepository.findById(userId).map((user) -> BookingAccountEntry.newUser(user)); - }).orElseThrow(() -> { - return new InvalidFormDataException("Der Kunde Nr. " + userId + " konnte nicht gefunden werden."); - }); + Optional bookingAccount = bookingAccountEntryRepository.getByUser(userId); + if (!bookingAccount.isPresent()) { + bookingAccount = userRepository.findById(userId).map((user) -> BookingAccountEntry.newUser(user)); + } + if (bookingAccount.isPresent()) { + return bookingAccount.get(); + } else { + throw new InvalidFormDataException("Der Kunde Nr. " + userId + " konnte nicht gefunden werden."); + } } else if (account.equals("Sup")) { long supplierId; try { @@ -224,12 +230,17 @@ public class ManualAccountingController { } catch (NumberFormatException e) { throw new InvalidFormDataException("Die angegebene Lieferanten-Nr. ist ungültig."); } - return bookingAccountEntryRepository.getBySupplier(supplierId).or(() -> { - return supplierRepository.findById(supplierId).map((sup) -> BookingAccountEntry.newSupplier(sup)); - }).orElseThrow(() -> { - return new InvalidFormDataException( + Optional bookingAccount = bookingAccountEntryRepository.getBySupplier(supplierId); + if (!bookingAccount.isPresent()) { + bookingAccount = supplierRepository.findById(supplierId) + .map((sup) -> BookingAccountEntry.newSupplier(sup)); + } + if (bookingAccount.isPresent()) { + return bookingAccount.get(); + } else { + throw new InvalidFormDataException( "Der Lieferant Nr. " + supplierId + " konnte nicht gefunden werden."); - }); + } } else { throw new RuntimeException("Invalid form value for an account: " + account); } diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/intern/suppliers/SupplierIndexController.java b/prototype/src/main/java/org/hso/ecommerce/controller/intern/suppliers/SupplierIndexController.java index 61f14f6..e8881fd 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/intern/suppliers/SupplierIndexController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/intern/suppliers/SupplierIndexController.java @@ -35,7 +35,7 @@ public class SupplierIndexController { private final BookingRepository bookingRepository = null; @Autowired - private AccountingController accountingController = null; + private final AccountingController accountingController = null; @GetMapping("suppliers") public String listSuppliers(Model model) { diff --git a/prototype/src/main/resources/templates/intern/accounting/addManual.html b/prototype/src/main/resources/templates/intern/accounting/addManual.html index bed16ca..9161dc7 100644 --- a/prototype/src/main/resources/templates/intern/accounting/addManual.html +++ b/prototype/src/main/resources/templates/intern/accounting/addManual.html @@ -23,7 +23,7 @@
-  EUR +  EUR