Add comments to InventoryController

This commit is contained in:
Paul Heimer 2023-12-07 20:47:30 +01:00 committed by Simon Bruder
parent e24ea8704f
commit 4300ee0408
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC

View file

@ -72,6 +72,12 @@ class InventoryController {
this.cateringCatalog = cateringCatalog; this.cateringCatalog = cateringCatalog;
} }
/**
* Lists all inventory products.
*
* @param model an instance of {@link Model}
* @return the inventory template
*/
@GetMapping("/inventory") @GetMapping("/inventory")
String list(Model model) { String list(Model model) {
model.addAttribute("inventory", inventory.findAll()); model.addAttribute("inventory", inventory.findAll());
@ -79,6 +85,13 @@ class InventoryController {
return "inventory"; return "inventory";
} }
/**
* Returns the edit page for an inventory product.
*
* @param model an instance of {@link Model}
* @param pid the {@link Product} to edit
* @return the inventory-mutate template with the {@link Product} data prefilled
*/
@GetMapping("/inventory/edit/{pid}") @GetMapping("/inventory/edit/{pid}")
String edit(Model model, @PathVariable Product pid) { String edit(Model model, @PathVariable Product pid) {
UniqueInventoryItem item = inventory.findByProduct(pid).get(); UniqueInventoryItem item = inventory.findByProduct(pid).get();
@ -87,6 +100,17 @@ class InventoryController {
return edit(model, form); return edit(model, form);
} }
/**
* Returns the edit page for an inventory product.
*
* This method is not mapped,
* but is used by other methods to generalise the display of the edit page.
*
* @param model an instance of {@link Model}
* @param form an already filled or empty {@link InventoryMutateForm}
* @return the inventory-mutate template with the {@link InventoryMutateForm}
* data prefilled
*/
String edit(Model model, InventoryMutateForm form) { String edit(Model model, InventoryMutateForm form) {
model.addAttribute("actionIsAdd", false); model.addAttribute("actionIsAdd", false);
model.addAttribute("form", form); model.addAttribute("form", form);
@ -94,18 +118,54 @@ class InventoryController {
return "inventory-mutate"; return "inventory-mutate";
} }
/**
* Edits a consumable.
*
* @param form a user-filled {@link ConsumableMutateForm}
* @param errors an {@link Errors} object including validation errors
* @param pid the {@link Product} to edit
* @param model an instance of {@link Model}
* @return a redirect on success, otherwise the filled form with all errors
* highlighted
*/
@PostMapping(path = "/inventory/edit/{pid}", params = "type=Consumable") @PostMapping(path = "/inventory/edit/{pid}", params = "type=Consumable")
String editConsumable(@Valid @ModelAttribute("form") ConsumableMutateForm form, Errors result, String editConsumable(@Valid @ModelAttribute("form") ConsumableMutateForm form, Errors result,
@PathVariable Consumable pid, Model model) { @PathVariable Consumable pid, Model model) {
return edit(form, result, pid, model); return edit(form, result, pid, model);
} }
/**
* Edits a rentable.
*
* @param form a user-filled {@link RentableMutateForm}
* @param errors an {@link Errors} object including validation errors
* @param pid the {@link Product} to edit
* @param model an instance of {@link Model}
* @return a redirect on success, otherwise the filled form with all errors
* highlighted
*/
@PostMapping(path = "/inventory/edit/{pid}", params = "type=Rentable") @PostMapping(path = "/inventory/edit/{pid}", params = "type=Rentable")
String editRentable(@Valid @ModelAttribute("form") RentableMutateForm form, Errors result, String editRentable(@Valid @ModelAttribute("form") RentableMutateForm form, Errors result,
@PathVariable Rentable pid, Model model) { @PathVariable Rentable pid, Model model) {
return edit(form, result, pid, model); return edit(form, result, pid, model);
} }
/**
* Edits an inventory product.
*
* This method is not mapped,
* but is used by
* {@link #editConsumable(ConsumableMutateForm, Errors, Consumable, Model)}
* and {@link #editRentable(RentableMutateForm, Errors, Rentable, Model)}
* to generalise the editing of products.
*
* @param form a user-filled {@link RentableMutateForm}
* @param errors an {@link Errors} object including validation errors
* @param product the {@link Product} to edit
* @param model an instance of {@link Model}
* @return a redirect on success, otherwise the filled form with all errors
* highlighted
*/
String edit(InventoryMutateForm form, Errors result, Product product, Model model) { String edit(InventoryMutateForm form, Errors result, Product product, Model model) {
if (result.hasErrors()) { if (result.hasErrors()) {
return edit(model, form); return edit(model, form);
@ -121,6 +181,14 @@ class InventoryController {
return "redirect:/inventory"; return "redirect:/inventory";
} }
/**
* Returns the add page for a new inventory product.
*
* @param model an instance of {@link org.springframework.ui.Model}
* @param type string representation of a {@link Product} subclass supported by
* {@link InventoryMutateForm}
* @return the inventory-mutate template with no data prefilled
*/
@GetMapping(path = "/inventory/add") @GetMapping(path = "/inventory/add")
String add(Model model, @RequestParam String type) { String add(Model model, @RequestParam String type) {
switch (type) { switch (type) {
@ -134,22 +202,60 @@ class InventoryController {
} }
} }
/**
* Returns the add page for a new inventory product.
*
* This method is not mapped,
* but is used by other methods to generalise the display of the add page.
*
* @param model an instance of {@link Model}
* @param form an already filled or empty {@link InventoryMutateForm}
* @return the inventory-mutate template with the {@link InventoryMutateForm}
* data prefilled
*/
String add(Model model, InventoryMutateForm form) { String add(Model model, InventoryMutateForm form) {
model.addAttribute("actionIsAdd", true); model.addAttribute("actionIsAdd", true);
model.addAttribute("form", form); model.addAttribute("form", form);
return "inventory-mutate"; return "inventory-mutate";
} }
/**
* Adds a consumable.
*
* @param form a user-filled {@link ConsumableMutateForm}
* @param errors an {@link Errors} object including validation errors
* @param model an instance of {@link Model}
* @return a redirect on success, otherwise the filled form with all errors
* highlighted
*/
@PostMapping(path = "/inventory/add", params = "type=Consumable") @PostMapping(path = "/inventory/add", params = "type=Consumable")
String addConsumable(@Valid @ModelAttribute("form") ConsumableMutateForm form, Errors result, Model model) { String addConsumable(@Valid @ModelAttribute("form") ConsumableMutateForm form, Errors result, Model model) {
return add(form, result, model); return add(form, result, model);
} }
/**
* Adds a rentable.
*
* @param form a user-filled {@link RentableMutateForm}
* @param errors an {@link Errors} object including validation errors
* @param model an instance of {@link Model}
* @return a redirect on success, otherwise the filled form with all errors
* highlighted
*/
@PostMapping(path = "/inventory/add", params = "type=Rentable") @PostMapping(path = "/inventory/add", params = "type=Rentable")
String addRentable(@Valid @ModelAttribute("form") RentableMutateForm form, Errors result, Model model) { String addRentable(@Valid @ModelAttribute("form") RentableMutateForm form, Errors result, Model model) {
return add(form, result, model); return add(form, result, model);
} }
/**
* Adds an inventory product.
*
* @param form a user-filled {@link InventoryMutateForm}
* @param errors an {@link Errors} object including validation errors
* @param model an instance of {@link Model}
* @return a redirect on success, otherwise the filled form with all errors
* highlighted
*/
String add(@Valid InventoryMutateForm form, Errors result, Model model) { String add(@Valid InventoryMutateForm form, Errors result, Model model) {
if (result.hasErrors()) { if (result.hasErrors()) {
return add(model, form); return add(model, form);
@ -158,6 +264,12 @@ class InventoryController {
return "redirect:/inventory"; return "redirect:/inventory";
} }
/**
* Deletes an inventory product.
*
* @param pid the {@link Product} to delete
* @return a redirect to the inventory overview
*/
@GetMapping("/inventory/delete/{pid}") @GetMapping("/inventory/delete/{pid}")
String delete(@PathVariable Product pid) { String delete(@PathVariable Product pid) {
UniqueInventoryItem item = inventory.findByProduct(pid).get(); UniqueInventoryItem item = inventory.findByProduct(pid).get();