From cf46765fb9df74129409132c921c62ab1df12b6e Mon Sep 17 00:00:00 2001 From: Inohara Date: Tue, 2 May 2023 14:47:10 +0400 Subject: [PATCH] =?UTF-8?q?=D1=83=D1=81=D1=82=D0=B0=D0=BB=D0=B0=20-=5F-?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/build.gradle | 9 +- .../com/example/demo/WebConfiguration.java | 22 +-- .../supply/Order/{_Order.java => Order.java} | 18 ++- .../demo/supply/Order/OrderController.java | 15 +- .../example/demo/supply/Order/OrderDto.java | 15 +- .../demo/supply/Order/OrderMvcController.java | 72 ++++++++++ .../demo/supply/Order/OrderRepository.java | 7 +- .../demo/supply/Order/OrderService.java | 36 ++--- .../example/demo/supply/Product/Product.java | 13 +- .../demo/supply/Product/ProductDto.java | 18 ++- .../supply/Product/ProductMvcController.java | 7 +- .../demo/supply/Product/ProductService.java | 1 - .../demo/supply/Supplier/Supplier.java | 14 +- .../demo/supply/Supplier/SupplierDto.java | 18 ++- .../Supplier/SupplierMvcController.java | 19 +-- .../example/demo/util/AdviceController.java | 37 ----- .../demo/util/ValidationException.java | 9 -- .../com/example/demo/util/ValidatorUtil.java | 29 ---- .../src/main/resources/application.properties | 2 +- .../resources/templates/common/header.html | 10 ++ .../templates/{main.html => default.html} | 22 +-- demo/src/main/resources/templates/index.html | 3 +- .../main/resources/templates/order-add.html | 135 ++++++++++++++++++ demo/src/main/resources/templates/order.html | 49 +++++++ .../resources/templates/product-edit.html | 3 +- .../src/main/resources/templates/product.html | 4 +- .../resources/templates/supplier-edit.html | 13 +- .../main/resources/templates/supplier.html | 4 +- 28 files changed, 416 insertions(+), 188 deletions(-) rename demo/src/main/java/com/example/demo/supply/Order/{_Order.java => Order.java} (89%) create mode 100644 demo/src/main/java/com/example/demo/supply/Order/OrderMvcController.java delete mode 100644 demo/src/main/java/com/example/demo/util/AdviceController.java delete mode 100644 demo/src/main/java/com/example/demo/util/ValidationException.java delete mode 100644 demo/src/main/java/com/example/demo/util/ValidatorUtil.java create mode 100644 demo/src/main/resources/templates/common/header.html rename demo/src/main/resources/templates/{main.html => default.html} (65%) create mode 100644 demo/src/main/resources/templates/order-add.html create mode 100644 demo/src/main/resources/templates/order.html diff --git a/demo/build.gradle b/demo/build.gradle index 3e80b02..367330f 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -1,7 +1,7 @@ plugins { id 'java' - id 'org.springframework.boot' version '3.0.2' - id 'io.spring.dependency-management' version '1.1.0' + id 'org.springframework.boot' version '2.6.3' + id 'io.spring.dependency-management' version '1.0.11.RELEASE' } group = 'com.example' @@ -17,7 +17,6 @@ jar{ } dependencies { - implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-devtools' @@ -29,7 +28,9 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'com.h2database:h2:2.1.210' - implementation 'org.springframework.boot:spring-boot-starter-validation' + + implementation 'org.hibernate.validator:hibernate-validator' + implementation 'org.springdoc:springdoc-openapi-ui:1.6.5' testImplementation 'org.springframework.boot:spring-boot-starter-test' diff --git a/demo/src/main/java/com/example/demo/WebConfiguration.java b/demo/src/main/java/com/example/demo/WebConfiguration.java index 97ec6de..0ee3e83 100644 --- a/demo/src/main/java/com/example/demo/WebConfiguration.java +++ b/demo/src/main/java/com/example/demo/WebConfiguration.java @@ -8,16 +8,16 @@ import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration -@EnableWebMvc -public class WebConfiguration { - @Bean - public WebMvcConfigurer corsConfigurer() { - return new WebMvcConfigurer() { - @Override - public void addCorsMappings(CorsRegistry registry) { - registry.addMapping("/**") - .allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH", "OPTIONS"); - } - }; +public class WebConfiguration implements WebMvcConfigurer { + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + WebMvcConfigurer.super.addViewControllers(registry); + registry.addViewController("rest-test"); + } + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**").allowedMethods("*"); } } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/supply/Order/_Order.java b/demo/src/main/java/com/example/demo/supply/Order/Order.java similarity index 89% rename from demo/src/main/java/com/example/demo/supply/Order/_Order.java rename to demo/src/main/java/com/example/demo/supply/Order/Order.java index 58a1061..762d2e6 100644 --- a/demo/src/main/java/com/example/demo/supply/Order/_Order.java +++ b/demo/src/main/java/com/example/demo/supply/Order/Order.java @@ -3,16 +3,22 @@ package com.example.demo.supply.Order; import com.example.demo.supply.Product.Product; import com.example.demo.supply.Supplier.Supplier; import com.fasterxml.jackson.annotation.JsonIgnore; -import jakarta.persistence.*; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.util.Objects; +import javax.persistence.*; import java.util.Date; import java.util.ArrayList; import java.util.List; -import java.util.Objects; @Entity -public class _Order { +@Table(name = "tab_order") +public class Order { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @@ -36,12 +42,12 @@ public class _Order { public Long getId(){ return id; } - public _Order(Date dateOfOrder) { + public Order(Date dateOfOrder) { this.dateOfOrder = dateOfOrder; products = new ArrayList<>(); } - public _Order() { + public Order() { } public Date getDateOfOrder() { @@ -81,7 +87,7 @@ public class _Order { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - _Order order = (_Order) o; + Order order = (Order) o; if(!Objects.equals(id, order.id)) return false; if(!Objects.equals(dateOfOrder.toString(), order.dateOfOrder.toString())) return false; diff --git a/demo/src/main/java/com/example/demo/supply/Order/OrderController.java b/demo/src/main/java/com/example/demo/supply/Order/OrderController.java index e5756fa..baafe05 100644 --- a/demo/src/main/java/com/example/demo/supply/Order/OrderController.java +++ b/demo/src/main/java/com/example/demo/supply/Order/OrderController.java @@ -11,7 +11,7 @@ import java.util.List; @RestController @CrossOrigin -@RequestMapping("/order") +@RequestMapping("/api/order") public class OrderController { private OrderService orderService; @@ -34,15 +34,10 @@ public class OrderController { return orderService.findAllOrderProducts(id).stream().map(ProductDto::new).toList(); } -// @PostMapping("/someSuppliers/") -// public List getSomeSuppliers(@RequestBody() List products) { -// return orderService.suppliers(products).stream().map(SupplierDto::new).toList(); -// } - -// @PostMapping("/someSuppliers/") -// public List getSomeSuppliers(@RequestBody() List products) { -// return products; -// } + @GetMapping("/someSuppliers/{id}") + public List getSomeSuppliers(@PathVariable Long id) { + return orderService.suppliers(id).stream().map(SupplierDto::new).toList(); + } @PostMapping("/") public Long createOrder(@RequestParam() Long supplierId) { diff --git a/demo/src/main/java/com/example/demo/supply/Order/OrderDto.java b/demo/src/main/java/com/example/demo/supply/Order/OrderDto.java index 970e3c4..f599dbd 100644 --- a/demo/src/main/java/com/example/demo/supply/Order/OrderDto.java +++ b/demo/src/main/java/com/example/demo/supply/Order/OrderDto.java @@ -14,7 +14,9 @@ public class OrderDto { private Supplier supplier; private List products; - public OrderDto(_Order order){ + public OrderDto(){ + } + public OrderDto(Order order){ this.id = order.getId(); this.dateOfOrder = order.getDateOfOrder(); this.supplier = order.getSupplier(); @@ -36,4 +38,15 @@ public class OrderDto { public List getProducts() { return products; } + public void setDateOfOrder(Date dateOfOrder) { + this.dateOfOrder = dateOfOrder; + } + + public void setSupplier(Supplier supplier) { + this.supplier = supplier; + } + + public void setProducts(List products) { + this.products = products; + } } diff --git a/demo/src/main/java/com/example/demo/supply/Order/OrderMvcController.java b/demo/src/main/java/com/example/demo/supply/Order/OrderMvcController.java new file mode 100644 index 0000000..8eed3e9 --- /dev/null +++ b/demo/src/main/java/com/example/demo/supply/Order/OrderMvcController.java @@ -0,0 +1,72 @@ +package com.example.demo.supply.Order; + +import com.example.demo.supply.Product.ProductDto; +import com.example.demo.supply.Product.ProductService; +import com.example.demo.supply.Supplier.SupplierDto; +import com.example.demo.supply.Supplier.SupplierService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.List; + +@Controller +@RequestMapping("/order") +public class OrderMvcController { + private OrderService orderService; + private ProductService productService; + private SupplierService supplierService; + + public OrderMvcController(OrderService orderService, + ProductService productService, + SupplierService supplierService){ + this.orderService = orderService; + this.productService = productService; + this.supplierService = supplierService; + } + + @GetMapping + public String getOrders(Model model) { + model.addAttribute("orders", + orderService.findAllOrders().stream() + .map(OrderDto::new) + .toList()); +// model.addAttribute("products", +// pro.findAllOrders().stream() +// .map(OrderDto::new) +// .toList()); + return "order"; + } + + @GetMapping("/{id}") + public String getOrder(@PathVariable Long id, + Model model) { + model.addAttribute("orderId", id); + model.addAttribute("orderDto", new OrderDto(orderService.findOrder(id))); + return "product-edit"; + } + + @PostMapping("/") + public String createOrder(@ModelAttribute @Valid OrderDto orderDto, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "order-add"; + } + + List suppliers =supplierService.findAllSuppliers().stream() + .map(SupplierDto::new).toList(); + + orderService.addOrder(orderDto.getSupplier().getId()); + return "redirect:/order"; + } +// +// @PostMapping("/delete/{id}") +// public String deleteProduct(@PathVariable Long id) { +// orderService.deleteProduct(id); +// return "redirect:/product"; +// } +} diff --git a/demo/src/main/java/com/example/demo/supply/Order/OrderRepository.java b/demo/src/main/java/com/example/demo/supply/Order/OrderRepository.java index 7a108b4..92d54db 100644 --- a/demo/src/main/java/com/example/demo/supply/Order/OrderRepository.java +++ b/demo/src/main/java/com/example/demo/supply/Order/OrderRepository.java @@ -7,8 +7,7 @@ import org.springframework.data.jpa.repository.Query; import java.util.List; -public interface OrderRepository extends JpaRepository<_Order, Long> { - @Query("SELECT distinct o.supplier FROM _Order o join Product p where p in (?1)") - List getSomeSuppliers(List products); - +public interface OrderRepository extends JpaRepository { + @Query("SELECT distinct o.supplier FROM Order o join Product p where p = ?1") + List getSomeSuppliers(Product product); } diff --git a/demo/src/main/java/com/example/demo/supply/Order/OrderService.java b/demo/src/main/java/com/example/demo/supply/Order/OrderService.java index c1483d2..c75929d 100644 --- a/demo/src/main/java/com/example/demo/supply/Order/OrderService.java +++ b/demo/src/main/java/com/example/demo/supply/Order/OrderService.java @@ -28,58 +28,52 @@ public class OrderService { } @Transactional - public _Order addOrder(Long supplierId){ - final _Order order = new _Order(new Date()); + public Order addOrder(Long supplierId){ + final Order order = new Order(new Date()); order.setSupplier(supplierService.findSupplier(supplierId)); return orderRepository.save(order); } - //поставщики, у которых есть заказ на конкретный товар или несколько товаров -// @Transactional -// public List suppliers(List products){ -// return em.createQuery("SELECT distinct o.supplier FROM Orders o join Product p where p in (:products) ", Supplier.class) -// .setParameter("products", products).getResultList(); -// } - @Transactional - public List suppliers(List products){ - return orderRepository.getSomeSuppliers(products); + public List suppliers(Long productId){ + final Product product = productService.findProduct(productId); + return orderRepository.getSomeSuppliers(product); } @Transactional - public _Order addProduct(Long id, Long productId) { - final _Order currentOrder = findOrder(id); + public Order addProduct(Long id, Long productId) { + final Order currentOrder = findOrder(id); currentOrder.addProduct(productService.findProduct(productId)); return orderRepository.save(currentOrder); } @Transactional - public _Order removeProduct(Long id, Long productId) { - final _Order currentOrder = findOrder(id); + public Order removeProduct(Long id, Long productId) { + final Order currentOrder = findOrder(id); currentOrder.addProduct(productService.findProduct(productId)); return orderRepository.save(currentOrder); } @Transactional(readOnly = true) - public _Order findOrder(Long id) { - final Optional<_Order> order = orderRepository.findById(id); + public Order findOrder(Long id) { + final Optional order = orderRepository.findById(id); return order.orElseThrow(() -> new OrderNotFoundException(id)); } @Transactional(readOnly = true) - public List<_Order> findAllOrders() { + public List findAllOrders() { return orderRepository.findAll(); } @Transactional(readOnly = true) public List findAllOrderProducts(Long orderId) { - final Optional<_Order> order = orderRepository.findById(orderId); + final Optional order = orderRepository.findById(orderId); return order.orElseThrow(() -> new OrderNotFoundException(orderId)).getProducts(); } @Transactional - public _Order deleteOrder(Long id) { - final _Order currentOrder = findOrder(id); + public Order deleteOrder(Long id) { + final Order currentOrder = findOrder(id); orderRepository.delete(currentOrder); return currentOrder; } diff --git a/demo/src/main/java/com/example/demo/supply/Product/Product.java b/demo/src/main/java/com/example/demo/supply/Product/Product.java index 9ec18b7..3fae949 100644 --- a/demo/src/main/java/com/example/demo/supply/Product/Product.java +++ b/demo/src/main/java/com/example/demo/supply/Product/Product.java @@ -1,12 +1,13 @@ package com.example.demo.supply.Product; -import com.example.demo.supply.Order._Order; +import com.example.demo.supply.Order.Order; import com.fasterxml.jackson.annotation.JsonIgnore; -import jakarta.persistence.*; + +import javax.persistence.*; +import java.util.Objects; import java.util.ArrayList; import java.util.List; -import java.util.Objects; @Entity public class Product { @@ -22,7 +23,7 @@ public class Product { private double cost; @JsonIgnore @ManyToMany(fetch = FetchType.EAGER, mappedBy = "products") - private List<_Order> orders; + private List orders; public Product(){} @@ -48,11 +49,11 @@ public class Product { this.cost = cost; } - public List<_Order> getOrders() { + public List getOrders() { return orders; } - public void setOrders(List<_Order> orders) { + public void setOrders(List orders) { this.orders = orders; } diff --git a/demo/src/main/java/com/example/demo/supply/Product/ProductDto.java b/demo/src/main/java/com/example/demo/supply/Product/ProductDto.java index 3c4eaa4..e6e1961 100644 --- a/demo/src/main/java/com/example/demo/supply/Product/ProductDto.java +++ b/demo/src/main/java/com/example/demo/supply/Product/ProductDto.java @@ -1,6 +1,6 @@ package com.example.demo.supply.Product; -import com.example.demo.supply.Order._Order; +import com.example.demo.supply.Order.Order; import java.util.List; @@ -8,7 +8,7 @@ public class ProductDto { private long id; private String name; private double cost; - private List<_Order> orders; + private List orders; public ProductDto() {} @@ -28,7 +28,19 @@ public class ProductDto { public double getCost() { return cost; } - public List<_Order> getOrders() { + public List getOrders() { return orders; } + + public void setName(String name) { + this.name = name; + } + + public void setCost(double cost) { + this.cost = cost; + } + + public void setOrders(List orders) { + this.orders = orders; + } } diff --git a/demo/src/main/java/com/example/demo/supply/Product/ProductMvcController.java b/demo/src/main/java/com/example/demo/supply/Product/ProductMvcController.java index ce8ae14..fabb9af 100644 --- a/demo/src/main/java/com/example/demo/supply/Product/ProductMvcController.java +++ b/demo/src/main/java/com/example/demo/supply/Product/ProductMvcController.java @@ -1,11 +1,12 @@ package com.example.demo.supply.Product; -import jakarta.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; +import javax.validation.Valid; + @Controller @RequestMapping("/product") public class ProductMvcController { @@ -16,7 +17,9 @@ public class ProductMvcController { @GetMapping public String getProducts(Model model) { model.addAttribute("products", - productService.findAllProducts().stream().map(ProductDto::new).toList()); + productService.findAllProducts().stream() + .map(ProductDto::new) + .toList()); return "product"; } diff --git a/demo/src/main/java/com/example/demo/supply/Product/ProductService.java b/demo/src/main/java/com/example/demo/supply/Product/ProductService.java index 73f5cc6..6a7e5d9 100644 --- a/demo/src/main/java/com/example/demo/supply/Product/ProductService.java +++ b/demo/src/main/java/com/example/demo/supply/Product/ProductService.java @@ -1,6 +1,5 @@ package com.example.demo.supply.Product; -import com.example.demo.util.ValidatorUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/demo/src/main/java/com/example/demo/supply/Supplier/Supplier.java b/demo/src/main/java/com/example/demo/supply/Supplier/Supplier.java index cfa5678..dc984f9 100644 --- a/demo/src/main/java/com/example/demo/supply/Supplier/Supplier.java +++ b/demo/src/main/java/com/example/demo/supply/Supplier/Supplier.java @@ -1,13 +1,13 @@ package com.example.demo.supply.Supplier; -import com.example.demo.supply.Order._Order; -import com.example.demo.supply.Product.Product; +import com.example.demo.supply.Order.Order; import com.fasterxml.jackson.annotation.JsonIgnore; -import jakarta.persistence.*; + +import javax.persistence.*; +import java.util.Objects; import java.util.ArrayList; import java.util.List; -import java.util.Objects; @Entity public class Supplier { @@ -22,7 +22,7 @@ public class Supplier { @JsonIgnore @OneToMany(fetch = FetchType.EAGER, mappedBy = "supplier", cascade = CascadeType.REMOVE) - private List<_Order> orders; + private List orders; public Supplier(){} @@ -49,11 +49,11 @@ public class Supplier { this.license = license; } - public List<_Order> getOrders() { + public List getOrders() { return orders; } - public void setOrders(List<_Order> orders) { + public void setOrders(List orders) { this.orders = orders; } diff --git a/demo/src/main/java/com/example/demo/supply/Supplier/SupplierDto.java b/demo/src/main/java/com/example/demo/supply/Supplier/SupplierDto.java index 168a5af..29ff747 100644 --- a/demo/src/main/java/com/example/demo/supply/Supplier/SupplierDto.java +++ b/demo/src/main/java/com/example/demo/supply/Supplier/SupplierDto.java @@ -1,6 +1,6 @@ package com.example.demo.supply.Supplier; -import com.example.demo.supply.Order._Order; +import com.example.demo.supply.Order.Order; import java.util.List; @@ -8,7 +8,7 @@ public class SupplierDto { private Long id; private String name; private int license; - private List<_Order> orders; + private List orders; public SupplierDto(){ } @@ -27,7 +27,19 @@ public class SupplierDto { public int getLicense() { return license; } - public List<_Order> getOrders() { + public List getOrders() { return orders; } + + public void setName(String name) { + this.name = name; + } + + public void setLicense(int license) { + this.license = license; + } + + public void setOrders(List orders) { + this.orders = orders; + } } diff --git a/demo/src/main/java/com/example/demo/supply/Supplier/SupplierMvcController.java b/demo/src/main/java/com/example/demo/supply/Supplier/SupplierMvcController.java index ca85b71..9d7e33d 100644 --- a/demo/src/main/java/com/example/demo/supply/Supplier/SupplierMvcController.java +++ b/demo/src/main/java/com/example/demo/supply/Supplier/SupplierMvcController.java @@ -1,11 +1,12 @@ package com.example.demo.supply.Supplier; -import jakarta.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; +import javax.validation.Valid; + @Controller @RequestMapping("/supplier") public class SupplierMvcController { @@ -13,15 +14,17 @@ public class SupplierMvcController { public SupplierMvcController(SupplierService supplierService){ this.supplierService = supplierService;} - @GetMapping("/") - public String getProducts(Model model) { + @GetMapping + public String getSuppliers(Model model) { model.addAttribute("suppliers", - supplierService.findAllSuppliers().stream().map(SupplierDto::new).toList()); + supplierService.findAllSuppliers().stream() + .map(SupplierDto::new) + .toList()); return "supplier"; } @GetMapping(value = {"/edit", "/edit/{id}"}) - public String editProduct(@PathVariable(required = false) Long id, + public String editSuppliers(@PathVariable(required = false) Long id, Model model) { if (id == null || id <= 0) { model.addAttribute("supplierDto", new SupplierDto()); @@ -33,7 +36,7 @@ public class SupplierMvcController { } @PostMapping(value = {"", "/{id}"}) - public String saveProduct(@PathVariable(required = false) Long id, + public String saveSuppliers(@PathVariable(required = false) Long id, @ModelAttribute @Valid SupplierDto supplierDto, BindingResult bindingResult, Model model) { @@ -42,7 +45,7 @@ public class SupplierMvcController { return "supplier-edit"; } if (id == null || id <= 0) { - supplierService.addSupplier(supplierDto.getName() , supplierDto.getLicense()); + supplierService.addSupplier(supplierDto.getName(), supplierDto.getLicense()); } else { supplierService.updateSupplier(id, supplierDto.getName(), supplierDto.getLicense()); } @@ -50,7 +53,7 @@ public class SupplierMvcController { } @PostMapping("/delete/{id}") - public String deleteProduct(@PathVariable Long id) { + public String deleteSuppliers(@PathVariable Long id) { supplierService.deleteSupplier(id); return "redirect:/supplier"; } diff --git a/demo/src/main/java/com/example/demo/util/AdviceController.java b/demo/src/main/java/com/example/demo/util/AdviceController.java deleted file mode 100644 index 4a41668..0000000 --- a/demo/src/main/java/com/example/demo/util/AdviceController.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.example.demo.util; - -import com.example.demo.supply.Product.ProductNotFoundException; -import org.springframework.context.support.DefaultMessageSourceResolvable; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.MethodArgumentNotValidException; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; - -import java.util.stream.Collectors; - -@ControllerAdvice -public class AdviceController { - @ExceptionHandler({ - ProductNotFoundException.class, - ValidationException.class - }) - public ResponseEntity handleException(Throwable e) { - return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); - } - - @ExceptionHandler(MethodArgumentNotValidException.class) - public ResponseEntity handleBindException(MethodArgumentNotValidException e) { - final ValidationException validationException = new ValidationException( - e.getBindingResult().getAllErrors().stream() - .map(DefaultMessageSourceResolvable::getDefaultMessage) - .collect(Collectors.toSet())); - return handleException(validationException); - } - - @ExceptionHandler(Exception.class) - public ResponseEntity handleUnknownException(Throwable e) { - e.printStackTrace(); - return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); - } -} diff --git a/demo/src/main/java/com/example/demo/util/ValidationException.java b/demo/src/main/java/com/example/demo/util/ValidationException.java deleted file mode 100644 index ab9623e..0000000 --- a/demo/src/main/java/com/example/demo/util/ValidationException.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.example.demo.util; - -import java.util.Set; - -public class ValidationException extends RuntimeException { - public ValidationException(Set errors) { - super(String.join("\n", errors)); - } -} diff --git a/demo/src/main/java/com/example/demo/util/ValidatorUtil.java b/demo/src/main/java/com/example/demo/util/ValidatorUtil.java deleted file mode 100644 index 804a50e..0000000 --- a/demo/src/main/java/com/example/demo/util/ValidatorUtil.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.example.demo.util; - -import jakarta.validation.ConstraintViolation; -import jakarta.validation.Validation; -import jakarta.validation.Validator; -import jakarta.validation.ValidatorFactory; -import org.springframework.stereotype.Component; -import java.util.Set; -import java.util.stream.Collectors; - -@Component -public class ValidatorUtil { - private final Validator validator; - - public ValidatorUtil() { - try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) { - this.validator = factory.getValidator(); - } - } - - public void validate(T object) { - final Set> errors = validator.validate(object); - if (!errors.isEmpty()) { - throw new ValidationException(errors.stream() - .map(ConstraintViolation::getMessage) - .collect(Collectors.toSet())); - } - } -} diff --git a/demo/src/main/resources/application.properties b/demo/src/main/resources/application.properties index e1aa7de..14d2c47 100644 --- a/demo/src/main/resources/application.properties +++ b/demo/src/main/resources/application.properties @@ -1,5 +1,5 @@ spring.main.banner-mode=off -server.port=8081 +server.port=8080 spring.datasource.url=jdbc:h2:file:./data spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa diff --git a/demo/src/main/resources/templates/common/header.html b/demo/src/main/resources/templates/common/header.html new file mode 100644 index 0000000..566549b --- /dev/null +++ b/demo/src/main/resources/templates/common/header.html @@ -0,0 +1,10 @@ + + + + + Title + + + + + \ No newline at end of file diff --git a/demo/src/main/resources/templates/main.html b/demo/src/main/resources/templates/default.html similarity index 65% rename from demo/src/main/resources/templates/main.html rename to demo/src/main/resources/templates/default.html index 220a8ce..764a7bf 100644 --- a/demo/src/main/resources/templates/main.html +++ b/demo/src/main/resources/templates/default.html @@ -6,18 +6,16 @@ Поставки - - - - - - - + + + + +