aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/ru/mrfoxygmfr
diff options
context:
space:
mode:
authormrfoxygmfr <mrfoxygmfr@sch9.ru>2025-04-28 00:45:56 +0300
committermrfoxygmfr <mrfoxygmfr@sch9.ru>2025-04-28 00:45:56 +0300
commite9cef81f89584110776c84ee511e26097eb8323c (patch)
treedabd0daf3bde7d61509d3e496dd50f9a5b4602cf /src/main/java/ru/mrfoxygmfr
parentfacd0103437ffb50108113567d5fbb24487ffc81 (diff)
feat: implement products controller + pages
Diffstat (limited to 'src/main/java/ru/mrfoxygmfr')
-rw-r--r--src/main/java/ru/mrfoxygmfr/warehouse_accounting/http/controllers/ProductsController.java132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/main/java/ru/mrfoxygmfr/warehouse_accounting/http/controllers/ProductsController.java b/src/main/java/ru/mrfoxygmfr/warehouse_accounting/http/controllers/ProductsController.java
new file mode 100644
index 0000000..b17ec47
--- /dev/null
+++ b/src/main/java/ru/mrfoxygmfr/warehouse_accounting/http/controllers/ProductsController.java
@@ -0,0 +1,132 @@
+package ru.mrfoxygmfr.warehouse_accounting.http.controllers;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.jpa.domain.Specification;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import ru.mrfoxygmfr.warehouse_accounting.db.dao.*;
+import ru.mrfoxygmfr.warehouse_accounting.db.dao.specs.PartnerSpecs;
+import ru.mrfoxygmfr.warehouse_accounting.db.dao.specs.ProductSlotSpecs;
+import ru.mrfoxygmfr.warehouse_accounting.db.dao.specs.ProductSpecs;
+import ru.mrfoxygmfr.warehouse_accounting.db.models.*;
+
+import java.time.Duration;
+import java.util.List;
+
+@Controller
+public class ProductsController {
+ @Autowired
+ private ProductDAO productDAO;
+ @Autowired
+ private ProductSlotDAO productSlotDAO;
+
+ @GetMapping("products")
+ public String products(@RequestParam(name = "productName", required = false) String name,
+ @RequestParam(name = "productHeightLess", required = false) Integer heightLess,
+ @RequestParam(name = "productHeightGreater", required = false) Integer heightGreater,
+ @RequestParam(name = "productWidthLess", required = false) Integer widthLess,
+ @RequestParam(name = "productWidthGreater", required = false) Integer widthGreater,
+ @RequestParam(name = "productDepthLess", required = false) Integer depthLess,
+ @RequestParam(name = "productDepthGreater", required = false) Integer depthGreater,
+ Model model) {
+ Specification<Product> spec = Specification.where(null);
+ if (name != null && !name.isEmpty()) {
+ spec = spec.and(ProductSpecs.productNameLike(name));
+ model.addAttribute("productNameFilter", name);
+ }
+ if (heightLess != null) {
+ spec = spec.and(ProductSpecs.productHeightLess(heightLess));
+ model.addAttribute("productHeightLessFilter", heightLess);
+ }
+ if (heightGreater != null) {
+ spec = spec.and(ProductSpecs.productHeightGreater(heightGreater));
+ model.addAttribute("productHeightGreaterFilter", heightGreater);
+ }
+ if (widthLess != null) {
+ spec = spec.and(ProductSpecs.productWidthLess(widthLess));
+ model.addAttribute("productWidthLessFilter", widthLess);
+ }
+ if (widthGreater != null) {
+ spec = spec.and(ProductSpecs.productWidthGreater(widthGreater));
+ model.addAttribute("productWidthGreaterFilter", widthGreater);
+ }
+ if (depthLess != null) {
+ spec = spec.and(ProductSpecs.productDepthLess(depthLess));
+ model.addAttribute("productDepthLessFilter", depthLess);
+ }
+ if (depthGreater != null) {
+ spec = spec.and(ProductSpecs.productDepthGreater(depthGreater));
+ model.addAttribute("productDepthGreaterFilter", depthGreater);
+ }
+
+ List<Product> products = productDAO.findAll(spec);
+ model.addAttribute("products", products);
+ return "products";
+ }
+
+ @GetMapping("product")
+ public String product(@RequestParam(name = "id") Integer id, Model model) {
+ Product product = productDAO.findById(id).orElseThrow();
+ model.addAttribute("product", product);
+ return "productEdit";
+ }
+
+
+ @PostMapping("product")
+ public String product(@RequestParam(name = "productId") Integer id,
+ @RequestParam(name = "productName") String name,
+ @RequestParam(name = "productHeight") Integer height,
+ @RequestParam(name = "productWidth") Integer width,
+ @RequestParam(name = "productDepth") Integer depth,
+ @RequestParam(name = "productMaxStorageDuration", required = false) Integer maxStorageDurationInteger) {
+ Product product;
+ Duration maxStorageDuration = null;
+ if (maxStorageDurationInteger != null && maxStorageDurationInteger != 0) {
+ maxStorageDuration = Duration.ofDays(maxStorageDurationInteger);
+ }
+ if (id == -1) {
+ product = new Product(name, ProductType.UNKNOWN, height, width, depth);
+ product.setMaxStorageDuration(maxStorageDuration);
+ } else {
+ product = productDAO.findById(id).orElseThrow();
+ product.setName(name);
+ product.setHeight(height);
+ product.setWidth(width);
+ product.setDepth(depth);
+ product.setMaxStorageDuration(maxStorageDuration);
+ }
+ productDAO.save(product);
+ return "redirect:/products";
+ }
+
+ @GetMapping("storage")
+ public String productsStorage(@RequestParam(name = "storageName", required = false) String name,
+ @RequestParam(name = "storageStatus", required = false) ProductStorageStatus storageStatus,
+ Model model) {
+ Specification<ProductSlot> spec = Specification.where(null);
+ if (name != null && !name.isEmpty()) {
+ spec = spec.and(ProductSlotSpecs.productSlotContainsLike(name));
+ model.addAttribute("storageNameFilter", name);
+ }
+ if (storageStatus != null) {
+ spec = spec.and(ProductSlotSpecs.productStorageStatusEqual(storageStatus));
+ model.addAttribute("storageStatusFilter", storageStatus.toString());
+ }
+
+ List<ProductSlot> storage = productSlotDAO.findAll(spec);
+ model.addAttribute("storage", storage);
+ return "storage";
+ }
+
+ @GetMapping("newProduct")
+ public String newProduct(Model model) {
+ Product product = new Product();
+ product.setId(-1);
+ model.addAttribute("product", product);
+ model.addAttribute("newItem", true);
+ return "productEdit";
+ }
+}