From 258c8737b832f4ed18d6f245ed19331536b565ef Mon Sep 17 00:00:00 2001 From: Nikita Sergeev Date: Sun, 9 Apr 2023 14:04:16 +0400 Subject: [PATCH] backend lab4 --- .../shop/controller/ComponentController.java | 18 +-- .../labwork/shop/controller/ComponentDTO.java | 9 +- .../shop/controller/OrderController.java | 31 +--- .../ip/labwork/shop/controller/OrderDTO.java | 24 ++- .../shop/controller/ProductController.java | 33 +--- .../labwork/shop/controller/ProductDTO.java | 51 ++++-- .../java/ip/labwork/shop/model/Order.java | 1 - .../ip/labwork/shop/model/OrderProducts.java | 7 +- .../java/ip/labwork/shop/model/Product.java | 15 +- .../labwork/shop/model/ProductComponents.java | 1 - .../repository/OrderProductRepository.java | 1 - .../ProductComponentRepository.java | 1 - .../shop/service/ComponentService.java | 52 +++---- .../ip/labwork/shop/service/OrderService.java | 145 +++++++----------- .../labwork/shop/service/ProductService.java | 113 ++++---------- 15 files changed, 211 insertions(+), 291 deletions(-) diff --git a/src/main/java/ip/labwork/shop/controller/ComponentController.java b/src/main/java/ip/labwork/shop/controller/ComponentController.java index 055e68c..4cd5eed 100644 --- a/src/main/java/ip/labwork/shop/controller/ComponentController.java +++ b/src/main/java/ip/labwork/shop/controller/ComponentController.java @@ -1,6 +1,7 @@ package ip.labwork.shop.controller; import ip.labwork.shop.service.ComponentService; +import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -16,21 +17,18 @@ public class ComponentController { } @PostMapping - public ComponentDTO createComponent(@RequestParam("name") String name, - @RequestParam("price") Integer price) { - return new ComponentDTO(componentService.addComponent(name, price)); + public ComponentDTO createComponent(@RequestBody @Valid ComponentDTO componentDTO) { + return componentService.create(componentDTO); } @PutMapping("/{id}") - public ComponentDTO updateComponent(@PathVariable Long id, - @RequestParam("name") String name, - @RequestParam("price") Integer price) { - return new ComponentDTO(componentService.updateComponent(id, name, price)); + public ComponentDTO updateComponent(@PathVariable Long id,@RequestBody @Valid ComponentDTO componentDTO) { + return componentService.updateComponent(id,componentDTO); } @DeleteMapping("/{id}") public ComponentDTO removeComponent(@PathVariable Long id) { - return new ComponentDTO(componentService.deleteComponent(id)); + return componentService.deleteComponent(id); } @DeleteMapping @@ -45,8 +43,6 @@ public class ComponentController { @GetMapping public List findAllComponent() { - return componentService.findAllComponent().stream() - .map(ComponentDTO::new) - .toList(); + return componentService.findAllComponent(); } } \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/controller/ComponentDTO.java b/src/main/java/ip/labwork/shop/controller/ComponentDTO.java index ec261af..a4b4caf 100644 --- a/src/main/java/ip/labwork/shop/controller/ComponentDTO.java +++ b/src/main/java/ip/labwork/shop/controller/ComponentDTO.java @@ -3,9 +3,9 @@ package ip.labwork.shop.controller; import ip.labwork.shop.model.Component; public class ComponentDTO { - private final long id; - private final String componentName; - private final int price; + private long id; + private String componentName; + private int price; private int count = 0; public ComponentDTO(Component component) { this.id = component.getId(); @@ -19,6 +19,9 @@ public class ComponentDTO { this.count = count; } + public ComponentDTO() { + } + public long getId() { return id; } diff --git a/src/main/java/ip/labwork/shop/controller/OrderController.java b/src/main/java/ip/labwork/shop/controller/OrderController.java index 3a69a92..a24b2c6 100644 --- a/src/main/java/ip/labwork/shop/controller/OrderController.java +++ b/src/main/java/ip/labwork/shop/controller/OrderController.java @@ -1,8 +1,7 @@ package ip.labwork.shop.controller; -import ip.labwork.shop.service.ProductService; -import ip.labwork.shop.model.Order; import ip.labwork.shop.service.OrderService; +import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -11,33 +10,21 @@ import java.util.List; @RequestMapping("/order") public class OrderController { private final OrderService orderService; - private final ProductService productService; - public OrderController(OrderService orderService, ProductService productService) { + public OrderController(OrderService orderService) { this.orderService = orderService; - this.productService = productService; } @PostMapping - public OrderDTO createOrder(@RequestParam("date") String date, - @RequestParam("price") Integer price, - @RequestParam("count") Integer[] count, - @RequestParam("prod") Long[] prod){ - final Order order = orderService.addOrder(date, price); - orderService.addOrderProducts(orderService.findOrder(order.getId()), count, productService.findFiltredProducts(prod)); - return new OrderDTO(order); + public OrderDTO createOrder(@RequestBody @Valid OrderDTO orderDTO){ + return orderService.create(orderDTO); } @PutMapping("/{id}") - public OrderDTO updateOrder(@PathVariable Long id, - @RequestParam("date") String date, - @RequestParam("price") Integer price, - @RequestParam("count") Integer[] count, - @RequestParam("prod") Long[] prod){ - orderService.updateOrder(id, date, price, count, productService.findFiltredProducts(prod)); - return new OrderDTO(orderService.update(orderService.findOrder(id),orderService.getOrderProducts(orderService.findOrder(id)), orderService.getOrderProducts(orderService.findOrder(id)).stream().map(p -> p.getId().getProductId()).toList(), count, productService.findFiltredProducts(prod))); + public OrderDTO updateOrder(@PathVariable Long id, @RequestBody @Valid OrderDTO orderDTO){ + return orderService.update(id, orderDTO); } @DeleteMapping("/{id}") public OrderDTO removeOrder(@PathVariable Long id){ - return new OrderDTO(orderService.deleteOrder(id)); + return orderService.deleteOrder(id); } @DeleteMapping public void removeAllOrder(){ @@ -49,8 +36,6 @@ public class OrderController { } @GetMapping public List findAllOrder(){ - return orderService.findAllOrder().stream() - .map(OrderDTO::new) - .toList(); + return orderService.findAllOrder(); } } diff --git a/src/main/java/ip/labwork/shop/controller/OrderDTO.java b/src/main/java/ip/labwork/shop/controller/OrderDTO.java index 3ca5b5a..75895c6 100644 --- a/src/main/java/ip/labwork/shop/controller/OrderDTO.java +++ b/src/main/java/ip/labwork/shop/controller/OrderDTO.java @@ -7,15 +7,21 @@ import java.util.List; import java.util.Objects; public class OrderDTO { - private final long id; - private final Date date; - private final int price; - private final List productDTOList; + private long id; + private Date date = new Date(); + private int price; + private List productDTOList; public OrderDTO(Order order) { this.id = order.getId(); this.date = order.getDate(); this.price = order.getPrice(); - this.productDTOList = order.getProducts().stream().filter(x -> Objects.equals(x.getId().getOrderId(), order.getId())).map(x -> new ProductDTO(x.getProduct())).toList(); + this.productDTOList = order.getProducts().stream() + .filter(x -> Objects.equals(x.getId().getOrderId(), order.getId())) + .map(y -> new ProductDTO(y.getProduct(), y.getCount())) + .toList(); + } + + public OrderDTO() { } public long getId() { @@ -30,6 +36,14 @@ public class OrderDTO { return price; } + public void setPrice(int price) { + this.price = price; + } + + public void setDate(Date date) { + this.date = date; + } + public List getProductDTOList() { return productDTOList; } diff --git a/src/main/java/ip/labwork/shop/controller/ProductController.java b/src/main/java/ip/labwork/shop/controller/ProductController.java index 6de4fcb..4199caf 100644 --- a/src/main/java/ip/labwork/shop/controller/ProductController.java +++ b/src/main/java/ip/labwork/shop/controller/ProductController.java @@ -1,8 +1,7 @@ package ip.labwork.shop.controller; import ip.labwork.shop.service.ProductService; -import ip.labwork.shop.model.Product; -import ip.labwork.shop.service.ComponentService; +import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -11,36 +10,22 @@ import java.util.List; @RequestMapping("/product") public class ProductController { private final ProductService productService; - private final ComponentService componentService; - public ProductController(ProductService productService, ComponentService componentService) { + public ProductController(ProductService productService) { this.productService = productService; - this.componentService = componentService; } @PostMapping - public ProductDTO createProduct(@RequestParam("name") String name, - @RequestParam("price") Integer price, - @RequestParam("count") Integer[] count, - @RequestParam("comp") Long[] comp){ - final Product product = productService.addProduct(name, price); - productService.addProductComponents(productService.findProduct(product.getId()), count, componentService.findFiltredComponents(comp)); - return new ProductDTO(productService.findProduct(product.getId())); + public ProductDTO createProduct(@RequestBody @Valid ProductDTO productDTO){ + return productService.create(productDTO); } @PutMapping("/{id}") - public ProductDTO updateProduct(@PathVariable Long id, - @RequestParam("name") String name, - @RequestParam("price") Integer price, - @RequestParam("count") Integer[] count, - @RequestParam("comp") Long[] comp){ - productService.updateProduct(id, name, price, count, componentService.findFiltredComponents(comp)); - return new ProductDTO(productService.update(productService.findProduct(id),productService.getProductComponents(productService.findProduct(id)), productService.getProductComponents(productService.findProduct(id)).stream().map(p -> p.getId().getComponentId()).toList(), count, componentService.findFiltredComponents(comp))); + public ProductDTO updateProduct(@PathVariable Long id,@RequestBody @Valid ProductDTO productDTO){ + return productService.updateProduct(id, productDTO); } @DeleteMapping("/{id}") public ProductDTO removeProduct(@PathVariable Long id){ - ProductDTO temp = new ProductDTO(productService.deleteProduct(id)); - productService.test(); - return null; + return productService.deleteProduct(id); } @DeleteMapping public void removeAllProduct(){ @@ -52,8 +37,6 @@ public class ProductController { } @GetMapping public List findAllProduct(){ - return productService.findAllProduct().stream() - .map(ProductDTO::new) - .toList(); + return productService.findAllProduct(); } } diff --git a/src/main/java/ip/labwork/shop/controller/ProductDTO.java b/src/main/java/ip/labwork/shop/controller/ProductDTO.java index 2815b2c..ee9d337 100644 --- a/src/main/java/ip/labwork/shop/controller/ProductDTO.java +++ b/src/main/java/ip/labwork/shop/controller/ProductDTO.java @@ -1,36 +1,57 @@ package ip.labwork.shop.controller; import ip.labwork.shop.model.Product; -import ip.labwork.shop.model.ProductComponents; -import java.util.HashMap; import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; public class ProductDTO { - private final long id; - private final String productName; - private final int price; - private final List componentDTOList; - private final List orderDTOList; + private long id; + private String name; + private int price; + private List componentDTOList; + private List orderDTOList; + private String image; + private int count; public ProductDTO(Product product) { this.id = product.getId(); - this.productName = product.getProductName(); + this.name = product.getProductName(); this.price = product.getPrice(); + this.image = product.getImage() == null? "" : new String(product.getImage()); this.componentDTOList = product.getComponents().stream() .filter(x -> Objects.equals(x.getId().getProductId(), product.getId())) .map(y -> new ComponentDTO(y.getComponent(), y.getCount())) .toList(); this.orderDTOList = product.getOrders() == null ? null : product.getOrders().stream().filter(x -> Objects.equals(x.getId().getProductId(), product.getId())).map(x -> new OrderDTO(x.getOrder())).toList(); } + public ProductDTO(Product product, int count) { + this.id = product.getId(); + this.name = product.getProductName(); + this.price = product.getPrice(); + this.image = product.getImage() == null? "" : new String(product.getImage()); + this.componentDTOList = product.getComponents().stream() + .filter(x -> Objects.equals(x.getId().getProductId(), product.getId())) + .map(y -> new ComponentDTO(y.getComponent(), y.getCount())) + .toList(); + this.count = count; + } + public ProductDTO() { + } public long getId() { return id; } - public String getProductName() { - return productName; + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public String getName() { + return name; } public int getPrice() { @@ -41,6 +62,14 @@ public class ProductDTO { return componentDTOList; } + public String getImage() { + return image; + } + + public void setImage(String image) { + this.image = image; + } + public List getOrderDTOList() { return orderDTOList; } diff --git a/src/main/java/ip/labwork/shop/model/Order.java b/src/main/java/ip/labwork/shop/model/Order.java index ce86872..9163f52 100644 --- a/src/main/java/ip/labwork/shop/model/Order.java +++ b/src/main/java/ip/labwork/shop/model/Order.java @@ -1,7 +1,6 @@ package ip.labwork.shop.model; import jakarta.persistence.*; -import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import java.util.ArrayList; diff --git a/src/main/java/ip/labwork/shop/model/OrderProducts.java b/src/main/java/ip/labwork/shop/model/OrderProducts.java index 021681a..9696cb3 100644 --- a/src/main/java/ip/labwork/shop/model/OrderProducts.java +++ b/src/main/java/ip/labwork/shop/model/OrderProducts.java @@ -2,19 +2,18 @@ package ip.labwork.shop.model; import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; -import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; @Entity @Table(name = "order_product") public class OrderProducts { @EmbeddedId - private OrderProductsKey id; - @ManyToOne + private OrderProductsKey id = new OrderProductsKey(); + @ManyToOne(cascade = CascadeType.MERGE) @MapsId("productId") @JoinColumn(name = "product_id") private Product product; - @ManyToOne + @ManyToOne(cascade = CascadeType.MERGE) @MapsId("orderId") @JoinColumn(name = "order_id") @JsonIgnore diff --git a/src/main/java/ip/labwork/shop/model/Product.java b/src/main/java/ip/labwork/shop/model/Product.java index f808497..7c07bbb 100644 --- a/src/main/java/ip/labwork/shop/model/Product.java +++ b/src/main/java/ip/labwork/shop/model/Product.java @@ -21,7 +21,9 @@ public class Product { @NotNull(message = "Price can't be null or empty") @Column(name = "price") private Integer price; - + @Lob + @Column(name = "image") + private byte[] image; @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER) private List components; @@ -34,9 +36,10 @@ public class Product { } - public Product(String productName, Integer price) { + public Product(String productName, Integer price, byte[] image) { this.productName = productName; this.price = price; + this.image = image; } public Long getId() { @@ -63,6 +66,14 @@ public class Product { return components; } + public byte[] getImage() { + return image; + } + + public void setImage(byte[] image) { + this.image = image; + } + public void setComponents(List components) { this.components = components; } diff --git a/src/main/java/ip/labwork/shop/model/ProductComponents.java b/src/main/java/ip/labwork/shop/model/ProductComponents.java index a946f89..8287a17 100644 --- a/src/main/java/ip/labwork/shop/model/ProductComponents.java +++ b/src/main/java/ip/labwork/shop/model/ProductComponents.java @@ -2,7 +2,6 @@ package ip.labwork.shop.model; import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; -import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; @Entity diff --git a/src/main/java/ip/labwork/shop/repository/OrderProductRepository.java b/src/main/java/ip/labwork/shop/repository/OrderProductRepository.java index 0d97c1d..976c32c 100644 --- a/src/main/java/ip/labwork/shop/repository/OrderProductRepository.java +++ b/src/main/java/ip/labwork/shop/repository/OrderProductRepository.java @@ -2,7 +2,6 @@ package ip.labwork.shop.repository; import ip.labwork.shop.model.OrderProducts; import ip.labwork.shop.model.OrderProductsKey; -import ip.labwork.shop.model.ProductComponents; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; diff --git a/src/main/java/ip/labwork/shop/repository/ProductComponentRepository.java b/src/main/java/ip/labwork/shop/repository/ProductComponentRepository.java index 46eb49e..0a2fbaa 100644 --- a/src/main/java/ip/labwork/shop/repository/ProductComponentRepository.java +++ b/src/main/java/ip/labwork/shop/repository/ProductComponentRepository.java @@ -3,7 +3,6 @@ package ip.labwork.shop.repository; import ip.labwork.shop.model.ProductComponents; import ip.labwork.shop.model.ProductComponentsKey; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; import java.util.List; diff --git a/src/main/java/ip/labwork/shop/service/ComponentService.java b/src/main/java/ip/labwork/shop/service/ComponentService.java index 73cb4a8..9d41ce7 100644 --- a/src/main/java/ip/labwork/shop/service/ComponentService.java +++ b/src/main/java/ip/labwork/shop/service/ComponentService.java @@ -1,5 +1,6 @@ package ip.labwork.shop.service; +import ip.labwork.shop.controller.ComponentDTO; import ip.labwork.shop.model.Component; import ip.labwork.shop.model.ProductComponents; import ip.labwork.shop.repository.ComponentRepository; @@ -8,8 +9,6 @@ import ip.labwork.util.validation.ValidatorUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -28,49 +27,40 @@ public class ComponentService { } @Transactional - public Component addComponent(String componentName, Integer price) { - final Component component = new Component(componentName, price); + public ComponentDTO create(ComponentDTO componentDTO) { + final Component component = new Component(componentDTO.getComponentName(), componentDTO.getPrice()); validatorUtil.validate(component); - return componentRepository.save(component); + return new ComponentDTO(componentRepository.save(component)); } - @Transactional(readOnly = true) public Component findComponent(Long id) { - final Optional student = componentRepository.findById(id); - return student.orElseThrow(() -> new ComponentNotFoundException(id)); + final Optional component = componentRepository.findById(id); + return component.orElseThrow(() -> new ComponentNotFoundException(id)); } - @Transactional(readOnly = true) - public List findAllComponent() { - return componentRepository.findAll(); + public List findAllComponent() { + return componentRepository.findAll().stream().map(x -> new ComponentDTO(x)).toList(); } - - @Transactional(readOnly = true) - public List findFiltredComponents(Long[] arr) { - return componentRepository.findAllById(Arrays.stream(arr).toList()); - } - @Transactional - public Component updateComponent(Long id, String componentName, Integer price) { + public ComponentDTO updateComponent(Long id, ComponentDTO component) { final Component currentComponent = findComponent(id); - currentComponent.setComponentName(componentName); - currentComponent.setPrice(price); + currentComponent.setComponentName(component.getComponentName()); + currentComponent.setPrice(component.getPrice()); validatorUtil.validate(currentComponent); - return componentRepository.save(currentComponent); + return new ComponentDTO(componentRepository.save(currentComponent)); } - @Transactional - public Component deleteComponent(Long id) { + public ComponentDTO deleteComponent(Long id) { final Component currentComponent = findComponent(id); int size = currentComponent.getProducts().size(); for (int i = 0; i < size; i++) { - ProductComponents temp = currentComponent.getProducts().get(0); - temp.getComponent().removeProduct(temp); - temp.getProduct().removeComponent(temp); - productComponentRepository.delete(temp); + ProductComponents productComponents = currentComponent.getProducts().get(0); + productComponents.getComponent().removeProduct(productComponents); + productComponents.getProduct().removeComponent(productComponents); + productComponentRepository.delete(productComponents); } componentRepository.delete(currentComponent); - return currentComponent; + return new ComponentDTO(currentComponent); } @Transactional public void deleteAllComponent() { @@ -78,8 +68,4 @@ public class ComponentService { productComponentRepository.deleteAll(); componentRepository.deleteAll(); } - - public void test() { - int s =5; - } -} +} \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/service/OrderService.java b/src/main/java/ip/labwork/shop/service/OrderService.java index 113c852..63ef9fd 100644 --- a/src/main/java/ip/labwork/shop/service/OrderService.java +++ b/src/main/java/ip/labwork/shop/service/OrderService.java @@ -1,147 +1,116 @@ package ip.labwork.shop.service; +import ip.labwork.shop.controller.OrderDTO; import ip.labwork.shop.model.*; import ip.labwork.shop.repository.OrderProductRepository; import ip.labwork.shop.repository.OrderRepository; +import ip.labwork.shop.repository.ProductRepository; import ip.labwork.util.validation.ValidatorUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.text.SimpleDateFormat; import java.util.*; @Service public class OrderService { private final OrderRepository orderRepository; private final OrderProductRepository orderProductRepository; + private final ProductRepository productRepository; private final ValidatorUtil validatorUtil; public OrderService(OrderRepository orderRepository, - ValidatorUtil validatorUtil, OrderProductRepository orderProductRepository) { + ValidatorUtil validatorUtil, OrderProductRepository orderProductRepository, ProductRepository productRepository) { this.orderRepository = orderRepository; this.validatorUtil = validatorUtil; this.orderProductRepository = orderProductRepository; + this.productRepository = productRepository; } @Transactional - public Order addOrder(String date, Integer price) { - Date correctDate = getDate(date); - final Order order = new Order(correctDate, price); + public OrderDTO create(OrderDTO orderDTO) { + int price = 0; + for(int i = 0; i < orderDTO.getProductDTOList().size(); i++){ + price += orderDTO.getProductDTOList().get(i).getPrice() * orderDTO.getProductDTOList().get(i).getCount(); + } + final Order order = new Order(new Date(), price); + validatorUtil.validate(order); + orderRepository.save(order); + for (int i = 0; i < orderDTO.getProductDTOList().size(); i++) { + final OrderProducts orderProducts = new OrderProducts(order, productRepository.findById(orderDTO.getProductDTOList().get(i).getId()).orElseThrow(() -> new ProductNotFoundException(1L)), orderDTO.getProductDTOList().get(i).getCount()); + orderProductRepository.saveAndFlush(orderProducts); + order.addProduct(orderProducts); + productRepository.findById(orderDTO.getProductDTOList().get(i).getId()).orElseThrow(() -> new ProductNotFoundException(1L)).addOrder(orderProducts); + orderProductRepository.saveAndFlush(orderProducts); + } + return new OrderDTO(findOrder(order.getId())); + } + @Transactional + public Order addOrder(OrderDTO orderDTO) { + int price = 0; + for(int i = 0; i < orderDTO.getProductDTOList().size(); i++){ + price += orderDTO.getProductDTOList().get(i).getPrice() * orderDTO.getProductDTOList().get(i).getCount(); + } + final Order order = new Order(new Date(), price); + orderDTO.setDate(order.getDate()); + orderDTO.setPrice(price); validatorUtil.validate(order); orderRepository.save(order); return order; } - @Transactional public void addOrderProducts(Order order, Integer[] count, List products){ for (int i = 0; i < products.size(); i++) { final OrderProducts orderProducts = new OrderProducts(order, products.get(i), count[i]); + orderProductRepository.saveAndFlush(orderProducts); order.addProduct(orderProducts); products.get(i).addOrder(orderProducts); - orderProductRepository.save(orderProducts); + orderProductRepository.saveAndFlush(orderProducts); } } - public Date getDate(String date) { - SimpleDateFormat format = new SimpleDateFormat(); - format.applyPattern("dd.MM.yyyy"); - Date newDate; - try { - newDate = format.parse(date); - } catch (Exception exception) { - newDate = new Date(); - } - return newDate; - } - @Transactional(readOnly = true) public Order findOrder(Long id) { final Optional order = orderRepository.findById(id); return order.orElseThrow(() -> new OrderNotFoundException(id)); } - @Transactional(readOnly = true) - public List findAllOrder() { - return orderRepository.findAll(); + public List findAllOrder() { + return orderRepository.findAll().stream().map(x -> new OrderDTO(x)).toList(); } - - /*@Transactional - public Order updateOrder(Long id, String date, Integer price, Integer[] count, List products) { + @Transactional + public OrderDTO update(Long id, OrderDTO orderDTO) { final Order currentOrder = findOrder(id); - currentOrder.setDate(getDate(date)); - currentOrder.setPrice(price); + currentOrder.setDate(orderDTO.getDate()); + currentOrder.setPrice(orderDTO.getPrice()); validatorUtil.validate(currentOrder); orderRepository.save(currentOrder); List orderProductsList = orderProductRepository.getOrderProductsByOrderId(id); List product_id = new ArrayList<>(orderProductsList.stream().map(p -> p.getId().getProductId()).toList()); - for (int i = 0; i < products.size(); i++) { - final Long currentId = products.get(i).getId(); + List newProducts = productRepository.findAllById(orderDTO.getProductDTOList().stream().map(x -> x.getId()).toList()); + for (int i = 0; i < newProducts.size(); i++) { + final Long currentId = newProducts.get(i).getId(); if (product_id.contains(currentId)) { final OrderProducts orderProducts = orderProductsList.stream().filter(x -> Objects.equals(x.getId().getProductId(), currentId)).toList().get(0); orderProductsList.remove(orderProducts); - product_id.remove(products.get(i).getId()); - orderProducts.setCount(count[i]); - orderProductRepository.save(orderProducts); - } else { - final OrderProducts orderProducts = new OrderProducts(currentOrder, products.get(i), count[i]); - currentOrder.addProduct(orderProducts); - products.get(i).addOrder(orderProducts); - orderProductRepository.save(orderProducts); - } - } - for (int i = 0; i < orderProductsList.size(); i++) { - orderProductsList.get(i).getProduct().removeOrder(orderProductsList.get(i)); - orderProductsList.get(i).getOrder().removeProducts(orderProductsList.get(i)); - orderProductRepository.delete(orderProductsList.get(i)); - } - return currentOrder; - }*/ - - @Transactional - public Order updateOrder(Long id, String date, Integer price, Integer[] count, List products) { - final Order currentOrder = findOrder(id); - currentOrder.setDate(getDate(date)); - currentOrder.setPrice(price); - validatorUtil.validate(currentOrder); - orderRepository.save(currentOrder); - List orderProductsList = orderProductRepository.getOrderProductsByOrderId(id); - List product_id = new ArrayList<>(orderProductsList.stream().map(p -> p.getId().getProductId()).toList()); - for (int i = 0; i < products.size(); i++) { - final Long currentId = products.get(i).getId(); - if (product_id.contains(currentId)) { - final OrderProducts orderProducts = orderProductsList.stream().filter(x -> Objects.equals(x.getId().getProductId(), currentId)).toList().get(0); - orderProductsList.remove(orderProducts); - product_id.remove(products.get(i).getId()); - orderProducts.setCount(count[i]); - orderProductRepository.save(orderProducts); - } - } - for (int i = 0; i < orderProductsList.size(); i++) { - orderProductsList.get(i).getProduct().removeOrder(orderProductsList.get(i)); - orderProductsList.get(i).getOrder().removeProducts(orderProductsList.get(i)); - orderProductRepository.delete(orderProductsList.get(i)); - } - return currentOrder; - } - @Transactional - public Order update(Order currentOrder, List orderProductsList, List product_id, Integer[] count, List products) { - for (int i = 0; i < products.size(); i++) { - final Long currentId = products.get(i).getId(); - if (product_id.contains(currentId)) { - orderProductsList.remove(orderProductsList.stream().filter(x -> Objects.equals(x.getId().getProductId(), currentId)).toList().get(0)); - product_id.remove(products.get(i).getId()); + int finalI = i; + product_id = product_id.stream().filter(x -> !Objects.equals(x, newProducts.get(finalI).getId())).toList(); + orderProducts.setCount(orderDTO.getProductDTOList().stream().filter(x -> x.getId() == currentId).toList().get(0).getCount()); + orderProductRepository.saveAndFlush(orderProducts); } else { - final OrderProducts orderProducts = new OrderProducts(currentOrder, products.get(i), count[i]); + final OrderProducts orderProducts = new OrderProducts(currentOrder, newProducts.get(i), orderDTO.getProductDTOList().stream().filter(x -> x.getId() == currentId).toList().get(0).getCount()); + orderProductRepository.saveAndFlush(orderProducts); currentOrder.addProduct(orderProducts); - products.get(i).addOrder(orderProducts); + newProducts.get(i).addOrder(orderProducts); orderProductRepository.save(orderProducts); } } - return currentOrder; - } - public List getOrderProducts(Order currentOrder){ - return orderProductRepository.getOrderProductsByOrderId(currentOrder.getId()); + for (int i = 0; i < orderProductsList.size(); i++) { + orderProductsList.get(i).getProduct().removeOrder(orderProductsList.get(i)); + orderProductsList.get(i).getOrder().removeProducts(orderProductsList.get(i)); + orderProductRepository.delete(orderProductsList.get(i)); + } + return new OrderDTO(currentOrder); } @Transactional - public Order deleteOrder(Long id) { + public OrderDTO deleteOrder(Long id) { final Order currentOrder = findOrder(id); int size = currentOrder.getProducts().size(); for (int i = 0; i < size; i++) { @@ -151,7 +120,7 @@ public class OrderService { orderProductRepository.delete(temp); } orderRepository.delete(currentOrder); - return currentOrder; + return new OrderDTO(currentOrder); } @Transactional public void deleteAllOrder() { diff --git a/src/main/java/ip/labwork/shop/service/ProductService.java b/src/main/java/ip/labwork/shop/service/ProductService.java index f148ccf..2d20447 100644 --- a/src/main/java/ip/labwork/shop/service/ProductService.java +++ b/src/main/java/ip/labwork/shop/service/ProductService.java @@ -1,18 +1,14 @@ package ip.labwork.shop.service; +import ip.labwork.shop.controller.ProductDTO; import ip.labwork.shop.model.Component; import ip.labwork.shop.model.OrderProducts; import ip.labwork.shop.model.Product; import ip.labwork.shop.model.ProductComponents; import ip.labwork.shop.repository.*; import ip.labwork.util.validation.ValidatorUtil; -import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityNotFoundException; -import jakarta.persistence.PersistenceContext; -import org.h2.mvstore.tx.Transaction; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.StringUtils; import java.util.*; @@ -32,22 +28,20 @@ public class ProductService { this.orderProductRepository = orderProductRepository; this.componentRepository = componentRepository; } + @Transactional - public Product addProduct(String productName, Integer price) { - final Product product = new Product(productName, price); + public ProductDTO create(ProductDTO productDTO) { + final Product product = new Product(productDTO.getName(), productDTO.getPrice(),productDTO.getImage().getBytes()); validatorUtil.validate(product); productRepository.save(product); - return product; - } - @Transactional - public void addProductComponents(Product product, Integer[] count, List components){ - for (int i = 0; i < components.size(); i++) { - final ProductComponents productComponents = new ProductComponents(components.get(i), product, count[i]); + for (int i = 0; i < productDTO.getComponentDTOList().size(); i++) { + final ProductComponents productComponents = new ProductComponents(componentRepository.findById(productDTO.getComponentDTOList().get(i).getId()).orElseThrow(() -> new ComponentNotFoundException(1L)), product, productDTO.getComponentDTOList().get(i).getCount()); productComponentRepository.saveAndFlush(productComponents); product.addComponent(productComponents); - components.get(i).addProduct(productComponents); + componentRepository.findById(productDTO.getComponentDTOList().get(i).getId()).orElseThrow(() -> new ComponentNotFoundException(1L)).addProduct(productComponents); productComponentRepository.saveAndFlush(productComponents); } + return new ProductDTO(findProduct(product.getId())); } @Transactional(readOnly = true) public Product findProduct(Long id) { @@ -56,32 +50,36 @@ public class ProductService { } @Transactional(readOnly = true) - public List findAllProduct() { - return productRepository.findAll(); + public List findAllProduct() { + return productRepository.findAll().stream().map(x -> new ProductDTO(x)).toList(); } - /*@Transactional - public Product updateProduct(Long id, String productName, Integer price, Integer[] count, List components) { + @Transactional + public ProductDTO updateProduct(Long id, ProductDTO product) { final Product currentProduct = findProduct(id); - currentProduct.setProductName(productName); - currentProduct.setPrice(price); + currentProduct.setProductName(product.getName()); + currentProduct.setPrice(product.getPrice()); + currentProduct.setImage(product.getImage().getBytes()); validatorUtil.validate(currentProduct); productRepository.save(currentProduct); List productComponentsList = productComponentRepository.getProductComponentsByProductId(id); List component_id = new ArrayList<>(productComponentsList.stream().map(p -> p.getId().getComponentId()).toList()); - for (int i = 0; i < components.size(); i++) { - final Long currentId = components.get(i).getId(); + List newComponents = componentRepository.findAllById(product.getComponentDTOList().stream().map(x -> x.getId()).toList()); + for (int i = 0; i < newComponents.size(); i++) { + final Long currentId = newComponents.get(i).getId(); if (component_id.contains(currentId)) { final ProductComponents productComponents = productComponentsList.stream().filter(x -> Objects.equals(x.getId().getComponentId(), currentId)).toList().get(0); productComponentsList.remove(productComponents); - component_id.remove(components.get(i).getId()); - productComponents.setCount(count[i]); + int finalI = i; + component_id = component_id.stream().filter(x -> !Objects.equals(x, newComponents.get(finalI).getId())).toList(); + productComponents.setCount(product.getComponentDTOList().stream().filter(x -> x.getId() == currentId).toList().get(0).getCount()); productComponentRepository.saveAndFlush(productComponents); } else { - final ProductComponents productComponents = new ProductComponents(components.get(i), currentProduct, count[i]); + final ProductComponents productComponents = new ProductComponents(newComponents.get(i), currentProduct, product.getComponentDTOList().stream().filter(x -> x.getId() == currentId).toList().get(0).getCount()); + productComponentRepository.saveAndFlush(productComponents); currentProduct.addComponent(productComponents); - components.get(i).addProduct(productComponents); + newComponents.get(i).addProduct(productComponents); productComponentRepository.save(productComponents); } } @@ -90,59 +88,10 @@ public class ProductService { productComponentsList.get(i).getProduct().removeComponent(productComponentsList.get(i)); productComponentRepository.delete(productComponentsList.get(i)); } - return currentProduct; - }*/ - @Transactional - public Product updateProduct(Long id, String productName, Integer price, Integer[] count, List components) { - final Product currentProduct = findProduct(id); - currentProduct.setProductName(productName); - currentProduct.setPrice(price); - validatorUtil.validate(currentProduct); - productRepository.save(currentProduct); - List productComponentsList = productComponentRepository.getProductComponentsByProductId(id); - List component_id = new ArrayList<>(productComponentsList.stream().map(p -> p.getId().getComponentId()).toList()); - for (int i = 0; i < components.size(); i++) { - final Long currentId = components.get(i).getId(); - if (component_id.contains(currentId)) { - final ProductComponents productComponents = productComponentsList.stream().filter(x -> Objects.equals(x.getId().getComponentId(), currentId)).toList().get(0); - productComponentsList.remove(productComponents); - component_id.remove(components.get(i).getId()); - productComponents.setCount(count[i]); - productComponentRepository.saveAndFlush(productComponents); - } - } - for (int i = 0; i < productComponentsList.size(); i++) { - productComponentsList.get(i).getComponent().removeProduct(productComponentsList.get(i)); - productComponentsList.get(i).getProduct().removeComponent(productComponentsList.get(i)); - productComponentRepository.delete(productComponentsList.get(i)); - } - return currentProduct; + return new ProductDTO(currentProduct); } @Transactional - public Product update(Product currentProduct, List productComponentsList, List component_id, Integer[] count, List components) { - for (int i = 0; i < components.size(); i++) { - final Long currentId = components.get(i).getId(); - if (component_id.contains(currentId)) { - productComponentsList.remove(productComponentsList.stream().filter(x -> Objects.equals(x.getId().getComponentId(), currentId)).toList().get(0)); - component_id.remove(components.get(i).getId()); - } - else { - final ProductComponents productComponents = new ProductComponents(components.get(i), currentProduct, count[i]); - currentProduct.addComponent(productComponents); - components.get(i).addProduct(productComponents); - productComponentRepository.save(productComponents); - } - } - return currentProduct; - } - public List getProductComponents(Product currentProduct){ - return productComponentRepository.getProductComponentsByProductId(currentProduct.getId()); - } - public void test(){ - int s =5; - } - @Transactional - public Product deleteProduct(Long id) { + public ProductDTO deleteProduct(Long id) { final Product currentProduct = findProduct(id); int size = currentProduct.getComponents().size(); for (int i = 0; i < size; i++) { @@ -153,13 +102,13 @@ public class ProductService { } int ordSize = currentProduct.getOrders().size(); for (int i = 0; i < ordSize; i++){ - OrderProducts temp = currentProduct.getOrders().get(0); - temp.getProduct().removeOrder(temp); - temp.getOrder().removeProducts(temp); - orderProductRepository.delete(temp); + OrderProducts orderProducts = currentProduct.getOrders().get(0); + orderProducts.getProduct().removeOrder(orderProducts); + orderProducts.getOrder().removeProducts(orderProducts); + orderProductRepository.delete(orderProducts); } productRepository.delete(currentProduct); - return currentProduct; + return new ProductDTO(currentProduct); } @Transactional