2023-11-05 16:11:36 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2023 Simon Bruder
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package catering.inventory;
|
|
|
|
|
|
|
|
import org.salespointframework.catalog.Product;
|
|
|
|
import org.salespointframework.inventory.UniqueInventory;
|
|
|
|
import org.salespointframework.inventory.UniqueInventoryItem;
|
2023-11-18 12:34:40 +01:00
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
2023-11-05 16:11:36 +01:00
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
import org.springframework.validation.Errors;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
2023-11-20 23:26:47 +01:00
|
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
2023-11-05 16:11:36 +01:00
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
2023-11-29 17:13:07 +01:00
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
2023-11-05 16:11:36 +01:00
|
|
|
|
|
|
|
import catering.catalog.CateringCatalog;
|
|
|
|
import jakarta.validation.Valid;
|
|
|
|
|
2023-11-29 17:13:07 +01:00
|
|
|
/*
|
|
|
|
* TODO TL;DR: Use HandlerMethodArgumentResolver
|
|
|
|
*
|
|
|
|
* This class currently has many confusing methods that do a variety of things,
|
|
|
|
* but mostly work around polymorphic restrictions of Spring.
|
|
|
|
* Ideally, spring would allow a form to be polymorphic
|
|
|
|
* based on its attributes, if they are unique,
|
|
|
|
* or based on another parameter.
|
|
|
|
* From what I could find,
|
|
|
|
* this is not possible without implementing a custom HandlerMethodArgumentResolver.
|
|
|
|
* If there is time, the controller should be changed to use this,
|
|
|
|
* which should vastly simplify it.
|
|
|
|
*
|
|
|
|
* Adding will always require a type parameter,
|
|
|
|
* as the type is not known at first.
|
|
|
|
* However, currently there are two (or even three) POST handlers,
|
|
|
|
* as there are two different form types it must support
|
|
|
|
* (and an infinite amount of invalid types that it should ignore).
|
|
|
|
*
|
|
|
|
* Editing should not require passing any additional type parameter around
|
|
|
|
* as the type can be inferred from the product.
|
|
|
|
* Changing the type of a product should not be possible.
|
|
|
|
*/
|
|
|
|
|
2023-11-05 16:11:36 +01:00
|
|
|
@Controller
|
2023-12-07 19:12:55 +01:00
|
|
|
@PreAuthorize("hasRole('ADMIN')")
|
2023-11-05 16:11:36 +01:00
|
|
|
class InventoryController {
|
|
|
|
private final UniqueInventory<UniqueInventoryItem> inventory;
|
|
|
|
private final CateringCatalog cateringCatalog;
|
|
|
|
|
|
|
|
InventoryController(UniqueInventory<UniqueInventoryItem> inventory, CateringCatalog cateringCatalog) {
|
|
|
|
Assert.notNull(inventory, "Inventory must not be null!");
|
|
|
|
Assert.notNull(inventory, "CateringCatalog must not be null!");
|
|
|
|
this.inventory = inventory;
|
|
|
|
this.cateringCatalog = cateringCatalog;
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/inventory")
|
|
|
|
String list(Model model) {
|
|
|
|
model.addAttribute("inventory", inventory.findAll());
|
|
|
|
|
|
|
|
return "inventory";
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/inventory/edit/{pid}")
|
|
|
|
String edit(Model model, @PathVariable Product pid) {
|
2023-11-21 23:01:33 +01:00
|
|
|
UniqueInventoryItem item = inventory.findByProduct(pid).get();
|
2023-11-29 17:13:07 +01:00
|
|
|
final InventoryMutateForm form = InventoryMutateForm.of(pid, item);
|
|
|
|
|
|
|
|
return edit(model, form);
|
2023-11-21 23:01:33 +01:00
|
|
|
}
|
|
|
|
|
2023-11-24 20:11:52 +01:00
|
|
|
String edit(Model model, InventoryMutateForm form) {
|
2023-11-21 23:01:33 +01:00
|
|
|
model.addAttribute("actionIsAdd", false);
|
|
|
|
model.addAttribute("form", form);
|
2023-11-05 16:11:36 +01:00
|
|
|
|
|
|
|
return "inventory-mutate";
|
|
|
|
}
|
|
|
|
|
2023-11-29 17:13:07 +01:00
|
|
|
@PostMapping(path = "/inventory/edit/{pid}", params = "type=Consumable")
|
|
|
|
String editConsumable(@Valid @ModelAttribute("form") ConsumableMutateForm form, Errors result,
|
|
|
|
@PathVariable Product pid, Model model) {
|
|
|
|
return edit(form, result, pid, model);
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping(path = "/inventory/edit/{pid}", params = "type=Rentable")
|
|
|
|
String editRentable(@Valid @ModelAttribute("form") RentableMutateForm form, Errors result,
|
|
|
|
@PathVariable Product pid, Model model) {
|
|
|
|
return edit(form, result, pid, model);
|
|
|
|
}
|
|
|
|
|
|
|
|
String edit(InventoryMutateForm form, Errors result, Product product, Model model) {
|
2023-11-05 16:11:36 +01:00
|
|
|
if (result.hasErrors()) {
|
2023-11-24 20:11:52 +01:00
|
|
|
return edit(model, form);
|
2023-11-05 16:11:36 +01:00
|
|
|
}
|
|
|
|
|
2023-11-29 17:13:07 +01:00
|
|
|
form.modifyProduct(product);
|
2023-11-05 16:11:36 +01:00
|
|
|
product = cateringCatalog.save(product);
|
|
|
|
|
2023-11-29 17:13:07 +01:00
|
|
|
UniqueInventoryItem item = inventory.findByProduct(product).get();
|
2023-11-05 16:11:36 +01:00
|
|
|
// no setQuantity in enterprise java
|
|
|
|
// (though returing a modified object is actually nice)
|
|
|
|
inventory.save(item.increaseQuantity(form.getQuantity().subtract(item.getQuantity())));
|
|
|
|
return "redirect:/inventory";
|
|
|
|
}
|
|
|
|
|
2023-11-29 17:13:07 +01:00
|
|
|
@GetMapping(path = "/inventory/add")
|
|
|
|
String add(Model model, @RequestParam String type) {
|
|
|
|
switch (type) {
|
|
|
|
case "Consumable":
|
|
|
|
return add(model, new ConsumableMutateForm());
|
|
|
|
case "Rentable":
|
|
|
|
return add(model, new RentableMutateForm());
|
|
|
|
default:
|
|
|
|
// TODO better error handling
|
|
|
|
return "redirect:/inventory";
|
|
|
|
}
|
2023-11-20 23:26:47 +01:00
|
|
|
}
|
2023-11-21 22:42:23 +01:00
|
|
|
|
2023-11-20 23:26:47 +01:00
|
|
|
String add(Model model, InventoryMutateForm form) {
|
|
|
|
model.addAttribute("actionIsAdd", true);
|
|
|
|
model.addAttribute("form", form);
|
2023-11-05 16:11:36 +01:00
|
|
|
return "inventory-mutate";
|
|
|
|
}
|
|
|
|
|
2023-11-29 17:13:07 +01:00
|
|
|
@PostMapping(path = "/inventory/add", params = "type=Consumable")
|
|
|
|
String addConsumable(@Valid @ModelAttribute("form") ConsumableMutateForm form, Errors result, Model model) {
|
|
|
|
return add(form, result, model);
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping(path = "/inventory/add", params = "type=Rentable")
|
|
|
|
String addRentable(@Valid @ModelAttribute("form") ConsumableMutateForm form, Errors result, Model model) {
|
|
|
|
return add(form, result, model);
|
|
|
|
}
|
|
|
|
|
|
|
|
String add(@Valid InventoryMutateForm form, Errors result, Model model) {
|
2023-11-05 16:11:36 +01:00
|
|
|
if (result.hasErrors()) {
|
2023-11-20 23:26:47 +01:00
|
|
|
return add(model, form);
|
2023-11-05 16:11:36 +01:00
|
|
|
}
|
2023-11-29 17:13:07 +01:00
|
|
|
inventory.save(new UniqueInventoryItem(cateringCatalog.save(form.toProduct()), form.getQuantity()));
|
2023-11-05 16:11:36 +01:00
|
|
|
return "redirect:/inventory";
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/inventory/delete/{pid}")
|
|
|
|
String delete(@PathVariable Product pid) {
|
|
|
|
UniqueInventoryItem item = inventory.findByProduct(pid).get();
|
|
|
|
inventory.delete(item);
|
2023-11-29 17:13:07 +01:00
|
|
|
cateringCatalog.delete(pid);
|
2023-11-05 16:11:36 +01:00
|
|
|
return "redirect:/inventory";
|
|
|
|
}
|
|
|
|
}
|