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; package ip.labwork.shop.controller;
import ip.labwork.shop.service.ComponentService; import ip.labwork.shop.service.ComponentService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@ -16,21 +17,18 @@ public class ComponentController {
} }
@PostMapping @PostMapping
public ComponentDTO createComponent(@RequestParam("name") String name, public ComponentDTO createComponent(@RequestBody @Valid ComponentDTO componentDTO) {
@RequestParam("price") Integer price) { return componentService.create(componentDTO);
return new ComponentDTO(componentService.addComponent(name, price));
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public ComponentDTO updateComponent(@PathVariable Long id, public ComponentDTO updateComponent(@PathVariable Long id,@RequestBody @Valid ComponentDTO componentDTO) {
@RequestParam("name") String name, return componentService.updateComponent(id,componentDTO);
@RequestParam("price") Integer price) {
return new ComponentDTO(componentService.updateComponent(id, name, price));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public ComponentDTO removeComponent(@PathVariable Long id) { public ComponentDTO removeComponent(@PathVariable Long id) {
return new ComponentDTO(componentService.deleteComponent(id)); return componentService.deleteComponent(id);
} }
@DeleteMapping @DeleteMapping
@ -45,8 +43,6 @@ public class ComponentController {
@GetMapping @GetMapping
public List<ComponentDTO> findAllComponent() { public List<ComponentDTO> findAllComponent() {
return componentService.findAllComponent().stream() return componentService.findAllComponent();
.map(ComponentDTO::new)
.toList();
} }
} }

View File

@ -3,9 +3,9 @@ package ip.labwork.shop.controller;
import ip.labwork.shop.model.Component; import ip.labwork.shop.model.Component;
public class ComponentDTO { public class ComponentDTO {
private final long id; private long id;
private final String componentName; private String componentName;
private final int price; private int price;
private int count = 0; private int count = 0;
public ComponentDTO(Component component) { public ComponentDTO(Component component) {
this.id = component.getId(); this.id = component.getId();
@ -19,6 +19,9 @@ public class ComponentDTO {
this.count = count; this.count = count;
} }
public ComponentDTO() {
}
public long getId() { public long getId() {
return id; return id;
} }

View File

@ -1,8 +1,7 @@
package ip.labwork.shop.controller; package ip.labwork.shop.controller;
import ip.labwork.shop.service.ProductService;
import ip.labwork.shop.model.Order;
import ip.labwork.shop.service.OrderService; import ip.labwork.shop.service.OrderService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@ -11,33 +10,21 @@ import java.util.List;
@RequestMapping("/order") @RequestMapping("/order")
public class OrderController { public class OrderController {
private final OrderService orderService; private final OrderService orderService;
private final ProductService productService; public OrderController(OrderService orderService) {
public OrderController(OrderService orderService, ProductService productService) {
this.orderService = orderService; this.orderService = orderService;
this.productService = productService;
} }
@PostMapping @PostMapping
public OrderDTO createOrder(@RequestParam("date") String date, public OrderDTO createOrder(@RequestBody @Valid OrderDTO orderDTO){
@RequestParam("price") Integer price, return orderService.create(orderDTO);
@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);
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public OrderDTO updateOrder(@PathVariable Long id, public OrderDTO updateOrder(@PathVariable Long id, @RequestBody @Valid OrderDTO orderDTO){
@RequestParam("date") String date, return orderService.update(id, orderDTO);
@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)));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public OrderDTO removeOrder(@PathVariable Long id){ public OrderDTO removeOrder(@PathVariable Long id){
return new OrderDTO(orderService.deleteOrder(id)); return orderService.deleteOrder(id);
} }
@DeleteMapping @DeleteMapping
public void removeAllOrder(){ public void removeAllOrder(){
@ -49,8 +36,6 @@ public class OrderController {
} }
@GetMapping @GetMapping
public List<OrderDTO> findAllOrder(){ public List<OrderDTO> findAllOrder(){
return orderService.findAllOrder().stream() return orderService.findAllOrder();
.map(OrderDTO::new)
.toList();
} }
} }

View File

@ -7,15 +7,21 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
public class OrderDTO { public class OrderDTO {
private final long id; private long id;
private final Date date; private Date date = new Date();
private final int price; private int price;
private final List<ProductDTO> productDTOList; private List<ProductDTO> productDTOList;
public OrderDTO(Order order) { public OrderDTO(Order order) {
this.id = order.getId(); this.id = order.getId();
this.date = order.getDate(); this.date = order.getDate();
this.price = order.getPrice(); 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() { public long getId() {
@ -30,6 +36,14 @@ public class OrderDTO {
return price; return price;
} }
public void setPrice(int price) {
this.price = price;
}
public void setDate(Date date) {
this.date = date;
}
public List<ProductDTO> getProductDTOList() { public List<ProductDTO> getProductDTOList() {
return productDTOList; return productDTOList;
} }

View File

@ -1,8 +1,7 @@
package ip.labwork.shop.controller; package ip.labwork.shop.controller;
import ip.labwork.shop.service.ProductService; import ip.labwork.shop.service.ProductService;
import ip.labwork.shop.model.Product; import jakarta.validation.Valid;
import ip.labwork.shop.service.ComponentService;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@ -11,36 +10,22 @@ import java.util.List;
@RequestMapping("/product") @RequestMapping("/product")
public class ProductController { public class ProductController {
private final ProductService productService; private final ProductService productService;
private final ComponentService componentService;
public ProductController(ProductService productService, ComponentService componentService) { public ProductController(ProductService productService) {
this.productService = productService; this.productService = productService;
this.componentService = componentService;
} }
@PostMapping @PostMapping
public ProductDTO createProduct(@RequestParam("name") String name, public ProductDTO createProduct(@RequestBody @Valid ProductDTO productDTO){
@RequestParam("price") Integer price, return productService.create(productDTO);
@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()));
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public ProductDTO updateProduct(@PathVariable Long id, public ProductDTO updateProduct(@PathVariable Long id,@RequestBody @Valid ProductDTO productDTO){
@RequestParam("name") String name, return productService.updateProduct(id, productDTO);
@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)));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public ProductDTO removeProduct(@PathVariable Long id){ public ProductDTO removeProduct(@PathVariable Long id){
ProductDTO temp = new ProductDTO(productService.deleteProduct(id)); return productService.deleteProduct(id);
productService.test();
return null;
} }
@DeleteMapping @DeleteMapping
public void removeAllProduct(){ public void removeAllProduct(){
@ -52,8 +37,6 @@ public class ProductController {
} }
@GetMapping @GetMapping
public List<ProductDTO> findAllProduct(){ public List<ProductDTO> findAllProduct(){
return productService.findAllProduct().stream() return productService.findAllProduct();
.map(ProductDTO::new)
.toList();
} }
} }

View File

@ -1,36 +1,57 @@
package ip.labwork.shop.controller; package ip.labwork.shop.controller;
import ip.labwork.shop.model.Product; import ip.labwork.shop.model.Product;
import ip.labwork.shop.model.ProductComponents;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors;
public class ProductDTO { public class ProductDTO {
private final long id; private long id;
private final String productName; private String name;
private final int price; private int price;
private final List<ComponentDTO> componentDTOList; private List<ComponentDTO> componentDTOList;
private final List<OrderDTO> orderDTOList; private List<OrderDTO> orderDTOList;
private String image;
private int count;
public ProductDTO(Product product) { public ProductDTO(Product product) {
this.id = product.getId(); this.id = product.getId();
this.productName = product.getProductName(); this.name = product.getProductName();
this.price = product.getPrice(); this.price = product.getPrice();
this.image = product.getImage() == null? "" : new String(product.getImage());
this.componentDTOList = product.getComponents().stream() this.componentDTOList = product.getComponents().stream()
.filter(x -> Objects.equals(x.getId().getProductId(), product.getId())) .filter(x -> Objects.equals(x.getId().getProductId(), product.getId()))
.map(y -> new ComponentDTO(y.getComponent(), y.getCount())) .map(y -> new ComponentDTO(y.getComponent(), y.getCount()))
.toList(); .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(); 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() { public long getId() {
return id; return id;
} }
public String getProductName() { public int getCount() {
return productName; return count;
}
public void setCount(int count) {
this.count = count;
}
public String getName() {
return name;
} }
public int getPrice() { public int getPrice() {
@ -41,6 +62,14 @@ public class ProductDTO {
return componentDTOList; return componentDTOList;
} }
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public List<OrderDTO> getOrderDTOList() { public List<OrderDTO> getOrderDTOList() {
return orderDTOList; return orderDTOList;
} }

View File

@ -1,7 +1,6 @@
package ip.labwork.shop.model; package ip.labwork.shop.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -2,19 +2,18 @@ package ip.labwork.shop.model;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
@Entity @Entity
@Table(name = "order_product") @Table(name = "order_product")
public class OrderProducts { public class OrderProducts {
@EmbeddedId @EmbeddedId
private OrderProductsKey id; private OrderProductsKey id = new OrderProductsKey();
@ManyToOne @ManyToOne(cascade = CascadeType.MERGE)
@MapsId("productId") @MapsId("productId")
@JoinColumn(name = "product_id") @JoinColumn(name = "product_id")
private Product product; private Product product;
@ManyToOne @ManyToOne(cascade = CascadeType.MERGE)
@MapsId("orderId") @MapsId("orderId")
@JoinColumn(name = "order_id") @JoinColumn(name = "order_id")
@JsonIgnore @JsonIgnore

View File

@ -21,7 +21,9 @@ public class Product {
@NotNull(message = "Price can't be null or empty") @NotNull(message = "Price can't be null or empty")
@Column(name = "price") @Column(name = "price")
private Integer price; private Integer price;
@Lob
@Column(name = "image")
private byte[] image;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<ProductComponents> components; 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.productName = productName;
this.price = price; this.price = price;
this.image = image;
} }
public Long getId() { public Long getId() {
@ -63,6 +66,14 @@ public class Product {
return components; return components;
} }
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public void setComponents(List<ProductComponents> components) { public void setComponents(List<ProductComponents> components) {
this.components = components; this.components = components;
} }

View File

@ -2,7 +2,6 @@ package ip.labwork.shop.model;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
@Entity @Entity

View File

@ -2,7 +2,6 @@ package ip.labwork.shop.repository;
import ip.labwork.shop.model.OrderProducts; import ip.labwork.shop.model.OrderProducts;
import ip.labwork.shop.model.OrderProductsKey; import ip.labwork.shop.model.OrderProductsKey;
import ip.labwork.shop.model.ProductComponents;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List; 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.ProductComponents;
import ip.labwork.shop.model.ProductComponentsKey; import ip.labwork.shop.model.ProductComponentsKey;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List; import java.util.List;

View File

@ -1,5 +1,6 @@
package ip.labwork.shop.service; package ip.labwork.shop.service;
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;
@ -8,8 +9,6 @@ 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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -28,49 +27,40 @@ public class ComponentService {
} }
@Transactional @Transactional
public Component addComponent(String componentName, Integer price) { public ComponentDTO create(ComponentDTO componentDTO) {
final Component component = new Component(componentName, price); final Component component = new Component(componentDTO.getComponentName(), componentDTO.getPrice());
validatorUtil.validate(component); validatorUtil.validate(component);
return componentRepository.save(component); return new ComponentDTO(componentRepository.save(component));
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Component findComponent(Long id) { public Component findComponent(Long id) {
final Optional<Component> student = componentRepository.findById(id); final Optional<Component> component = componentRepository.findById(id);
return student.orElseThrow(() -> new ComponentNotFoundException(id)); return component.orElseThrow(() -> new ComponentNotFoundException(id));
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Component> findAllComponent() { public List<ComponentDTO> findAllComponent() {
return componentRepository.findAll(); 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 @Transactional
public Component updateComponent(Long id, String componentName, Integer price) { public ComponentDTO updateComponent(Long id, ComponentDTO component) {
final Component currentComponent = findComponent(id); final Component currentComponent = findComponent(id);
currentComponent.setComponentName(componentName); currentComponent.setComponentName(component.getComponentName());
currentComponent.setPrice(price); currentComponent.setPrice(component.getPrice());
validatorUtil.validate(currentComponent); validatorUtil.validate(currentComponent);
return componentRepository.save(currentComponent); return new ComponentDTO(componentRepository.save(currentComponent));
} }
@Transactional @Transactional
public Component deleteComponent(Long id) { public ComponentDTO deleteComponent(Long id) {
final Component currentComponent = findComponent(id); final Component currentComponent = findComponent(id);
int size = currentComponent.getProducts().size(); int size = currentComponent.getProducts().size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
ProductComponents temp = currentComponent.getProducts().get(0); ProductComponents productComponents = currentComponent.getProducts().get(0);
temp.getComponent().removeProduct(temp); productComponents.getComponent().removeProduct(productComponents);
temp.getProduct().removeComponent(temp); productComponents.getProduct().removeComponent(productComponents);
productComponentRepository.delete(temp); productComponentRepository.delete(productComponents);
} }
componentRepository.delete(currentComponent); componentRepository.delete(currentComponent);
return currentComponent; return new ComponentDTO(currentComponent);
} }
@Transactional @Transactional
public void deleteAllComponent() { public void deleteAllComponent() {
@ -78,8 +68,4 @@ public class ComponentService {
productComponentRepository.deleteAll(); productComponentRepository.deleteAll();
componentRepository.deleteAll(); componentRepository.deleteAll();
} }
}
public void test() {
int s =5;
}
}

View File

@ -1,147 +1,116 @@
package ip.labwork.shop.service; package ip.labwork.shop.service;
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.OrderProductRepository;
import ip.labwork.shop.repository.OrderRepository; import ip.labwork.shop.repository.OrderRepository;
import ip.labwork.shop.repository.ProductRepository;
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;
import java.text.SimpleDateFormat;
import java.util.*; 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 OrderProductRepository orderProductRepository;
private final ProductRepository productRepository;
private final ValidatorUtil validatorUtil; private final ValidatorUtil validatorUtil;
public OrderService(OrderRepository orderRepository, public OrderService(OrderRepository orderRepository,
ValidatorUtil validatorUtil, OrderProductRepository orderProductRepository) { ValidatorUtil validatorUtil, OrderProductRepository orderProductRepository, ProductRepository productRepository) {
this.orderRepository = orderRepository; this.orderRepository = orderRepository;
this.validatorUtil = validatorUtil; this.validatorUtil = validatorUtil;
this.orderProductRepository = orderProductRepository; this.orderProductRepository = orderProductRepository;
this.productRepository = productRepository;
} }
@Transactional @Transactional
public Order addOrder(String date, Integer price) { public OrderDTO create(OrderDTO orderDTO) {
Date correctDate = getDate(date); int price = 0;
final Order order = new Order(correctDate, price); 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); validatorUtil.validate(order);
orderRepository.save(order); orderRepository.save(order);
return order; return order;
} }
@Transactional
public void addOrderProducts(Order order, Integer[] count, List<Product> products){ public void addOrderProducts(Order order, Integer[] count, List<Product> products){
for (int i = 0; i < products.size(); i++) { for (int i = 0; i < products.size(); i++) {
final OrderProducts orderProducts = new OrderProducts(order, products.get(i), count[i]); final OrderProducts orderProducts = new OrderProducts(order, products.get(i), count[i]);
orderProductRepository.saveAndFlush(orderProducts);
order.addProduct(orderProducts); order.addProduct(orderProducts);
products.get(i).addOrder(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) @Transactional(readOnly = true)
public Order findOrder(Long id) { public Order findOrder(Long id) {
final Optional<Order> order = orderRepository.findById(id); final Optional<Order> order = orderRepository.findById(id);
return order.orElseThrow(() -> new OrderNotFoundException(id)); return order.orElseThrow(() -> new OrderNotFoundException(id));
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Order> findAllOrder() { public List<OrderDTO> findAllOrder() {
return orderRepository.findAll(); return orderRepository.findAll().stream().map(x -> new OrderDTO(x)).toList();
} }
@Transactional
/*@Transactional public OrderDTO update(Long id, OrderDTO orderDTO) {
public Order updateOrder(Long id, String date, Integer price, Integer[] count, List<Product> products) {
final Order currentOrder = findOrder(id); final Order currentOrder = findOrder(id);
currentOrder.setDate(getDate(date)); currentOrder.setDate(orderDTO.getDate());
currentOrder.setPrice(price); 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 = orderProductRepository.getOrderProductsByOrderId(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());
for (int i = 0; i < products.size(); i++) { List<Product> newProducts = productRepository.findAllById(orderDTO.getProductDTOList().stream().map(x -> x.getId()).toList());
final Long currentId = products.get(i).getId(); for (int i = 0; i < newProducts.size(); i++) {
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 -> Objects.equals(x.getId().getProductId(), currentId)).toList().get(0);
orderProductsList.remove(orderProducts); orderProductsList.remove(orderProducts);
product_id.remove(products.get(i).getId()); int finalI = i;
orderProducts.setCount(count[i]); product_id = product_id.stream().filter(x -> !Objects.equals(x, newProducts.get(finalI).getId())).toList();
orderProductRepository.save(orderProducts); orderProducts.setCount(orderDTO.getProductDTOList().stream().filter(x -> x.getId() == currentId).toList().get(0).getCount());
} else { orderProductRepository.saveAndFlush(orderProducts);
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());
} }
else { 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); currentOrder.addProduct(orderProducts);
products.get(i).addOrder(orderProducts); newProducts.get(i).addOrder(orderProducts);
orderProductRepository.save(orderProducts); orderProductRepository.save(orderProducts);
} }
} }
return currentOrder; for (int i = 0; i < orderProductsList.size(); i++) {
} orderProductsList.get(i).getProduct().removeOrder(orderProductsList.get(i));
public List<OrderProducts> getOrderProducts(Order currentOrder){ orderProductsList.get(i).getOrder().removeProducts(orderProductsList.get(i));
return orderProductRepository.getOrderProductsByOrderId(currentOrder.getId()); orderProductRepository.delete(orderProductsList.get(i));
}
return new OrderDTO(currentOrder);
} }
@Transactional @Transactional
public Order deleteOrder(Long id) { public OrderDTO deleteOrder(Long id) {
final Order currentOrder = findOrder(id); final Order currentOrder = findOrder(id);
int size = currentOrder.getProducts().size(); int size = currentOrder.getProducts().size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
@ -151,7 +120,7 @@ public class OrderService {
orderProductRepository.delete(temp); orderProductRepository.delete(temp);
} }
orderRepository.delete(currentOrder); orderRepository.delete(currentOrder);
return currentOrder; return new OrderDTO(currentOrder);
} }
@Transactional @Transactional
public void deleteAllOrder() { public void deleteAllOrder() {

View File

@ -1,18 +1,14 @@
package ip.labwork.shop.service; package ip.labwork.shop.service;
import ip.labwork.shop.controller.ProductDTO;
import ip.labwork.shop.model.Component; import ip.labwork.shop.model.Component;
import ip.labwork.shop.model.OrderProducts; import ip.labwork.shop.model.OrderProducts;
import ip.labwork.shop.model.Product; import ip.labwork.shop.model.Product;
import ip.labwork.shop.model.ProductComponents; import ip.labwork.shop.model.ProductComponents;
import ip.labwork.shop.repository.*; import ip.labwork.shop.repository.*;
import ip.labwork.util.validation.ValidatorUtil; 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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.*; import java.util.*;
@ -32,22 +28,20 @@ public class ProductService {
this.orderProductRepository = orderProductRepository; this.orderProductRepository = orderProductRepository;
this.componentRepository = componentRepository; this.componentRepository = componentRepository;
} }
@Transactional @Transactional
public Product addProduct(String productName, Integer price) { public ProductDTO create(ProductDTO productDTO) {
final Product product = new Product(productName, price); final Product product = new Product(productDTO.getName(), productDTO.getPrice(),productDTO.getImage().getBytes());
validatorUtil.validate(product); validatorUtil.validate(product);
productRepository.save(product); productRepository.save(product);
return 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());
@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]);
productComponentRepository.saveAndFlush(productComponents); productComponentRepository.saveAndFlush(productComponents);
product.addComponent(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); productComponentRepository.saveAndFlush(productComponents);
} }
return new ProductDTO(findProduct(product.getId()));
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Product findProduct(Long id) { public Product findProduct(Long id) {
@ -56,32 +50,36 @@ public class ProductService {
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Product> findAllProduct() { public List<ProductDTO> findAllProduct() {
return productRepository.findAll(); return productRepository.findAll().stream().map(x -> new ProductDTO(x)).toList();
} }
/*@Transactional @Transactional
public Product updateProduct(Long id, String productName, Integer price, Integer[] count, List<Component> components) { public ProductDTO updateProduct(Long id, ProductDTO product) {
final Product currentProduct = findProduct(id); final Product currentProduct = findProduct(id);
currentProduct.setProductName(productName); currentProduct.setProductName(product.getName());
currentProduct.setPrice(price); currentProduct.setPrice(product.getPrice());
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 = productComponentRepository.getProductComponentsByProductId(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());
for (int i = 0; i < components.size(); i++) { List<Component> newComponents = componentRepository.findAllById(product.getComponentDTOList().stream().map(x -> x.getId()).toList());
final Long currentId = components.get(i).getId(); for (int i = 0; i < newComponents.size(); i++) {
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 -> Objects.equals(x.getId().getComponentId(), currentId)).toList().get(0);
productComponentsList.remove(productComponents); productComponentsList.remove(productComponents);
component_id.remove(components.get(i).getId()); int finalI = i;
productComponents.setCount(count[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); productComponentRepository.saveAndFlush(productComponents);
} }
else { 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); currentProduct.addComponent(productComponents);
components.get(i).addProduct(productComponents); newComponents.get(i).addProduct(productComponents);
productComponentRepository.save(productComponents); productComponentRepository.save(productComponents);
} }
} }
@ -90,59 +88,10 @@ public class ProductService {
productComponentsList.get(i).getProduct().removeComponent(productComponentsList.get(i)); productComponentsList.get(i).getProduct().removeComponent(productComponentsList.get(i));
productComponentRepository.delete(productComponentsList.get(i)); productComponentRepository.delete(productComponentsList.get(i));
} }
return currentProduct; return new ProductDTO(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;
} }
@Transactional @Transactional
public Product update(Product currentProduct, List<ProductComponents> productComponentsList, List<Long> component_id, Integer[] count, List<Component> components) { public ProductDTO deleteProduct(Long id) {
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) {
final Product currentProduct = findProduct(id); final Product currentProduct = findProduct(id);
int size = currentProduct.getComponents().size(); int size = currentProduct.getComponents().size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
@ -153,13 +102,13 @@ public class ProductService {
} }
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 temp = currentProduct.getOrders().get(0); OrderProducts orderProducts = currentProduct.getOrders().get(0);
temp.getProduct().removeOrder(temp); orderProducts.getProduct().removeOrder(orderProducts);
temp.getOrder().removeProducts(temp); orderProducts.getOrder().removeProducts(orderProducts);
orderProductRepository.delete(temp); orderProductRepository.delete(orderProducts);
} }
productRepository.delete(currentProduct); productRepository.delete(currentProduct);
return currentProduct; return new ProductDTO(currentProduct);
} }
@Transactional @Transactional