From 70a17c4b7cd1b923a902760bd794f90ee454da15 Mon Sep 17 00:00:00 2001 From: Nikita Sergeev Date: Thu, 16 Mar 2023 22:58:22 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20=D0=B2=D1=81=D1=91,=20=D0=BA=D1=80=D0=BE=D0=BC=D0=B5?= =?UTF-8?q?=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../student/controller/OrderController.java | 79 +++++++++++++++++ .../student/controller/ProductController.java | 72 ++++++++++++++++ .../ip/labwork/student/model/Component.java | 21 +++-- .../java/ip/labwork/student/model/Order.java | 85 +++++++++++++++++++ .../labwork/student/model/OrderProducts.java | 63 ++++++++++++++ .../student/model/OrderProductsKey.java | 48 +++++++++++ .../ip/labwork/student/model/Product.java | 49 +++++++++-- .../student/model/ProductComponents.java | 1 + .../student/service/OrderProductsService.java | 77 +++++++++++++++++ .../labwork/student/service/OrderService.java | 60 +++++++++++++ .../service/ProductComponentsService.java | 83 ++++++++++++++++++ .../student/service/ProductService.java | 32 ++----- src/test/java/ip/labwork/JpaStudentTests.java | 4 +- 13 files changed, 635 insertions(+), 39 deletions(-) create mode 100644 src/main/java/ip/labwork/student/controller/OrderController.java create mode 100644 src/main/java/ip/labwork/student/controller/ProductController.java create mode 100644 src/main/java/ip/labwork/student/model/Order.java create mode 100644 src/main/java/ip/labwork/student/model/OrderProducts.java create mode 100644 src/main/java/ip/labwork/student/model/OrderProductsKey.java create mode 100644 src/main/java/ip/labwork/student/service/OrderProductsService.java create mode 100644 src/main/java/ip/labwork/student/service/OrderService.java create mode 100644 src/main/java/ip/labwork/student/service/ProductComponentsService.java diff --git a/src/main/java/ip/labwork/student/controller/OrderController.java b/src/main/java/ip/labwork/student/controller/OrderController.java new file mode 100644 index 0000000..d72cb19 --- /dev/null +++ b/src/main/java/ip/labwork/student/controller/OrderController.java @@ -0,0 +1,79 @@ +package ip.labwork.student.controller; + +import ip.labwork.student.model.Order; +import ip.labwork.student.model.Product; +import ip.labwork.student.service.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +@RestController +@RequestMapping("/order") +public class OrderController { + private final OrderService orderService; + private final ProductService productService; + private final OrderProductsService orderProductsService; + + public OrderController(OrderService orderService, ProductService productService, OrderProductsService orderProductsService) { + this.orderService = orderService; + this.productService = productService; + this.orderProductsService = orderProductsService; + } + + @GetMapping("/add") + public Order create(@RequestParam("date") String date, + @RequestParam("price") Integer price, + @RequestParam("count") Integer[] count, + @RequestParam("prod") Long[] prod){ + SimpleDateFormat format = new SimpleDateFormat(); + format.applyPattern("dd.MM.yyyy"); + Date newDate; + try{ + newDate = format.parse(date); + }catch (Exception exception){ + newDate = new Date(); + } + Order order = orderService.addOrder(newDate, price); + for (int i=0; i < prod.length; i++) + orderProductsService.addOrderProducts(order, productService.findProduct(prod[i]), count[i]); + return order; + } + @GetMapping("/update") + public Order update(@RequestParam("id") Long id, + @RequestParam("date") Date date, + @RequestParam("price") Integer price, + @RequestParam("count") Integer[] count, + @RequestParam("prod") Long[] prod){ + orderService.updateOrder(id, date, price); + orderProductsService.removeAll(id, prod); + Order order = orderService.findOrder(id); + for(int i = 0; i < prod.length; i++){ + orderProductsService.addOrderProducts(order, productService.findProduct(prod[i]),count[i]); + } + return order; + } + @GetMapping("/remove") + public Order remove(@RequestParam("id") Long id){ + Order order = orderService.findOrder(id); + orderProductsService.deleteOrder(order); + return orderService.deleteOrder(id); + } + @GetMapping("/removeAll") + public void remove(){ + orderProductsService.deleteAllOrder(); + orderService.deleteAllOrder(); + } + @GetMapping("/find") + public Order find(@RequestParam("id") Long id){ + return orderService.findOrder(id); + } + @GetMapping("/findAll") + public List findAll(){ + return orderService.findAllOrder(); + } +} diff --git a/src/main/java/ip/labwork/student/controller/ProductController.java b/src/main/java/ip/labwork/student/controller/ProductController.java new file mode 100644 index 0000000..881d548 --- /dev/null +++ b/src/main/java/ip/labwork/student/controller/ProductController.java @@ -0,0 +1,72 @@ +package ip.labwork.student.controller; + +import ip.labwork.student.model.Component; +import ip.labwork.student.model.Product; +import ip.labwork.student.model.ProductComponents; +import ip.labwork.student.service.ComponentService; +import ip.labwork.student.service.ProductComponentsService; +import ip.labwork.student.service.ProductService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/product") +public class ProductController { + private final ProductService productService; + private final ComponentService componentService; + private final ProductComponentsService productComponentsService; + + public ProductController(ProductService productService, ComponentService componentService, ProductComponentsService productComponentsService) { + this.productService = productService; + this.componentService = componentService; + this.productComponentsService = productComponentsService; + } + + @GetMapping("/add") + public Product create(@RequestParam("name") String name, + @RequestParam("price") Integer price, + @RequestParam("count") Integer[] count, + @RequestParam("comp") Long[] comp){ + Product product = productService.addProduct(name,price); + for (int i=0; i < comp.length; i++) + productComponentsService.addProductComponents(product,componentService.findComponent(comp[i]), count[i]); + return product; + } + @GetMapping("/update") + public Product update(@RequestParam("id") Long id, + @RequestParam("name") String name, + @RequestParam("price") Integer price, + @RequestParam("count") Integer[] count, + @RequestParam("comp") Long[] comp){ + productService.updateProduct(id, name, price); + productComponentsService.removeAll(id, comp); + Product product = productService.findProduct(id); + for(int i = 0; i < comp.length; i++){ + productComponentsService.addProductComponents(product, componentService.findComponent(comp[i]),count[i]); + } + return product; + } + @GetMapping("/remove") + public Product remove(@RequestParam("id") Long id){ + Product product = productService.findProduct(id); + productComponentsService.deleteProduct(product); + return productService.deleteProduct(id); + } + @GetMapping("/removeAll") + public void remove(){ + productComponentsService.deleteAllProduct(); + productService.deleteAllProduct(); + } + @GetMapping("/find") + public Product find(@RequestParam("id") Long id){ + return productService.findProduct(id); + } + @GetMapping("/findAll") + public List findAll(){ + return productService.findAllProduct(); + } +} diff --git a/src/main/java/ip/labwork/student/model/Component.java b/src/main/java/ip/labwork/student/model/Component.java index 782c094..db27e9b 100644 --- a/src/main/java/ip/labwork/student/model/Component.java +++ b/src/main/java/ip/labwork/student/model/Component.java @@ -3,8 +3,9 @@ package ip.labwork.student.model; import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; -import java.util.Set; @Entity public class Component { @@ -17,7 +18,7 @@ public class Component { @OneToMany(mappedBy = "component", cascade = CascadeType.ALL) @JsonIgnore - private Set products; + private List products; public Component() { } @@ -46,14 +47,24 @@ public class Component { this.price = price; } - public Set getProducts() { + public List getProducts() { return products; } - public void setProducts(Set products) { + public void setProducts(List products) { this.products = products; } - + public void addProduct(ProductComponents productComponents){ + if (products == null){ + products = new ArrayList<>(); + } + if (!products.contains(productComponents)) + this.products.add(productComponents); + } + public void removeProduct(ProductComponents productComponents){ + if (products.contains(productComponents)) + this.products.remove(productComponents); + } @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/ip/labwork/student/model/Order.java b/src/main/java/ip/labwork/student/model/Order.java new file mode 100644 index 0000000..214e6a4 --- /dev/null +++ b/src/main/java/ip/labwork/student/model/Order.java @@ -0,0 +1,85 @@ +package ip.labwork.student.model; + +import jakarta.persistence.*; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Objects; + +@Entity +@Table(name = "orders") +public class Order { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private Date date; + private Integer price; + @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + private List products; + public Order(){ + + } + + public Order(Long id, Date date, Integer price, List products) { + this.id = id; + this.date = date; + this.price = price; + this.products = products; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public Integer getPrice() { + return price; + } + + public void setPrice(Integer price) { + this.price = price; + } + public List getProducts() { + return products; + } + + public void setProducts(List products) { + this.products = products; + } + + public void addProduct(OrderProducts orderProducts){ + if (products == null){ + this.products = new ArrayList<>(); + } + if (!products.contains(orderProducts)) + this.products.add(orderProducts); + } + public void removeProducts(OrderProducts orderProducts){ + if (products.contains(orderProducts)) + this.products.remove(orderProducts); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Order order)) return false; + return Objects.equals(getId(), order.getId()) && Objects.equals(getDate(), order.getDate()) && Objects.equals(getPrice(), order.getPrice()); + } + + @Override + public int hashCode() { + return Objects.hash(getId(), getDate(), getPrice()); + } +} diff --git a/src/main/java/ip/labwork/student/model/OrderProducts.java b/src/main/java/ip/labwork/student/model/OrderProducts.java new file mode 100644 index 0000000..1cc0456 --- /dev/null +++ b/src/main/java/ip/labwork/student/model/OrderProducts.java @@ -0,0 +1,63 @@ +package ip.labwork.student.model; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import jakarta.persistence.*; + +@Entity +public class OrderProducts { + @EmbeddedId + private OrderProductsKey id; + @ManyToOne + @MapsId("productId") + @JoinColumn(name = "product_id") + private Product product; + @ManyToOne + @MapsId("orderId") + @JoinColumn(name = "order_id") + @JsonIgnore + private Order order; + private Integer count; + public OrderProducts() { + } + + public OrderProducts(Order order, Product product, Integer count) { + this.order = order; + this.id = new OrderProductsKey(product.getId(), order.getId()); + this.id.setOrderId(order.getId()); + this.id.setProductId(product.getId()); + this.product = product; + this.count = count; + } + + public OrderProductsKey getId() { + return id; + } + + public void setId(OrderProductsKey id) { + this.id = id; + } + + public Order getOrder() { + return order; + } + + public void setOrder(Order order) { + this.order = order; + } + + public Product getProduct() { + return product; + } + + public void setProduct(Product product) { + this.product = product; + } + + public Integer getCount() { + return count; + } + + public void setCount(Integer count) { + this.count = count; + } +} diff --git a/src/main/java/ip/labwork/student/model/OrderProductsKey.java b/src/main/java/ip/labwork/student/model/OrderProductsKey.java new file mode 100644 index 0000000..339cf4c --- /dev/null +++ b/src/main/java/ip/labwork/student/model/OrderProductsKey.java @@ -0,0 +1,48 @@ +package ip.labwork.student.model; + +import jakarta.persistence.Embeddable; + +import java.io.Serializable; +import java.util.Objects; + +@Embeddable +public class OrderProductsKey implements Serializable { + private Long productId; + private Long orderId; + + public OrderProductsKey() { + } + + public OrderProductsKey(Long productId, Long orderId) { + this.productId = productId; + this.orderId = orderId; + } + + public Long getProductId() { + return productId; + } + + public void setProductId(Long productId) { + this.productId = productId; + } + + public Long getOrderId() { + return orderId; + } + + public void setOrderId(Long orderId) { + this.orderId = orderId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof OrderProductsKey that)) return false; + return Objects.equals(getProductId(), that.getProductId()) && Objects.equals(getOrderId(), that.getOrderId()); + } + + @Override + public int hashCode() { + return Objects.hash(getProductId(), getOrderId()); + } +} \ No newline at end of file diff --git a/src/main/java/ip/labwork/student/model/Product.java b/src/main/java/ip/labwork/student/model/Product.java index 49a89f1..79094ac 100644 --- a/src/main/java/ip/labwork/student/model/Product.java +++ b/src/main/java/ip/labwork/student/model/Product.java @@ -1,8 +1,11 @@ package ip.labwork.student.model; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; +import org.springframework.data.domain.Sort; -import java.util.HashSet; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; import java.util.Set; @@ -14,13 +17,18 @@ public class Product { private String productName; private Integer price; + @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + private List components; + @OneToMany(mappedBy = "product", cascade = CascadeType.ALL) - private Set components = new HashSet<>(); + @JsonIgnore + private List orders; + public Product(){ } - public Product(String productName, Integer price, Set components) { + public Product(String productName, Integer price, List components) { this.productName = productName; this.price = price; this.components = components; @@ -46,11 +54,11 @@ public class Product { this.price = price; } - public Set getComponents() { + public List getComponents() { return components; } - public void setComponents(Set components) { + public void setComponents(List components) { this.components = components; } @@ -61,11 +69,37 @@ public class Product { } public void addComponent(ProductComponents productComponents){ - this.components.add(productComponents); + if (components == null){ + this.components = new ArrayList<>(); + } + if (!components.contains(productComponents)) + this.components.add(productComponents); } public void removeComponent(ProductComponents productComponents){ - this.components.remove(productComponents); + if (components.contains(productComponents)) + this.components.remove(productComponents); } + + public List getOrders() { + return orders; + } + + public void setOrders(List orders) { + this.orders = orders; + } + public void addOrder(OrderProducts orderProducts){ + if (orders == null){ + orders = new ArrayList<>(); + } + if (!orders.contains(orderProducts)) + this.orders.add(orderProducts); + } + public void removeOrder(OrderProducts orderProducts){ + if (orders.contains(orderProducts)) + this.orders.remove(orderProducts); + } + + @Override public boolean equals(Object o) { if (this == o) return true; @@ -87,4 +121,5 @@ public class Product { ", price='" + price + '\'' + '}'; } + } diff --git a/src/main/java/ip/labwork/student/model/ProductComponents.java b/src/main/java/ip/labwork/student/model/ProductComponents.java index 9bd5404..3f1aa37 100644 --- a/src/main/java/ip/labwork/student/model/ProductComponents.java +++ b/src/main/java/ip/labwork/student/model/ProductComponents.java @@ -22,6 +22,7 @@ public class ProductComponents { public ProductComponents(Component component, Product product, Integer count) { this.component = component; + this.id = new ProductComponentsKey(product.getId(), component.getId()); this.id.setComponentId(component.getId()); this.id.setProductId(product.getId()); this.product = product; diff --git a/src/main/java/ip/labwork/student/service/OrderProductsService.java b/src/main/java/ip/labwork/student/service/OrderProductsService.java new file mode 100644 index 0000000..cfdd10e --- /dev/null +++ b/src/main/java/ip/labwork/student/service/OrderProductsService.java @@ -0,0 +1,77 @@ +package ip.labwork.student.service; + +import ip.labwork.student.model.*; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +public class OrderProductsService { + @PersistenceContext + private EntityManager em; + + @Transactional + public OrderProducts addOrderProducts(Order order, Product product, Integer count) { + final OrderProducts orderProducts = new OrderProducts(order, product, count); + order.addProduct(orderProducts); + product.addOrder(orderProducts); + em.persist(orderProducts); + return orderProducts; + } + + /* @Transactional(readOnly = true) + public Component findProductComponent(Long productId, Long componentId) { + em.find(Product.class, id); + List productComponentsList = em.createQuery("select pc from ProductComponents pc", ProductComponents.class) + .getResultList(); + final ProductComponentsKey productComponentsKey = new ProductComponentsKey(id,) + final ProductComponents productComponents = em.find(ProductComponents.class, id); + if (component == null) { + throw new EntityNotFoundException(String.format("Component with id [%s] is not found", id)); + } + return component; + }*/ + + @Transactional(readOnly = true) + public List findAllOrderProducts(Long id) { + return em.createQuery("select o from OrderProducts o where id.orderId = " + id, OrderProducts.class) + .getResultList(); + } + + /* @Transactional + public ProductComponents updateProduct(Product product, Component component, Integer Count) { + + //final Component currentComponent = fin(id); + currentComponent.setComponentName(ProductName); + currentComponent.setPrice(Count); + return em.merge(currentComponent); + }*/ + @Transactional + public void deleteOrder(Order order) { + int size = order.getProducts().size(); + for (int i = 0; i < size; i++){ + OrderProducts temp = order.getProducts().get(0); + temp.getProduct().removeOrder(temp); + temp.getProduct().removeOrder(temp); + em.remove(temp); + } + } + @Transactional + public void deleteAllOrder() { + em.createQuery("delete from OrderProducts").executeUpdate(); + } + @Transactional + public void removeAll(Long id, Long[] prodid) { + /*em.createQuery("delete from ProductComponents p where p.id.productId = " + id + " and p.id.componentId not in "+ compid).executeUpdate(); + Product product = em.find(Product.class, id); + product.getComponents().clear(); + int s = 5;*/ + List temp = findAllOrderProducts(id); + for(int i = 0; i < temp.size(); i++){ + em.remove(temp.get(i)); + } + } +} diff --git a/src/main/java/ip/labwork/student/service/OrderService.java b/src/main/java/ip/labwork/student/service/OrderService.java new file mode 100644 index 0000000..4183c87 --- /dev/null +++ b/src/main/java/ip/labwork/student/service/OrderService.java @@ -0,0 +1,60 @@ +package ip.labwork.student.service; + +import ip.labwork.student.model.Order; +import ip.labwork.student.model.Product; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; +import java.util.List; + +@Service +public class OrderService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Order addOrder(Date date, Integer price){ + Order order = new Order(); + order.setDate(date); + order.setPrice(price); + em.persist(order); + return order; + } + @Transactional(readOnly = true) + public Order findOrder(Long id) { + final Order order = em.find(Order.class, id); + if (order == null) { + throw new EntityNotFoundException(String.format("Order with id [%s] is not found", id)); + } + return order; + } + + @Transactional(readOnly = true) + public List findAllOrder() { + return em.createQuery("select o from Order o", Order.class) + .getResultList(); + } + + @Transactional + public Order updateOrder(Long id, Date date, Integer price) { + final Order currentOrder = findOrder(id); + currentOrder.setDate(date); + currentOrder.setPrice(price); + return em.merge(currentOrder); + } + + @Transactional + public Order deleteOrder(Long id) { + final Order currentOrder = findOrder(id); + em.remove(currentOrder); + return currentOrder; + } + @Transactional + public void deleteAllOrder() { + em.createQuery("delete from Order").executeUpdate(); + } +} \ No newline at end of file diff --git a/src/main/java/ip/labwork/student/service/ProductComponentsService.java b/src/main/java/ip/labwork/student/service/ProductComponentsService.java new file mode 100644 index 0000000..12ffc65 --- /dev/null +++ b/src/main/java/ip/labwork/student/service/ProductComponentsService.java @@ -0,0 +1,83 @@ +package ip.labwork.student.service; + +import ip.labwork.student.model.Component; +import ip.labwork.student.model.Product; +import ip.labwork.student.model.ProductComponents; +import ip.labwork.student.model.ProductComponentsKey; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +import java.util.List; +import java.util.Objects; + +@Service +public class ProductComponentsService { + @PersistenceContext + private EntityManager em; + + @Transactional + public ProductComponents addProductComponents(Product product, Component component, Integer count) { + final ProductComponents productComponents = new ProductComponents(component, product, count); + product.addComponent(productComponents); + component.addProduct(productComponents); + em.persist(productComponents); + return productComponents; + } + + /* @Transactional(readOnly = true) + public Component findProductComponent(Long productId, Long componentId) { + em.find(Product.class, id); + List productComponentsList = em.createQuery("select pc from ProductComponents pc", ProductComponents.class) + .getResultList(); + final ProductComponentsKey productComponentsKey = new ProductComponentsKey(id,) + final ProductComponents productComponents = em.find(ProductComponents.class, id); + if (component == null) { + throw new EntityNotFoundException(String.format("Component with id [%s] is not found", id)); + } + return component; + }*/ + + @Transactional(readOnly = true) + public List findAllProductComponents(Long id) { + return em.createQuery("select p from ProductComponents p where id.productId = " + id, ProductComponents.class) + .getResultList(); + } + + /* @Transactional + public ProductComponents updateProduct(Product product, Component component, Integer Count) { + + //final Component currentComponent = fin(id); + currentComponent.setComponentName(ProductName); + currentComponent.setPrice(Count); + return em.merge(currentComponent); + }*/ + @Transactional + public void deleteProduct(Product product) { + int size = product.getComponents().size(); + for (int i = 0; i < size; i++){ + ProductComponents temp = product.getComponents().get(0); + temp.getComponent().removeProduct(temp); + temp.getProduct().removeComponent(temp); + em.remove(temp); + } + } + @Transactional + public void deleteAllProduct() { + em.createQuery("delete from ProductComponents").executeUpdate(); + } + @Transactional + public void removeAll(Long id, Long[] compid) { + /*em.createQuery("delete from ProductComponents p where p.id.productId = " + id + " and p.id.componentId not in "+ compid).executeUpdate(); + Product product = em.find(Product.class, id); + product.getComponents().clear(); + int s = 5;*/ + List temp = findAllProductComponents(id); + for(int i = 0; i < temp.size(); i++){ + em.remove(temp.get(i)); + } + } +} diff --git a/src/main/java/ip/labwork/student/service/ProductService.java b/src/main/java/ip/labwork/student/service/ProductService.java index 4926036..d7883c8 100644 --- a/src/main/java/ip/labwork/student/service/ProductService.java +++ b/src/main/java/ip/labwork/student/service/ProductService.java @@ -12,8 +12,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; @Service public class ProductService { @@ -21,25 +19,12 @@ public class ProductService { private EntityManager em; @Transactional - public Product addProduct(String productName, Integer price, Set components){ - Product product = new Product(productName,price,components); - Product newProduct = new Product(); - newProduct.setProductName(product.getProductName()); - newProduct.setPrice(product.getPrice()); - newProduct.getComponents().addAll((product.getComponents() - .stream() - .map(productComponents -> { - Component component = em.find(Component.class, productComponents.getComponent().getId()); - ProductComponents newProductComponents = new ProductComponents(); - newProductComponents.setComponent(component); - newProductComponents.setProduct(newProduct); - newProductComponents.setCount(productComponents.getCount()); - return newProductComponents; - }) - .collect(Collectors.toSet()) - )); - em.persist(newProduct); - return newProduct; + public Product addProduct(String productName, Integer price){ + Product product = new Product(); + product.setProductName(productName); + product.setPrice(price); + em.persist(product); + return product; } @Transactional(readOnly = true) public Product findProduct(Long id) { @@ -52,15 +37,12 @@ public class ProductService { @Transactional(readOnly = true) public List findAllProduct() { - return em.createQuery("select s from Product s", Product.class) + return em.createQuery("select p from Product p", Product.class) .getResultList(); } @Transactional public Product updateProduct(Long id, String ProductName, Integer Count) { - if (!StringUtils.hasText(ProductName) || Count != 0) { - throw new IllegalArgumentException("Product is null or empty"); - } final Product currentProduct = findProduct(id); currentProduct.setProductName(ProductName); currentProduct.setPrice(Count); diff --git a/src/test/java/ip/labwork/JpaStudentTests.java b/src/test/java/ip/labwork/JpaStudentTests.java index 74dd796..2ef4167 100644 --- a/src/test/java/ip/labwork/JpaStudentTests.java +++ b/src/test/java/ip/labwork/JpaStudentTests.java @@ -22,7 +22,7 @@ public class JpaStudentTests { ComponentService componentService; @Autowired ProductService productService; - @Test + /*@Test void test(){ Component component = componentService.addComponent("Помидор", 10); Component component1 = componentService.addComponent("Огурец", 20); @@ -41,5 +41,5 @@ public class JpaStudentTests { productService.addProduct("Гамбургер", 100, temp); productService.check(); componentService.check(); - } + }*/ } \ No newline at end of file