From f89a16bcd1d08bb66dc169e3ba5778748387e7df Mon Sep 17 00:00:00 2001 From: Nikita Sergeev Date: Fri, 21 Apr 2023 14:23:06 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B0=D0=BA=20=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=D1=8C=D0=BD=D0=B5=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- front/src/main.jsx | 2 +- .../java/ip/labwork/shop/model/Component.java | 7 ++- .../java/ip/labwork/shop/model/Product.java | 14 ++++-- .../repository/OrderProductRepository.java | 11 ----- .../shop/repository/OrderRepository.java | 7 +++ .../ProductComponentRepository.java | 11 ----- .../shop/repository/ProductRepository.java | 7 +++ .../shop/service/ComponentService.java | 8 +--- .../ip/labwork/shop/service/OrderService.java | 48 ++++--------------- .../labwork/shop/service/ProductService.java | 30 ++++-------- 11 files changed, 51 insertions(+), 96 deletions(-) delete mode 100644 src/main/java/ip/labwork/shop/repository/OrderProductRepository.java delete mode 100644 src/main/java/ip/labwork/shop/repository/ProductComponentRepository.java diff --git a/build.gradle b/build.gradle index a30d46c..8fdd46b 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,7 @@ dependencies { implementation 'com.h2database:h2:2.1.210' implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5' implementation 'org.hibernate.validator:hibernate-validator' - + implementation group: 'org.springframework', name: 'spring-tx' implementation 'org.springdoc:springdoc-openapi-ui:1.6.5' testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.springframework.boot:spring-boot-starter-validation' diff --git a/front/src/main.jsx b/front/src/main.jsx index 428dd8f..edd296f 100644 --- a/front/src/main.jsx +++ b/front/src/main.jsx @@ -1,5 +1,5 @@ import React from 'react' -import ReactDOM from 'react-dom' +import ReactDOM from 'react-dom/client' import App from './App' import './style.css' diff --git a/src/main/java/ip/labwork/shop/model/Component.java b/src/main/java/ip/labwork/shop/model/Component.java index 61ccbb1..6d76a7c 100644 --- a/src/main/java/ip/labwork/shop/model/Component.java +++ b/src/main/java/ip/labwork/shop/model/Component.java @@ -22,8 +22,11 @@ public class Component { @Column(name = "price") private Integer price; - @OneToMany(mappedBy = "component", cascade = CascadeType.ALL, fetch = FetchType.EAGER) - @JsonIgnore + @OneToMany(mappedBy = "component", cascade = + { + CascadeType.REMOVE, + CascadeType.PERSIST + }, orphanRemoval = true, fetch = FetchType.EAGER) private List products; public Component() { diff --git a/src/main/java/ip/labwork/shop/model/Product.java b/src/main/java/ip/labwork/shop/model/Product.java index 7c07bbb..704c31a 100644 --- a/src/main/java/ip/labwork/shop/model/Product.java +++ b/src/main/java/ip/labwork/shop/model/Product.java @@ -24,11 +24,19 @@ public class Product { @Lob @Column(name = "image") private byte[] image; - @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + @OneToMany(mappedBy = "product", cascade = + { + CascadeType.REMOVE, + CascadeType.MERGE, + CascadeType.PERSIST + }, orphanRemoval = true, fetch = FetchType.EAGER) private List components; - @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER) - @JsonIgnore + @OneToMany(mappedBy = "product", cascade = + { + CascadeType.REMOVE, + CascadeType.PERSIST + }, orphanRemoval = true, fetch = FetchType.EAGER) private List orders; diff --git a/src/main/java/ip/labwork/shop/repository/OrderProductRepository.java b/src/main/java/ip/labwork/shop/repository/OrderProductRepository.java deleted file mode 100644 index 976c32c..0000000 --- a/src/main/java/ip/labwork/shop/repository/OrderProductRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package ip.labwork.shop.repository; - -import ip.labwork.shop.model.OrderProducts; -import ip.labwork.shop.model.OrderProductsKey; -import org.springframework.data.jpa.repository.JpaRepository; - -import java.util.List; - -public interface OrderProductRepository extends JpaRepository { - List getOrderProductsByOrderId(Long order_id); -} \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/repository/OrderRepository.java b/src/main/java/ip/labwork/shop/repository/OrderRepository.java index 6067615..e2444fc 100644 --- a/src/main/java/ip/labwork/shop/repository/OrderRepository.java +++ b/src/main/java/ip/labwork/shop/repository/OrderRepository.java @@ -1,7 +1,14 @@ package ip.labwork.shop.repository; import ip.labwork.shop.model.Order; +import ip.labwork.shop.model.OrderProducts; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; public interface OrderRepository extends JpaRepository { + @Query("Select os from OrderProducts os where os.order.id = :orderId") + List getOrderProduct(@Param("orderId") Long orderId); } \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/repository/ProductComponentRepository.java b/src/main/java/ip/labwork/shop/repository/ProductComponentRepository.java deleted file mode 100644 index 0a2fbaa..0000000 --- a/src/main/java/ip/labwork/shop/repository/ProductComponentRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -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 java.util.List; - -public interface ProductComponentRepository extends JpaRepository { - List getProductComponentsByProductId(Long product_id); -} \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/repository/ProductRepository.java b/src/main/java/ip/labwork/shop/repository/ProductRepository.java index 4044eae..9084245 100644 --- a/src/main/java/ip/labwork/shop/repository/ProductRepository.java +++ b/src/main/java/ip/labwork/shop/repository/ProductRepository.java @@ -1,7 +1,14 @@ package ip.labwork.shop.repository; import ip.labwork.shop.model.Product; +import ip.labwork.shop.model.ProductComponents; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; public interface ProductRepository extends JpaRepository { + @Query("Select os from ProductComponents os where os.product.id = :productId") + List getProductComponent(@Param("productId") Long orderId); } \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/service/ComponentService.java b/src/main/java/ip/labwork/shop/service/ComponentService.java index 9d41ce7..ae8e208 100644 --- a/src/main/java/ip/labwork/shop/service/ComponentService.java +++ b/src/main/java/ip/labwork/shop/service/ComponentService.java @@ -4,7 +4,6 @@ import ip.labwork.shop.controller.ComponentDTO; import ip.labwork.shop.model.Component; import ip.labwork.shop.model.ProductComponents; import ip.labwork.shop.repository.ComponentRepository; -import ip.labwork.shop.repository.ProductComponentRepository; import ip.labwork.util.validation.ValidatorUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -16,14 +15,12 @@ import java.util.Optional; public class ComponentService { private final ComponentRepository componentRepository; - private final ProductComponentRepository productComponentRepository; private final ValidatorUtil validatorUtil; public ComponentService(ComponentRepository componentRepository, - ValidatorUtil validatorUtil, ProductComponentRepository productComponentRepository) { + ValidatorUtil validatorUtil) { this.componentRepository = componentRepository; this.validatorUtil = validatorUtil; - this.productComponentRepository = productComponentRepository; } @Transactional @@ -57,15 +54,12 @@ public class ComponentService { ProductComponents productComponents = currentComponent.getProducts().get(0); productComponents.getComponent().removeProduct(productComponents); productComponents.getProduct().removeComponent(productComponents); - productComponentRepository.delete(productComponents); } componentRepository.delete(currentComponent); return new ComponentDTO(currentComponent); } @Transactional public void deleteAllComponent() { - productComponentRepository.findAll().forEach(ProductComponents::remove); - productComponentRepository.deleteAll(); componentRepository.deleteAll(); } } \ 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 63ef9fd..b81338f 100644 --- a/src/main/java/ip/labwork/shop/service/OrderService.java +++ b/src/main/java/ip/labwork/shop/service/OrderService.java @@ -2,7 +2,6 @@ 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; @@ -14,15 +13,13 @@ 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, ProductRepository productRepository) { + ValidatorUtil validatorUtil, ProductRepository productRepository) { this.orderRepository = orderRepository; this.validatorUtil = validatorUtil; - this.orderProductRepository = orderProductRepository; this.productRepository = productRepository; } @Transactional @@ -36,34 +33,10 @@ public class OrderService { 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; - } - 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.saveAndFlush(orderProducts); - } + return new OrderDTO(findOrder(order.getId())); } @Transactional(readOnly = true) public Order findOrder(Long id) { @@ -81,32 +54,32 @@ public class OrderService { currentOrder.setPrice(orderDTO.getPrice()); validatorUtil.validate(currentOrder); orderRepository.save(currentOrder); - List orderProductsList = orderProductRepository.getOrderProductsByOrderId(id); + List orderProductsList = orderRepository.getOrderProduct(id); List product_id = new ArrayList<>(orderProductsList.stream().map(p -> p.getId().getProductId()).toList()); 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); + final OrderProducts orderProducts = orderProductsList.stream().filter(x -> x.getId().getProductId().equals(currentId)).findFirst().get(); orderProductsList.remove(orderProducts); + currentOrder.removeProducts(orderProducts); + currentOrder.addProduct(new OrderProducts(currentOrder, newProducts.get(i), orderDTO.getProductDTOList().stream().filter(x -> x.getId() == currentId).toList().get(0).getCount())); 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); + orderRepository.saveAndFlush(currentOrder); } else { 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); - newProducts.get(i).addOrder(orderProducts); - orderProductRepository.save(orderProducts); + orderRepository.saveAndFlush(currentOrder); } } 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)); } + orderRepository.saveAndFlush(currentOrder); return new OrderDTO(currentOrder); } @Transactional @@ -117,15 +90,12 @@ public class OrderService { OrderProducts temp = currentOrder.getProducts().get(0); temp.getProduct().removeOrder(temp); temp.getOrder().removeProducts(temp); - orderProductRepository.delete(temp); } orderRepository.delete(currentOrder); return new OrderDTO(currentOrder); } @Transactional public void deleteAllOrder() { - orderProductRepository.findAll().forEach(OrderProducts::remove); - orderProductRepository.deleteAll(); orderRepository.deleteAll(); } } \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/service/ProductService.java b/src/main/java/ip/labwork/shop/service/ProductService.java index 2d20447..982d330 100644 --- a/src/main/java/ip/labwork/shop/service/ProductService.java +++ b/src/main/java/ip/labwork/shop/service/ProductService.java @@ -15,17 +15,13 @@ import java.util.*; @Service public class ProductService { private final ProductRepository productRepository; - private final ProductComponentRepository productComponentRepository; - private final OrderProductRepository orderProductRepository; private final ComponentRepository componentRepository; private final ValidatorUtil validatorUtil; public ProductService(ProductRepository productRepository, - ValidatorUtil validatorUtil, ProductComponentRepository productComponentRepository, OrderProductRepository orderProductRepository, ComponentRepository componentRepository) { + ValidatorUtil validatorUtil, ComponentRepository componentRepository) { this.productRepository = productRepository; this.validatorUtil = validatorUtil; - this.productComponentRepository = productComponentRepository; - this.orderProductRepository = orderProductRepository; this.componentRepository = componentRepository; } @@ -36,11 +32,9 @@ public class ProductService { productRepository.save(product); 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); - componentRepository.findById(productDTO.getComponentDTOList().get(i).getId()).orElseThrow(() -> new ComponentNotFoundException(1L)).addProduct(productComponents); - productComponentRepository.saveAndFlush(productComponents); } + productRepository.save(product); return new ProductDTO(findProduct(product.getId())); } @Transactional(readOnly = true) @@ -62,32 +56,32 @@ public class ProductService { currentProduct.setImage(product.getImage().getBytes()); validatorUtil.validate(currentProduct); productRepository.save(currentProduct); - List productComponentsList = productComponentRepository.getProductComponentsByProductId(id); + List productComponentsList = productRepository.getProductComponent(id); List component_id = new ArrayList<>(productComponentsList.stream().map(p -> p.getId().getComponentId()).toList()); 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); + final ProductComponents productComponents = productComponentsList.stream().filter(x -> x.getId().getComponentId().equals(currentId)).findFirst().get(); productComponentsList.remove(productComponents); + currentProduct.removeComponent(productComponents); + currentProduct.addComponent(new ProductComponents(newComponents.get(i) , currentProduct, product.getComponentDTOList().stream().filter(x -> x.getId() == currentId).toList().get(0).getCount())); 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); + productRepository.saveAndFlush(currentProduct); } else { 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); - newComponents.get(i).addProduct(productComponents); - productComponentRepository.save(productComponents); + productRepository.saveAndFlush(currentProduct); } } 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)); } + productRepository.saveAndFlush(currentProduct); return new ProductDTO(currentProduct); } @Transactional @@ -98,14 +92,12 @@ public class ProductService { ProductComponents temp = currentProduct.getComponents().get(0); temp.getComponent().removeProduct(temp); temp.getProduct().removeComponent(temp); - productComponentRepository.delete(temp); } int ordSize = currentProduct.getOrders().size(); for (int i = 0; i < ordSize; i++){ OrderProducts orderProducts = currentProduct.getOrders().get(0); orderProducts.getProduct().removeOrder(orderProducts); orderProducts.getOrder().removeProducts(orderProducts); - orderProductRepository.delete(orderProducts); } productRepository.delete(currentProduct); return new ProductDTO(currentProduct); @@ -113,10 +105,6 @@ public class ProductService { @Transactional public void deleteAllProduct() { - orderProductRepository.findAll().forEach(OrderProducts::remove); - productComponentRepository.findAll().forEach(ProductComponents::remove); - productComponentRepository.deleteAll(); - orderProductRepository.deleteAll(); productRepository.deleteAll(); }