Так правильнее

This commit is contained in:
Nikita Sergeev 2023-04-21 14:23:06 +04:00
parent 26a040743d
commit f89a16bcd1
11 changed files with 51 additions and 96 deletions

View File

@ -23,7 +23,7 @@ dependencies {
implementation 'com.h2database:h2:2.1.210' implementation 'com.h2database:h2:2.1.210'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5' implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
implementation 'org.hibernate.validator:hibernate-validator' implementation 'org.hibernate.validator:hibernate-validator'
implementation group: 'org.springframework', name: 'spring-tx'
implementation 'org.springdoc:springdoc-openapi-ui:1.6.5' implementation 'org.springdoc:springdoc-openapi-ui:1.6.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-validation'

View File

@ -1,5 +1,5 @@
import React from 'react' import React from 'react'
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom/client'
import App from './App' import App from './App'
import './style.css' import './style.css'

View File

@ -22,8 +22,11 @@ public class Component {
@Column(name = "price") @Column(name = "price")
private Integer price; private Integer price;
@OneToMany(mappedBy = "component", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @OneToMany(mappedBy = "component", cascade =
@JsonIgnore {
CascadeType.REMOVE,
CascadeType.PERSIST
}, orphanRemoval = true, fetch = FetchType.EAGER)
private List<ProductComponents> products; private List<ProductComponents> products;
public Component() { public Component() {

View File

@ -24,11 +24,19 @@ public class Product {
@Lob @Lob
@Column(name = "image") @Column(name = "image")
private byte[] 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<ProductComponents> components; private List<ProductComponents> components;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @OneToMany(mappedBy = "product", cascade =
@JsonIgnore {
CascadeType.REMOVE,
CascadeType.PERSIST
}, orphanRemoval = true, fetch = FetchType.EAGER)
private List<OrderProducts> orders; private List<OrderProducts> orders;

View File

@ -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<OrderProducts, OrderProductsKey> {
List<OrderProducts> getOrderProductsByOrderId(Long order_id);
}

View File

@ -1,7 +1,14 @@
package ip.labwork.shop.repository; package ip.labwork.shop.repository;
import ip.labwork.shop.model.Order; 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.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface OrderRepository extends JpaRepository<Order, Long> { public interface OrderRepository extends JpaRepository<Order, Long> {
@Query("Select os from OrderProducts os where os.order.id = :orderId")
List<OrderProducts> getOrderProduct(@Param("orderId") Long orderId);
} }

View File

@ -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<ProductComponents, ProductComponentsKey> {
List<ProductComponents> getProductComponentsByProductId(Long product_id);
}

View File

@ -1,7 +1,14 @@
package ip.labwork.shop.repository; package ip.labwork.shop.repository;
import ip.labwork.shop.model.Product; 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.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface ProductRepository extends JpaRepository<Product, Long> { public interface ProductRepository extends JpaRepository<Product, Long> {
@Query("Select os from ProductComponents os where os.product.id = :productId")
List<ProductComponents> getProductComponent(@Param("productId") Long orderId);
} }

View File

@ -4,7 +4,6 @@ import ip.labwork.shop.controller.ComponentDTO;
import ip.labwork.shop.model.Component; import ip.labwork.shop.model.Component;
import ip.labwork.shop.model.ProductComponents; import ip.labwork.shop.model.ProductComponents;
import ip.labwork.shop.repository.ComponentRepository; import ip.labwork.shop.repository.ComponentRepository;
import ip.labwork.shop.repository.ProductComponentRepository;
import ip.labwork.util.validation.ValidatorUtil; import ip.labwork.util.validation.ValidatorUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -16,14 +15,12 @@ import java.util.Optional;
public class ComponentService { public class ComponentService {
private final ComponentRepository componentRepository; private final ComponentRepository componentRepository;
private final ProductComponentRepository productComponentRepository;
private final ValidatorUtil validatorUtil; private final ValidatorUtil validatorUtil;
public ComponentService(ComponentRepository componentRepository, public ComponentService(ComponentRepository componentRepository,
ValidatorUtil validatorUtil, ProductComponentRepository productComponentRepository) { ValidatorUtil validatorUtil) {
this.componentRepository = componentRepository; this.componentRepository = componentRepository;
this.validatorUtil = validatorUtil; this.validatorUtil = validatorUtil;
this.productComponentRepository = productComponentRepository;
} }
@Transactional @Transactional
@ -57,15 +54,12 @@ public class ComponentService {
ProductComponents productComponents = currentComponent.getProducts().get(0); ProductComponents productComponents = currentComponent.getProducts().get(0);
productComponents.getComponent().removeProduct(productComponents); productComponents.getComponent().removeProduct(productComponents);
productComponents.getProduct().removeComponent(productComponents); productComponents.getProduct().removeComponent(productComponents);
productComponentRepository.delete(productComponents);
} }
componentRepository.delete(currentComponent); componentRepository.delete(currentComponent);
return new ComponentDTO(currentComponent); return new ComponentDTO(currentComponent);
} }
@Transactional @Transactional
public void deleteAllComponent() { public void deleteAllComponent() {
productComponentRepository.findAll().forEach(ProductComponents::remove);
productComponentRepository.deleteAll();
componentRepository.deleteAll(); componentRepository.deleteAll();
} }
} }

View File

@ -2,7 +2,6 @@ package ip.labwork.shop.service;
import ip.labwork.shop.controller.OrderDTO; import ip.labwork.shop.controller.OrderDTO;
import ip.labwork.shop.model.*; import ip.labwork.shop.model.*;
import ip.labwork.shop.repository.OrderProductRepository;
import ip.labwork.shop.repository.OrderRepository; import ip.labwork.shop.repository.OrderRepository;
import ip.labwork.shop.repository.ProductRepository; import ip.labwork.shop.repository.ProductRepository;
import ip.labwork.util.validation.ValidatorUtil; import ip.labwork.util.validation.ValidatorUtil;
@ -14,15 +13,13 @@ import java.util.*;
@Service @Service
public class OrderService { public class OrderService {
private final OrderRepository orderRepository; private final OrderRepository orderRepository;
private final OrderProductRepository orderProductRepository;
private final ProductRepository productRepository; private final ProductRepository productRepository;
private final ValidatorUtil validatorUtil; private final ValidatorUtil validatorUtil;
public OrderService(OrderRepository orderRepository, public OrderService(OrderRepository orderRepository,
ValidatorUtil validatorUtil, OrderProductRepository orderProductRepository, ProductRepository productRepository) { ValidatorUtil validatorUtil, ProductRepository productRepository) {
this.orderRepository = orderRepository; this.orderRepository = orderRepository;
this.validatorUtil = validatorUtil; this.validatorUtil = validatorUtil;
this.orderProductRepository = orderProductRepository;
this.productRepository = productRepository; this.productRepository = productRepository;
} }
@Transactional @Transactional
@ -36,34 +33,10 @@ public class OrderService {
orderRepository.save(order); orderRepository.save(order);
for (int i = 0; i < orderDTO.getProductDTOList().size(); i++) { 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()); 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); 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); orderRepository.save(order);
return order; return new OrderDTO(findOrder(order.getId()));
}
public void addOrderProducts(Order order, Integer[] count, List<Product> 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);
}
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Order findOrder(Long id) { public Order findOrder(Long id) {
@ -81,32 +54,32 @@ public class OrderService {
currentOrder.setPrice(orderDTO.getPrice()); currentOrder.setPrice(orderDTO.getPrice());
validatorUtil.validate(currentOrder); validatorUtil.validate(currentOrder);
orderRepository.save(currentOrder); orderRepository.save(currentOrder);
List<OrderProducts> orderProductsList = orderProductRepository.getOrderProductsByOrderId(id); List<OrderProducts> orderProductsList = orderRepository.getOrderProduct(id);
List<Long> product_id = new ArrayList<>(orderProductsList.stream().map(p -> p.getId().getProductId()).toList()); List<Long> product_id = new ArrayList<>(orderProductsList.stream().map(p -> p.getId().getProductId()).toList());
List<Product> newProducts = productRepository.findAllById(orderDTO.getProductDTOList().stream().map(x -> x.getId()).toList()); List<Product> newProducts = productRepository.findAllById(orderDTO.getProductDTOList().stream().map(x -> x.getId()).toList());
for (int i = 0; i < newProducts.size(); i++) { for (int i = 0; i < newProducts.size(); i++) {
final Long currentId = newProducts.get(i).getId(); final Long currentId = newProducts.get(i).getId();
if (product_id.contains(currentId)) { 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); 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; int finalI = i;
product_id = product_id.stream().filter(x -> !Objects.equals(x, newProducts.get(finalI).getId())).toList(); 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()); orderProducts.setCount(orderDTO.getProductDTOList().stream().filter(x -> x.getId() == currentId).toList().get(0).getCount());
orderProductRepository.saveAndFlush(orderProducts); orderRepository.saveAndFlush(currentOrder);
} }
else { else {
final OrderProducts orderProducts = new OrderProducts(currentOrder, newProducts.get(i), orderDTO.getProductDTOList().stream().filter(x -> x.getId() == currentId).toList().get(0).getCount()); 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); currentOrder.addProduct(orderProducts);
newProducts.get(i).addOrder(orderProducts); orderRepository.saveAndFlush(currentOrder);
orderProductRepository.save(orderProducts);
} }
} }
for (int i = 0; i < orderProductsList.size(); i++) { for (int i = 0; i < orderProductsList.size(); i++) {
orderProductsList.get(i).getProduct().removeOrder(orderProductsList.get(i)); orderProductsList.get(i).getProduct().removeOrder(orderProductsList.get(i));
orderProductsList.get(i).getOrder().removeProducts(orderProductsList.get(i)); orderProductsList.get(i).getOrder().removeProducts(orderProductsList.get(i));
orderProductRepository.delete(orderProductsList.get(i));
} }
orderRepository.saveAndFlush(currentOrder);
return new OrderDTO(currentOrder); return new OrderDTO(currentOrder);
} }
@Transactional @Transactional
@ -117,15 +90,12 @@ public class OrderService {
OrderProducts temp = currentOrder.getProducts().get(0); OrderProducts temp = currentOrder.getProducts().get(0);
temp.getProduct().removeOrder(temp); temp.getProduct().removeOrder(temp);
temp.getOrder().removeProducts(temp); temp.getOrder().removeProducts(temp);
orderProductRepository.delete(temp);
} }
orderRepository.delete(currentOrder); orderRepository.delete(currentOrder);
return new OrderDTO(currentOrder); return new OrderDTO(currentOrder);
} }
@Transactional @Transactional
public void deleteAllOrder() { public void deleteAllOrder() {
orderProductRepository.findAll().forEach(OrderProducts::remove);
orderProductRepository.deleteAll();
orderRepository.deleteAll(); orderRepository.deleteAll();
} }
} }

View File

@ -15,17 +15,13 @@ import java.util.*;
@Service @Service
public class ProductService { public class ProductService {
private final ProductRepository productRepository; private final ProductRepository productRepository;
private final ProductComponentRepository productComponentRepository;
private final OrderProductRepository orderProductRepository;
private final ComponentRepository componentRepository; private final ComponentRepository componentRepository;
private final ValidatorUtil validatorUtil; private final ValidatorUtil validatorUtil;
public ProductService(ProductRepository productRepository, public ProductService(ProductRepository productRepository,
ValidatorUtil validatorUtil, ProductComponentRepository productComponentRepository, OrderProductRepository orderProductRepository, ComponentRepository componentRepository) { ValidatorUtil validatorUtil, ComponentRepository componentRepository) {
this.productRepository = productRepository; this.productRepository = productRepository;
this.validatorUtil = validatorUtil; this.validatorUtil = validatorUtil;
this.productComponentRepository = productComponentRepository;
this.orderProductRepository = orderProductRepository;
this.componentRepository = componentRepository; this.componentRepository = componentRepository;
} }
@ -36,11 +32,9 @@ public class ProductService {
productRepository.save(product); productRepository.save(product);
for (int i = 0; i < productDTO.getComponentDTOList().size(); 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()); 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); 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())); return new ProductDTO(findProduct(product.getId()));
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
@ -62,32 +56,32 @@ public class ProductService {
currentProduct.setImage(product.getImage().getBytes()); currentProduct.setImage(product.getImage().getBytes());
validatorUtil.validate(currentProduct); validatorUtil.validate(currentProduct);
productRepository.save(currentProduct); productRepository.save(currentProduct);
List<ProductComponents> productComponentsList = productComponentRepository.getProductComponentsByProductId(id); List<ProductComponents> productComponentsList = productRepository.getProductComponent(id);
List<Long> component_id = new ArrayList<>(productComponentsList.stream().map(p -> p.getId().getComponentId()).toList()); List<Long> component_id = new ArrayList<>(productComponentsList.stream().map(p -> p.getId().getComponentId()).toList());
List<Component> newComponents = componentRepository.findAllById(product.getComponentDTOList().stream().map(x -> x.getId()).toList()); List<Component> newComponents = componentRepository.findAllById(product.getComponentDTOList().stream().map(x -> x.getId()).toList());
for (int i = 0; i < newComponents.size(); i++) { for (int i = 0; i < newComponents.size(); i++) {
final Long currentId = newComponents.get(i).getId(); final Long currentId = newComponents.get(i).getId();
if (component_id.contains(currentId)) { 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); 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; int finalI = i;
component_id = component_id.stream().filter(x -> !Objects.equals(x, newComponents.get(finalI).getId())).toList(); 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()); productComponents.setCount(product.getComponentDTOList().stream().filter(x -> x.getId() == currentId).toList().get(0).getCount());
productComponentRepository.saveAndFlush(productComponents); productRepository.saveAndFlush(currentProduct);
} }
else { else {
final ProductComponents productComponents = new ProductComponents(newComponents.get(i), currentProduct, product.getComponentDTOList().stream().filter(x -> x.getId() == currentId).toList().get(0).getCount()); 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); currentProduct.addComponent(productComponents);
newComponents.get(i).addProduct(productComponents); productRepository.saveAndFlush(currentProduct);
productComponentRepository.save(productComponents);
} }
} }
for (int i = 0; i < productComponentsList.size(); i++) { for (int i = 0; i < productComponentsList.size(); i++) {
productComponentsList.get(i).getComponent().removeProduct(productComponentsList.get(i)); productComponentsList.get(i).getComponent().removeProduct(productComponentsList.get(i));
productComponentsList.get(i).getProduct().removeComponent(productComponentsList.get(i)); productComponentsList.get(i).getProduct().removeComponent(productComponentsList.get(i));
productComponentRepository.delete(productComponentsList.get(i));
} }
productRepository.saveAndFlush(currentProduct);
return new ProductDTO(currentProduct); return new ProductDTO(currentProduct);
} }
@Transactional @Transactional
@ -98,14 +92,12 @@ public class ProductService {
ProductComponents temp = currentProduct.getComponents().get(0); ProductComponents temp = currentProduct.getComponents().get(0);
temp.getComponent().removeProduct(temp); temp.getComponent().removeProduct(temp);
temp.getProduct().removeComponent(temp); temp.getProduct().removeComponent(temp);
productComponentRepository.delete(temp);
} }
int ordSize = currentProduct.getOrders().size(); int ordSize = currentProduct.getOrders().size();
for (int i = 0; i < ordSize; i++){ for (int i = 0; i < ordSize; i++){
OrderProducts orderProducts = currentProduct.getOrders().get(0); OrderProducts orderProducts = currentProduct.getOrders().get(0);
orderProducts.getProduct().removeOrder(orderProducts); orderProducts.getProduct().removeOrder(orderProducts);
orderProducts.getOrder().removeProducts(orderProducts); orderProducts.getOrder().removeProducts(orderProducts);
orderProductRepository.delete(orderProducts);
} }
productRepository.delete(currentProduct); productRepository.delete(currentProduct);
return new ProductDTO(currentProduct); return new ProductDTO(currentProduct);
@ -113,10 +105,6 @@ public class ProductService {
@Transactional @Transactional
public void deleteAllProduct() { public void deleteAllProduct() {
orderProductRepository.findAll().forEach(OrderProducts::remove);
productComponentRepository.findAll().forEach(ProductComponents::remove);
productComponentRepository.deleteAll();
orderProductRepository.deleteAll();
productRepository.deleteAll(); productRepository.deleteAll();
} }