backend lab4

This commit is contained in:
Nikita Sergeev 2023-04-09 14:04:16 +04:00
parent 686c5db7bf
commit 258c8737b8
15 changed files with 211 additions and 291 deletions

View File

@ -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<ComponentDTO> findAllComponent() {
return componentService.findAllComponent().stream()
.map(ComponentDTO::new)
.toList();
return componentService.findAllComponent();
}
}

View File

@ -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;
}

View File

@ -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<OrderDTO> findAllOrder(){
return orderService.findAllOrder().stream()
.map(OrderDTO::new)
.toList();
return orderService.findAllOrder();
}
}

View File

@ -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<ProductDTO> productDTOList;
private long id;
private Date date = new Date();
private int price;
private List<ProductDTO> 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<ProductDTO> getProductDTOList() {
return productDTOList;
}

View File

@ -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<ProductDTO> findAllProduct(){
return productService.findAllProduct().stream()
.map(ProductDTO::new)
.toList();
return productService.findAllProduct();
}
}

View File

@ -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<ComponentDTO> componentDTOList;
private final List<OrderDTO> orderDTOList;
private long id;
private String name;
private int price;
private List<ComponentDTO> componentDTOList;
private List<OrderDTO> 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<OrderDTO> getOrderDTOList() {
return orderDTOList;
}

View File

@ -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;

View File

@ -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

View File

@ -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<ProductComponents> 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<ProductComponents> components) {
this.components = components;
}

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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<Component> student = componentRepository.findById(id);
return student.orElseThrow(() -> new ComponentNotFoundException(id));
final Optional<Component> component = componentRepository.findById(id);
return component.orElseThrow(() -> new ComponentNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Component> findAllComponent() {
return componentRepository.findAll();
public List<ComponentDTO> findAllComponent() {
return componentRepository.findAll().stream().map(x -> new ComponentDTO(x)).toList();
}
@Transactional(readOnly = true)
public List<Component> 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;
}
}
}

View File

@ -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<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.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> order = orderRepository.findById(id);
return order.orElseThrow(() -> new OrderNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Order> findAllOrder() {
return orderRepository.findAll();
public List<OrderDTO> findAllOrder() {
return orderRepository.findAll().stream().map(x -> new OrderDTO(x)).toList();
}
/*@Transactional
public Order updateOrder(Long id, String date, Integer price, Integer[] count, List<Product> 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<OrderProducts> orderProductsList = orderProductRepository.getOrderProductsByOrderId(id);
List<Long> 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<Product> 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<Product> products) {
final Order currentOrder = findOrder(id);
currentOrder.setDate(getDate(date));
currentOrder.setPrice(price);
validatorUtil.validate(currentOrder);
orderRepository.save(currentOrder);
List<OrderProducts> orderProductsList = orderProductRepository.getOrderProductsByOrderId(id);
List<Long> 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<OrderProducts> orderProductsList, List<Long> product_id, Integer[] count, List<Product> 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<OrderProducts> 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() {

View File

@ -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<Component> 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<Product> findAllProduct() {
return productRepository.findAll();
public List<ProductDTO> findAllProduct() {
return productRepository.findAll().stream().map(x -> new ProductDTO(x)).toList();
}
/*@Transactional
public Product updateProduct(Long id, String productName, Integer price, Integer[] count, List<Component> 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<ProductComponents> productComponentsList = productComponentRepository.getProductComponentsByProductId(id);
List<Long> 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<Component> 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<Component> components) {
final Product currentProduct = findProduct(id);
currentProduct.setProductName(productName);
currentProduct.setPrice(price);
validatorUtil.validate(currentProduct);
productRepository.save(currentProduct);
List<ProductComponents> productComponentsList = productComponentRepository.getProductComponentsByProductId(id);
List<Long> 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<ProductComponents> productComponentsList, List<Long> component_id, Integer[] count, List<Component> 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<ProductComponents> 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