устала -_-
This commit is contained in:
parent
44d9f2b935
commit
cf46765fb9
@ -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'
|
||||
|
@ -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() {
|
||||
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("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH", "OPTIONS");
|
||||
}
|
||||
};
|
||||
registry.addMapping("/**").allowedMethods("*");
|
||||
}
|
||||
}
|
@ -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;
|
@ -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<SupplierDto> getSomeSuppliers(@RequestBody() List<Product> products) {
|
||||
// return orderService.suppliers(products).stream().map(SupplierDto::new).toList();
|
||||
// }
|
||||
|
||||
// @PostMapping("/someSuppliers/")
|
||||
// public List<ProductDto> getSomeSuppliers(@RequestBody() List<ProductDto> products) {
|
||||
// return products;
|
||||
// }
|
||||
@GetMapping("/someSuppliers/{id}")
|
||||
public List<SupplierDto> getSomeSuppliers(@PathVariable Long id) {
|
||||
return orderService.suppliers(id).stream().map(SupplierDto::new).toList();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public Long createOrder(@RequestParam() Long supplierId) {
|
||||
|
@ -14,7 +14,9 @@ public class OrderDto {
|
||||
private Supplier supplier;
|
||||
private List<Product> 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<Product> getProducts() {
|
||||
return products;
|
||||
}
|
||||
public void setDateOfOrder(Date dateOfOrder) {
|
||||
this.dateOfOrder = dateOfOrder;
|
||||
}
|
||||
|
||||
public void setSupplier(Supplier supplier) {
|
||||
this.supplier = supplier;
|
||||
}
|
||||
|
||||
public void setProducts(List<Product> products) {
|
||||
this.products = products;
|
||||
}
|
||||
}
|
||||
|
@ -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<SupplierDto> 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";
|
||||
// }
|
||||
}
|
@ -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<Supplier> getSomeSuppliers(List<Product> products);
|
||||
|
||||
public interface OrderRepository extends JpaRepository<Order, Long> {
|
||||
@Query("SELECT distinct o.supplier FROM Order o join Product p where p = ?1")
|
||||
List<Supplier> getSomeSuppliers(Product product);
|
||||
}
|
||||
|
@ -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<Supplier> suppliers(List<Product> 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<Supplier> suppliers(List<Product> products){
|
||||
return orderRepository.getSomeSuppliers(products);
|
||||
public List<Supplier> 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> order = orderRepository.findById(id);
|
||||
return order.orElseThrow(() -> new OrderNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<_Order> findAllOrders() {
|
||||
public List<Order> findAllOrders() {
|
||||
return orderRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Product> findAllOrderProducts(Long orderId) {
|
||||
final Optional<_Order> order = orderRepository.findById(orderId);
|
||||
final Optional<Order> 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;
|
||||
}
|
||||
|
@ -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<Order> orders;
|
||||
|
||||
public Product(){}
|
||||
|
||||
@ -48,11 +49,11 @@ public class Product {
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
public List<_Order> getOrders() {
|
||||
public List<Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(List<_Order> orders) {
|
||||
public void setOrders(List<Order> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
|
@ -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<Order> orders;
|
||||
|
||||
public ProductDto() {}
|
||||
|
||||
@ -28,7 +28,19 @@ public class ProductDto {
|
||||
public double getCost() {
|
||||
return cost;
|
||||
}
|
||||
public List<_Order> getOrders() {
|
||||
public List<Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setCost(double cost) {
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
public void setOrders(List<Order> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
}
|
||||
|
@ -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";
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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<Order> orders;
|
||||
|
||||
public Supplier(){}
|
||||
|
||||
@ -49,11 +49,11 @@ public class Supplier {
|
||||
this.license = license;
|
||||
}
|
||||
|
||||
public List<_Order> getOrders() {
|
||||
public List<Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(List<_Order> orders) {
|
||||
public void setOrders(List<Order> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
|
@ -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<Order> orders;
|
||||
|
||||
public SupplierDto(){
|
||||
}
|
||||
@ -27,7 +27,19 @@ public class SupplierDto {
|
||||
public int getLicense() {
|
||||
return license;
|
||||
}
|
||||
public List<_Order> getOrders() {
|
||||
public List<Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setLicense(int license) {
|
||||
this.license = license;
|
||||
}
|
||||
|
||||
public void setOrders(List<Order> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
}
|
||||
|
@ -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";
|
||||
}
|
||||
|
@ -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<Object> handleException(Throwable e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<Object> 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<Object> handleUnknownException(Throwable e) {
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package com.example.demo.util;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class ValidationException extends RuntimeException {
|
||||
public ValidationException(Set<String> errors) {
|
||||
super(String.join("\n", errors));
|
||||
}
|
||||
}
|
@ -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 <T> void validate(T object) {
|
||||
final Set<ConstraintViolation<T>> errors = validator.validate(object);
|
||||
if (!errors.isEmpty()) {
|
||||
throw new ValidationException(errors.stream()
|
||||
.map(ConstraintViolation::getMessage)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
|
10
demo/src/main/resources/templates/common/header.html
Normal file
10
demo/src/main/resources/templates/common/header.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
@ -6,18 +6,16 @@
|
||||
<meta charset="UTF-8"/>
|
||||
<title>Поставки</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<!-- <link rel="icon" href="/">-->
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" src="./webjars/bootstrap"></script>
|
||||
<link rel="stylesheet" href="./webjars/bootstrap"/>
|
||||
<link rel="stylesheet" href="./webjars/font-awesome"/>
|
||||
<link rel="stylesheet" href="../public/favicon.svg"/>
|
||||
<link rel="icon" href="/favicon.svg">
|
||||
<script type="text/javascript" src="/webjars/bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
|
||||
<link rel="stylesheet" href="/webjars/bootstrap/5.1.3/css/bootstrap.min.css"/>
|
||||
<link rel="stylesheet" href="/webjars/font-awesome/6.1.0/css/all.min.css"/>
|
||||
<link rel="stylesheet" href="/css/style.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand">
|
||||
<a class="navbar-brand" href="/">
|
||||
<i class="fa-solid fa-font-awesome"></i>
|
||||
Поставки
|
||||
</a>
|
||||
@ -28,10 +26,14 @@
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav" th:with="activeLink=${#request.requestURI}">
|
||||
<a class="nav-link" href="/"
|
||||
th:classappend="${#strings.equals(activeLink, '/')} ? 'active' : ''">Главная</a>
|
||||
<a class="nav-link" href="/product"
|
||||
th:classappend="${#strings.equals(activeLink, '/product')} ? 'active' : ''">Продукт</a>
|
||||
th:classappend="${#strings.equals(activeLink, '/product')} ? 'active' : ''">Продукты</a>
|
||||
<a class="nav-link" href="/supplier"
|
||||
th:classappend="${#strings.equals(activeLink, '/supplier')} ? 'active' : ''">Поставщик</a>
|
||||
th:classappend="${#strings.equals(activeLink, '/supplier')} ? 'active' : ''">Поставщики</a>
|
||||
<a class="nav-link" href="/order"
|
||||
th:classappend="${#strings.equals(activeLink, '/order')} ? 'active' : ''">Заказы</a>
|
||||
<a class="nav-link" href="/swagger-ui/index.html" target="_blank">Документация REST API</a>
|
||||
<a class="nav-link" href="/h2-console/" target="_blank">Консоль H2</a>
|
||||
</ul>
|
@ -1,13 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{main}">
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<div>It's works!</div>
|
||||
<a href="123">ERROR</a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
135
demo/src/main/resources/templates/order-add.html
Normal file
135
demo/src/main/resources/templates/order-add.html
Normal file
@ -0,0 +1,135 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content" class="mw-100">
|
||||
<form action="#" th:action="@{/car/{id}(id=${id})}" method="post" class="mb-3">
|
||||
<div class="d-flex justify-content-center">
|
||||
|
||||
<div style="margin-left: 2vw;">
|
||||
<button type="submit" class="btn btn-outline-dark text-center button-fixed">
|
||||
<span>Создать</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div style="margin-left: 2vw;">
|
||||
<a class="btn btn-outline-dark text-center button-fixed" th:href="@{/order}">
|
||||
Отмена
|
||||
</a>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div class="d-flex container-fluid ">
|
||||
<div style="width: 45vw; margin-right: 2vw" class="container-fluid">
|
||||
|
||||
<form action="#" th:action="@{/car/{id}(id=${id})}" method="post" >
|
||||
<input name="wpName" type="hidden" th:value="${name}" />
|
||||
<input name="PW" type="hidden" th:value="W" />
|
||||
<div class="mb-3">
|
||||
<label for="idW" class="form-label">Покупатель</label>
|
||||
<select class="form-select" id = "idW" th:name="idPW">
|
||||
<option th:each="value: ${buyers}"
|
||||
th:value="${value.id}"
|
||||
th:text="${value.buyerFirstName} + ' ' + ${value.buyerSecondName}">
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<button type="submit" class="btn btn-outline-dark text-center button-fixed">
|
||||
<span>Добавить продукт</span>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<table class="table" >
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">id</th>
|
||||
<th scope="col">Название</th>
|
||||
<th scope="col">Цена</th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="product, iterator: ${carBuyers}">
|
||||
<td th:text="${iterator.index} + 1"></td>
|
||||
<td th:text="${product.name}"></td>
|
||||
<td th:text="${product.cost}"></td>
|
||||
<td>
|
||||
<div>
|
||||
<a type="button" class="btn btn-outline-dark text-center button-fixed"
|
||||
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${product.id}').click()|">
|
||||
<i class="fa fa-trash"></i>
|
||||
</a>
|
||||
</div>
|
||||
<form th:action="@{'/car/' + ${id} + '?PW=W&wpName='+ ${name} +'&idPW=' + ${product.id} + '&delete=true'}" method="post">
|
||||
<button th:id="'remove-' + ${product.id}" type="submit" style="display: none">
|
||||
Удалить
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
</div>
|
||||
<div style="width: 45vw; margin-left: 2vw" class="container-fluid">
|
||||
<form action="#" th:action="@{/car/{id}(id=${id})}" method="post" class="mb-3">
|
||||
<input name="wpName" type="hidden" th:value="${name}" />
|
||||
<input type="hidden" th:value="P" name="PW"/>
|
||||
<div class="mb-3">
|
||||
<label for="idP" class="form-label">Магазин</label>
|
||||
<select class="form-select" id = "idP" th:name="idPW">
|
||||
<option th:each="store, iterator: ${stores}"
|
||||
th:value="${store.id}"
|
||||
th:text="${store.storeName}">
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<button type="submit" class="btn btn-outline-dark text-center button-fixed">
|
||||
<span>Добавить</span>
|
||||
</button>
|
||||
|
||||
</div>
|
||||
</form>
|
||||
<table class="table" id="tbl-items">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">id</th>
|
||||
<th scope="col">Название</th>
|
||||
<th scope="col">Цена</th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="carStore, iterator: ${carStores}">
|
||||
<td th:text="${iterator.index} + 1"></td>
|
||||
<td th:text="${carStore.storeName}"></td>
|
||||
<td>
|
||||
<div>
|
||||
<a type="button" class="btn btn-outline-dark text-center button-fixed"
|
||||
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${carStore.id}').click()|">
|
||||
<i class="fa fa-trash"></i>
|
||||
</a>
|
||||
</div>
|
||||
<form th:action="@{'/car/' + ${id} + '?PW=P&wpName='+ ${name} + '&idPW=' + ${carStore.id} + '&delete=true'}" method="post">
|
||||
<button class = "btn btn-outline-dark text-center button-fixed" th:id="'remove-' + ${carStore.id}" type="submit" style="display: none">
|
||||
Удалить
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
49
demo/src/main/resources/templates/order.html
Normal file
49
demo/src/main/resources/templates/order.html
Normal file
@ -0,0 +1,49 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<div>
|
||||
<a class="btn btn-success button-fixed"
|
||||
th:href="@{/order/}">
|
||||
<i class="fa-solid fa-plus"></i> Создать заказ
|
||||
</a>
|
||||
</div>
|
||||
<div class="table-responsive">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">#</th>
|
||||
<th scope="col">ID</th>
|
||||
<th scope="col">Дата создания</th>
|
||||
<th scope="col">Поставщик</th>
|
||||
<th scope="col">Продукты</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="product, iterator: ${orders}">
|
||||
<th scope="row" th:text="${iterator.index} + 1"/>
|
||||
<td th:text="${order.id}"/>
|
||||
<td th:text="${order.dateOfOrder}" />
|
||||
<td th:text="${order.supplier}" />
|
||||
<td>
|
||||
<li th:each="product : ${order.products}" th:text="${product.name}"></li>
|
||||
|
||||
<!-- <select class="form-select" size="3" aria-label="size 3 select example">-->
|
||||
<!-- <option selected>Open this select menu</option>-->
|
||||
<!-- <option value="1">One</option>-->
|
||||
<!-- <option value="2">Two</option>-->
|
||||
<!-- <option value="3">Three</option>-->
|
||||
<!-- </select>-->
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -1,12 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{main}">
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<!-- <div th:text="${errors}" class="margin-bottom alert-danger"></div>-->
|
||||
<form action="#" th:action="@{/product/{id}(id=${id})}" th:object="${productDto}" method="post">
|
||||
<div class="mb-3">
|
||||
<label for="name" class="form-label">Название</label>
|
||||
|
@ -1,7 +1,7 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{main}">
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
@ -28,7 +28,7 @@
|
||||
<th scope="row" th:text="${iterator.index} + 1"/>
|
||||
<td th:text="${product.id}"/>
|
||||
<td th:text="${product.name}" />
|
||||
<td th:text="${product.cost}"/>
|
||||
<td th:text="${product.cost}" />
|
||||
<td style="width: 10%">
|
||||
<div class="btn-group" role="group" aria-label="Basic example">
|
||||
<a class="btn btn-warning button-fixed button-sm"
|
||||
|
@ -1,27 +1,26 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{main}">
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<!-- <div th:text="${errors}" class="margin-bottom alert-danger"></div>-->
|
||||
<form action="#" th:action="@{/product/{id}(id=${id})}" th:object="${productDto}" method="post">
|
||||
<form action="#" th:action="@{/supplier/{id}(id=${id})}" th:object="${supplierDto}" method="post">
|
||||
<div class="mb-3">
|
||||
<label for="name" class="form-label">Название</label>
|
||||
<input type="text" class="form-control" id="name" th:field="${productDto.name}" required="true">
|
||||
<input type="text" class="form-control" id="name" th:field="${supplierDto.name}" required="true">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="cost" class="form-label">Лицензия</label>
|
||||
<input type="text" class="form-control" id="cost" th:field="${productDto.cost}" required="true">
|
||||
<label for="license" class="form-label">Лицензия</label>
|
||||
<input type="text" class="form-control" id="license" th:field="${supplierDto.license}" required="true">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<button type="submit" class="btn btn-primary button-fixed">
|
||||
<span th:if="${id == null}">Добавить</span>
|
||||
<span th:if="${id != null}">Обновить</span>
|
||||
</button>
|
||||
<a class="btn btn-secondary button-fixed" th:href="@{/product}">
|
||||
<a class="btn btn-secondary button-fixed" th:href="@{/supplier}">
|
||||
Назад
|
||||
</a>
|
||||
</div>
|
||||
|
@ -1,7 +1,7 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{main}">
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
@ -27,7 +27,7 @@
|
||||
<tr th:each="supplier, iterator: ${suppliers}">
|
||||
<th scope="row" th:text="${iterator.index} + 1"/>
|
||||
<td th:text="${supplier.id}"/>
|
||||
<td th:text="${supplier.name}" style="width: 60%"/>
|
||||
<td th:text="${supplier.name}"/>
|
||||
<td th:text="${supplier.license}"/>
|
||||
<td style="width: 10%">
|
||||
<div class="btn-group" role="group" aria-label="Basic example">
|
||||
|
Loading…
Reference in New Issue
Block a user