swt23w23/src/main/java/catering/inventory/InventoryController.java

147 lines
5.1 KiB
Java
Raw Normal View History

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 static org.salespointframework.core.Currencies.EURO;
import java.util.Optional;
import javax.money.MonetaryAmount;
import javax.money.NumberValue;
import org.javamoney.moneta.Money;
2023-11-05 16:11:36 +01:00
import org.salespointframework.catalog.Product;
import org.salespointframework.inventory.UniqueInventory;
import org.salespointframework.inventory.UniqueInventoryItem;
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;
import catering.catalog.CatalogDummy;
import catering.catalog.CateringCatalog;
import jakarta.validation.Valid;
@Controller
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;
}
@PreAuthorize("hasRole('ADMIN')")
2023-11-05 16:11:36 +01:00
@GetMapping("/inventory")
String list(Model model) {
model.addAttribute("inventory", inventory.findAll());
return "inventory";
}
@PreAuthorize("hasRole('ADMIN')")
2023-11-05 16:11:36 +01:00
@GetMapping("/inventory/edit/{pid}")
String edit(Model model, @PathVariable Product pid) {
CatalogDummy product = (CatalogDummy) pid;
UniqueInventoryItem item = inventory.findByProduct(pid).get();
return edit(model,
new InventoryMutateForm(product.getType(),
product.getName(),
item.getQuantity(),
product.getWholesalePrice().getNumber().doubleValueExact(),
product.getPrice().getNumber().doubleValueExact(),
Optional.ofNullable(product.getPromotionPrice())
.map(MonetaryAmount::getNumber)
.map(NumberValue::doubleValueExact)));
}
String edit(Model model, InventoryMutateForm form) {
model.addAttribute("actionIsAdd", false);
model.addAttribute("form", form);
2023-11-05 16:11:36 +01:00
return "inventory-mutate";
}
@PreAuthorize("hasRole('ADMIN')")
2023-11-05 16:11:36 +01:00
@PostMapping("/inventory/edit/{pid}")
String edit(@Valid @ModelAttribute("form") InventoryMutateForm form, Errors result, @PathVariable Product pid,
Model model) {
2023-11-05 16:11:36 +01:00
if (result.hasErrors()) {
return edit(model, form);
2023-11-05 16:11:36 +01:00
}
CatalogDummy product = (CatalogDummy) pid;
UniqueInventoryItem item = inventory.findByProduct(pid).get();
product.setName(form.getName());
product.setType(form.getType());
product.setPrice(Money.of(form.getRetailPrice(), EURO));
product.setWholesalePrice(Money.of(form.getWholesalePrice(), EURO));
product.setPromotionPrice(form.getPromotionPrice().map(price -> Money.of(price, EURO)).orElse(null));
2023-11-05 16:11:36 +01:00
product = cateringCatalog.save(product);
// 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";
}
@PreAuthorize("hasRole('ADMIN')")
2023-11-05 16:11:36 +01:00
@GetMapping("/inventory/add")
String add(Model model) {
2023-11-20 23:26:47 +01:00
return add(model, InventoryMutateForm.empty());
}
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";
}
@PreAuthorize("hasRole('ADMIN')")
2023-11-05 16:11:36 +01:00
@PostMapping("/inventory/add")
2023-11-20 23:26:47 +01:00
String add(@Valid @ModelAttribute("form") 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
}
inventory.save(new UniqueInventoryItem(
cateringCatalog
.save(new CatalogDummy(form.getName(), form.getType(), Money.of(form.getRetailPrice(), EURO),
Money.of(form.getWholesalePrice(), EURO),
form.getPromotionPrice().map(price -> Money.of(price, EURO)).orElse(null))),
2023-11-05 16:11:36 +01:00
form.getQuantity()));
return "redirect:/inventory";
}
@PreAuthorize("hasRole('ADMIN')")
2023-11-05 16:11:36 +01:00
@GetMapping("/inventory/delete/{pid}")
String delete(@PathVariable Product pid) {
UniqueInventoryItem item = inventory.findByProduct(pid).get();
inventory.delete(item);
cateringCatalog.delete((CatalogDummy) pid);
return "redirect:/inventory";
}
}