Странно работает удаление

This commit is contained in:
Nikita Sergeev 2023-03-17 18:57:11 +04:00
parent 1b4d36a1e2
commit 8c516e69f1
18 changed files with 393 additions and 454 deletions

View File

@ -1,7 +1,7 @@
package ip.labwork.student.controller;
package ip.labwork.shop.controller;
import ip.labwork.student.model.Component;
import ip.labwork.student.service.ComponentService;
import ip.labwork.shop.model.Component;
import ip.labwork.shop.service.ComponentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@ -21,29 +21,34 @@ public class ComponentController {
@GetMapping("/add")
public Component create(@RequestParam("name") String name,
@RequestParam("price") Integer price){
@RequestParam("price") Integer price) {
return componentService.addComponent(name, price);
}
@GetMapping("/update")
public Component update(@RequestParam("id") Long id,
@RequestParam("name") String name,
@RequestParam("price") Integer price){
@RequestParam("price") Integer price) {
return componentService.updateComponent(id, name, price);
}
@GetMapping("/remove")
public Component remove(@RequestParam("id") Long id){
public Component remove(@RequestParam("id") Long id) {
return componentService.deleteComponent(id);
}
@GetMapping("/removeAll")
public void remove(){
public void remove() {
componentService.deleteAllComponent();
}
@GetMapping("/find")
public Component find(@RequestParam("id") Long id){
public Component find(@RequestParam("id") Long id) {
return componentService.findComponent(id);
}
@GetMapping("/findAll")
public List<Component> findAll(){
public List<Component> findAll() {
return componentService.findAllComponent();
}
}

View File

@ -1,15 +1,13 @@
package ip.labwork.student.controller;
package ip.labwork.shop.controller;
import ip.labwork.student.model.Order;
import ip.labwork.student.model.Product;
import ip.labwork.student.service.*;
import ip.labwork.shop.service.ProductService;
import ip.labwork.shop.model.Order;
import ip.labwork.shop.service.OrderService;
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
@ -17,12 +15,9 @@ import java.util.List;
public class OrderController {
private final OrderService orderService;
private final ProductService productService;
private final OrderProductsService orderProductsService;
public OrderController(OrderService orderService, ProductService productService, OrderProductsService orderProductsService) {
public OrderController(OrderService orderService, ProductService productService) {
this.orderService = orderService;
this.productService = productService;
this.orderProductsService = orderProductsService;
}
@GetMapping("/add")
@ -30,41 +25,22 @@ public class OrderController {
@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;
return orderService.addOrder(date, price, count, productService.findFiltredProducts(prod));
}
@GetMapping("/update")
public Order update(@RequestParam("id") Long id,
@RequestParam("date") Date date,
@RequestParam("date") String date,
@RequestParam("price") Integer price,
@RequestParam("count") Integer[] count,
@RequestParam("prod") Long[] prod){
orderService.updateOrder(id, date, price);
Order order = orderService.findOrder(id);
for(int i = 0; i < prod.length; i++){
orderProductsService.update(order, productService.findProduct(prod[i]),count[i], prod);
}
return order;
return orderService.updateOrder(id, date, price, count, productService.findFiltredProducts(prod));
}
@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")

View File

@ -1,11 +1,8 @@
package ip.labwork.student.controller;
package ip.labwork.shop.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 ip.labwork.shop.service.ProductService;
import ip.labwork.shop.model.Product;
import ip.labwork.shop.service.ComponentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@ -18,12 +15,10 @@ import java.util.List;
public class ProductController {
private final ProductService productService;
private final ComponentService componentService;
private final ProductComponentsService productComponentsService;
public ProductController(ProductService productService, ComponentService componentService, ProductComponentsService productComponentsService) {
public ProductController(ProductService productService, ComponentService componentService) {
this.productService = productService;
this.componentService = componentService;
this.productComponentsService = productComponentsService;
}
@GetMapping("/add")
@ -31,10 +26,7 @@ public class ProductController {
@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;
return productService.addProduct(name, price, count, componentService.findFiltredComponents(comp));
}
@GetMapping("/update")
public Product update(@RequestParam("id") Long id,
@ -42,23 +34,14 @@ public class ProductController {
@RequestParam("price") Integer price,
@RequestParam("count") Integer[] count,
@RequestParam("comp") Long[] comp){
productService.updateProduct(id, name, price);
Product product = productService.findProduct(id);
for(int i = 0; i < comp.length; i++){
productComponentsService.update(product, componentService.findComponent(comp[i]),count[i], comp);
}
return product;
return productService.updateProduct(id, name, price, count, componentService.findFiltredComponents(comp));
}
@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")

View File

@ -1,4 +1,4 @@
package ip.labwork.student.model;
package ip.labwork.shop.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
@ -19,6 +19,7 @@ public class Component {
@OneToMany(mappedBy = "component", cascade = CascadeType.ALL)
@JsonIgnore
private List<ProductComponents> products;
public Component() {
}
@ -54,17 +55,20 @@ public class Component {
public void setProducts(List<ProductComponents> products) {
this.products = products;
}
public void addProduct(ProductComponents productComponents){
if (products == null){
public void addProduct(ProductComponents productComponents) {
if (products == null) {
products = new ArrayList<>();
}
if (!products.contains(productComponents))
this.products.add(productComponents);
}
public void removeProduct(ProductComponents 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;

View File

@ -1,4 +1,4 @@
package ip.labwork.student.model;
package ip.labwork.shop.model;
import jakarta.persistence.*;
@ -21,11 +21,9 @@ public class Order {
}
public Order(Long id, Date date, Integer price, List<OrderProducts> products) {
this.id = id;
public Order(Date date, Integer price) {
this.date = date;
this.price = price;
this.products = products;
}
public Long getId() {

View File

@ -1,4 +1,4 @@
package ip.labwork.student.model;
package ip.labwork.shop.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
@ -17,6 +17,7 @@ public class OrderProducts {
@JsonIgnore
private Order order;
private Integer count;
public OrderProducts() {
}

View File

@ -1,4 +1,4 @@
package ip.labwork.student.model;
package ip.labwork.shop.model;
import jakarta.persistence.Embeddable;

View File

@ -1,13 +1,11 @@
package ip.labwork.student.model;
package ip.labwork.shop.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import org.springframework.data.domain.Sort;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@Entity
public class Product {
@ -25,13 +23,13 @@ public class Product {
private List<OrderProducts> orders;
public Product(){
public Product() {
}
public Product(String productName, Integer price, List<ProductComponents> components) {
public Product(String productName, Integer price) {
this.productName = productName;
this.price = price;
this.components = components;
}
public Long getId() {

View File

@ -1,4 +1,4 @@
package ip.labwork.student.model;
package ip.labwork.shop.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
@ -17,6 +17,7 @@ public class ProductComponents {
@JsonIgnore
private Product product;
private Integer count;
public ProductComponents() {
}

View File

@ -1,4 +1,4 @@
package ip.labwork.student.model;
package ip.labwork.shop.model;
import jakarta.persistence.Embeddable;
@ -9,6 +9,7 @@ import java.util.Objects;
public class ProductComponentsKey implements Serializable {
private Long productId;
private Long componentId;
public ProductComponentsKey() {
}

View File

@ -1,6 +1,6 @@
package ip.labwork.student.service;
package ip.labwork.shop.service;
import ip.labwork.student.model.Component;
import ip.labwork.shop.model.Component;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
@Service
@ -16,11 +17,11 @@ public class ComponentService {
private EntityManager em;
@Transactional
public Component addComponent(String ComponentName, Integer Count) {
if (!StringUtils.hasText(ComponentName) || Count == 0) {
public Component addComponent(String componentName, Integer price) {
if (!StringUtils.hasText(componentName) || price == 0) {
throw new IllegalArgumentException("Component is null or empty");
}
final Component component = new Component(ComponentName, Count);
final Component component = new Component(componentName, price);
em.persist(component);
return component;
}
@ -40,14 +41,26 @@ public class ComponentService {
.getResultList();
}
@Transactional(readOnly = true)
public List<Component> findFiltredComponents(Long[] arr) {
if (arr.length == 0) {
throw new IllegalArgumentException("Array id is empty");
}
List<Component> componentList = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
componentList.add(em.find(Component.class, arr[i]));
}
return componentList;
}
@Transactional
public Component updateComponent(Long id, String ComponentName, Integer Count) {
if (!StringUtils.hasText(ComponentName) || Count == 0) {
public Component updateComponent(Long id, String componentName, Integer price) {
if (!StringUtils.hasText(componentName) || price == 0) {
throw new IllegalArgumentException("Component is null or empty");
}
final Component currentComponent = findComponent(id);
currentComponent.setComponentName(ComponentName);
currentComponent.setPrice(Count);
currentComponent.setComponentName(componentName);
currentComponent.setPrice(price);
return em.merge(currentComponent);
}
@ -58,10 +71,6 @@ public class ComponentService {
return currentComponent;
}
@Transactional
public void check(){
int s = 5;
}
@Transactional
public void deleteAllComponent() {
em.createQuery("delete from Component").executeUpdate();
}

View File

@ -0,0 +1,118 @@
package ip.labwork.shop.service;
import ip.labwork.shop.model.Order;
import ip.labwork.shop.model.OrderProducts;
import ip.labwork.shop.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 org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class OrderService {
@PersistenceContext
private EntityManager em;
@Transactional
public Order addOrder(String date, Integer price, Integer[] count, List<Product> products) {
if (!StringUtils.hasText(date) || price == 0 || count.length == 0 || Arrays.stream(count).filter(c -> c == 0).toList().size() != 0 || products.size() == 0 || products.stream().filter(Objects::isNull).toList().size() != 0 || count.length != products.size()) {
throw new IllegalArgumentException("Order is null or empty");
}
Date correctDate = getDate(date);
final Order order = new Order(correctDate, price);
em.persist(order);
for (int i = 0; i < products.size(); i++) {
final OrderProducts orderProducts = new OrderProducts(order, products.get(i), count[i]);
order.addProduct(orderProducts);
products.get(i).addOrder(orderProducts);
em.persist(orderProducts);
}
return order;
}
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 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<Order> findAllOrder() {
return em.createQuery("select o from Order o", Order.class)
.getResultList();
}
@Transactional
public Order updateOrder(Long id, String date, Integer price, Integer[] count, List<Product> products) {
if (!StringUtils.hasText(date) || price == 0 || count.length == 0 || Arrays.stream(count).filter(c -> c == 0).toList().size() != 0 || products.size() == 0 || products.stream().filter(Objects::isNull).toList().size() != 0 || count.length != products.size()) {
throw new IllegalArgumentException("Order is null or empty");
}
final Order currentOrder = findOrder(id);
currentOrder.setDate(getDate(date));
currentOrder.setPrice(price);
em.merge(currentOrder);
List<OrderProducts> orderProductsList = em.createQuery("select o from OrderProducts o where o.id.orderId = " + id, OrderProducts.class)
.getResultList();
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]);
em.merge(orderProducts);
} else {
final OrderProducts orderProducts = new OrderProducts(currentOrder, products.get(i), count[i]);
currentOrder.addProduct(orderProducts);
products.get(i).addOrder(orderProducts);
em.persist(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));
em.remove(orderProductsList.get(i));
}
return currentOrder;
}
@Transactional
public Order deleteOrder(Long id) {
final Order currentOrder = findOrder(id);
int size = currentOrder.getProducts().size();
for (int i = 0; i < size; i++) {
OrderProducts temp = currentOrder.getProducts().get(0);
temp.getProduct().removeOrder(temp);
temp.getOrder().removeProducts(temp);
em.remove(temp);
}
em.remove(currentOrder);
return currentOrder;
}
@Transactional
public void deleteAllOrder() {
em.createQuery("delete from OrderProducts").executeUpdate();
em.createQuery("delete from Order").executeUpdate();
}
}

View File

@ -0,0 +1,120 @@
package ip.labwork.shop.service;
import ip.labwork.shop.model.Component;
import ip.labwork.shop.model.Product;
import ip.labwork.shop.model.ProductComponents;
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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@Service
public class ProductService {
@PersistenceContext
private EntityManager em;
@Transactional
public Product addProduct(String productName, Integer price, Integer[] count, List<Component> components) {
if (!StringUtils.hasText(productName) || price == 0 || count.length == 0 || Arrays.stream(count).filter(c -> c == 0).toList().size() != 0 || components.size() == 0 || components.stream().filter(Objects::isNull).toList().size() != 0 || count.length != components.size()) {
throw new IllegalArgumentException("Product name is null or empty");
}
final Product product = new Product(productName, price);
em.persist(product);
for (int i = 0; i < components.size(); i++) {
final ProductComponents productComponents = new ProductComponents(components.get(i), product, count[i]);
product.addComponent(productComponents);
components.get(i).addProduct(productComponents);
em.persist(productComponents);
}
return product;
}
@Transactional(readOnly = true)
public Product findProduct(Long id) {
final Product product = em.find(Product.class, id);
if (product == null) {
throw new EntityNotFoundException(String.format("Product with id [%s] is not found", id));
}
return product;
}
@Transactional(readOnly = true)
public List<Product> findAllProduct() {
return em.createQuery("select p from Product p", Product.class)
.getResultList();
}
@Transactional
public Product updateProduct(Long id, String productName, Integer price, Integer[] count, List<Component> components) {
if (!StringUtils.hasText(productName) || price == 0 || count.length == 0 || Arrays.stream(count).filter(c -> c == 0).toList().size() != 0 || components.size() == 0 || components.stream().filter(Objects::isNull).toList().size() != 0 || count.length != components.size()) {
throw new IllegalArgumentException("Product name is null or empty");
}
final Product currentProduct = findProduct(id);
currentProduct.setProductName(productName);
currentProduct.setPrice(price);
em.merge(currentProduct);
List<ProductComponents> productComponentsList = em.createQuery("select p from ProductComponents p where p.id.productId = " + id, ProductComponents.class)
.getResultList();
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]);
em.merge(productComponents);
} else {
final ProductComponents productComponents = new ProductComponents(components.get(i), currentProduct, count[i]);
currentProduct.addComponent(productComponents);
components.get(i).addProduct(productComponents);
em.persist(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));
em.remove(productComponentsList.get(i));
}
return currentProduct;
}
@Transactional
public Product deleteProduct(Long id) {
final Product currentProduct = findProduct(id);
int size = currentProduct.getComponents().size();
for (int i = 0; i < size; i++) {
ProductComponents temp = currentProduct.getComponents().get(0);
temp.getComponent().removeProduct(temp);
temp.getProduct().removeComponent(temp);
em.remove(temp);
}
em.remove(currentProduct);
return currentProduct;
}
@Transactional
public void deleteAllProduct() {
em.createQuery("delete from ProductComponents").executeUpdate();
em.createQuery("delete from Product").executeUpdate();
}
@Transactional
public List<Product> findFiltredProducts(Long[] arr) {
if (arr.length == 0) {
throw new IllegalArgumentException("Array id is empty");
}
List<Product> productList = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
productList.add(em.find(Product.class, arr[i]));
}
return productList;
}
}

View File

@ -1,112 +0,0 @@
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;
import java.util.Objects;
@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<ProductComponents> 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<OrderProducts> 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<OrderProducts> temp = findAllOrderProducts(id);
for(int i = 0; i < temp.size(); i++){
em.remove(temp.get(i));
}
}
public void update(Order order, Product product, Integer count, Long[] prod) {
for (int i = 0; i < prod.length; i++){
List<OrderProducts> tem = em.createQuery("select o from OrderProducts o where o.id.orderId = " + order.getId() + " and o.id.productId = " + product.getId(), OrderProducts.class)
.getResultList();
if (tem.size() != 0){
final OrderProducts orderProducts = tem.get(0);
orderProducts.setCount(count);
em.merge(orderProducts);
}else{
final OrderProducts orderProducts = new OrderProducts(order, product, count);
order.addProduct(orderProducts);
product.addOrder(orderProducts);
em.persist(orderProducts);
}
}
List<OrderProducts> newList = em.createQuery("select o from OrderProducts o where o.id.orderId = " + order.getId(), OrderProducts.class).getResultList();
for(int i =0; i < newList.size(); i++){
boolean flag = false;
for (int j = 0; j < prod.length; j++){
if (Objects.equals(newList.get(i).getId().getProductId(), prod[j])) {
flag = true;
break;
}
}
if (!flag){
newList.get(i).getProduct().removeOrder(newList.get(i));
newList.get(i).getOrder().removeProducts(newList.get(i));
em.remove(newList.get(i));
}
}
}
}

View File

@ -1,60 +0,0 @@
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<Order> 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();
}
}

View File

@ -1,93 +0,0 @@
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.ArrayList;
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 List<ProductComponents> findAllProductComponents(Long id) {
return em.createQuery("select p from ProductComponents p where id.productId = " + id, ProductComponents.class)
.getResultList();
}
@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) {
List<ProductComponents> temp = findAllProductComponents(id);
for(int i = 0; i < temp.size(); i++){
em.remove(temp.get(i));
}
}
@Transactional
public void update(Product product, Component component, Integer count, Long[] comp) {
for (int i = 0; i < comp.length; i++){
List<ProductComponents> tem = em.createQuery("select p from ProductComponents p where p.id.productId = " + product.getId() + " and p.id.componentId = " + component.getId(), ProductComponents.class)
.getResultList();
if (tem.size() != 0){
final ProductComponents productComponents = tem.get(0);
productComponents.setCount(count);
em.merge(productComponents);
}else{
final ProductComponents productComponents = new ProductComponents(component, product, count);
product.addComponent(productComponents);
component.addProduct(productComponents);
em.persist(productComponents);
}
}
List<ProductComponents> newList = em.createQuery("select p from ProductComponents p where p.id.productId = " + product.getId(), ProductComponents.class).getResultList();
for(int i =0; i < newList.size(); i++){
boolean flag = false;
for (int j = 0; j < comp.length; j++){
if (Objects.equals(newList.get(i).getId().getComponentId(), comp[j])) {
flag = true;
break;
}
}
if (!flag){
newList.get(i).getComponent().removeProduct(newList.get(i));
newList.get(i).getProduct().removeComponent(newList.get(i));
em.remove(newList.get(i));
}
}
}
}

View File

@ -1,66 +0,0 @@
package ip.labwork.student.service;
import ip.labwork.student.model.Component;
import ip.labwork.student.model.Product;
import ip.labwork.student.model.ProductComponents;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
public class ProductService {
@PersistenceContext
private EntityManager em;
@Transactional
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) {
final Product product = em.find(Product.class, id);
if (product == null) {
throw new EntityNotFoundException(String.format("Product with id [%s] is not found", id));
}
return product;
}
@Transactional(readOnly = true)
public List<Product> findAllProduct() {
return em.createQuery("select p from Product p", Product.class)
.getResultList();
}
@Transactional
public Product updateProduct(Long id, String ProductName, Integer Count) {
final Product currentProduct = findProduct(id);
currentProduct.setProductName(ProductName);
currentProduct.setPrice(Count);
return em.merge(currentProduct);
}
@Transactional
public Product deleteProduct(Long id) {
final Product currentProduct = findProduct(id);
em.remove(currentProduct);
return currentProduct;
}
@Transactional
public void check(){
int s = 5;
}
@Transactional
public void deleteAllProduct() {
em.createQuery("delete from Product").executeUpdate();
}
}

View File

@ -1,10 +1,12 @@
package ip.labwork;
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.ProductService;
import ip.labwork.shop.model.Component;
import ip.labwork.shop.model.Order;
import ip.labwork.shop.model.Product;
import ip.labwork.shop.service.ComponentService;
import ip.labwork.shop.service.OrderService;
import ip.labwork.shop.service.ProductService;
import jakarta.persistence.EntityNotFoundException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
@ -12,8 +14,9 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@SpringBootTest
public class JpaStudentTests {
@ -22,24 +25,77 @@ public class JpaStudentTests {
ComponentService componentService;
@Autowired
ProductService productService;
/*@Test
void test(){
Component component = componentService.addComponent("Помидор", 10);
Component component1 = componentService.addComponent("Огурец", 20);
Set<ProductComponents> temp = new HashSet<>();
ProductComponents tem = new ProductComponents();
tem.setComponent(component);
tem.setCount(5);
ProductComponents te = new ProductComponents();
te.setComponent(component1);
te.setCount(6);
temp.add(tem);
temp.add(te);
Product product = new Product("Гамбургер", 100, temp);
productService.check();
componentService.check();
productService.addProduct("Гамбургер", 100, temp);
productService.check();
componentService.check();
}*/
@Autowired
OrderService orderService;
@Test
void testCreate() {
componentService.deleteAllComponent();
productService.deleteAllProduct();
orderService.deleteAllOrder();
//TestCreate
final Component component = componentService.addComponent("Огурец", 4);
log.info(component.toString());
Assertions.assertNotNull(component.getId());
List<Component> componentList = new ArrayList<>();
componentList.add(componentService.findComponent(component.getId()));
final Product product = productService.addProduct("Бургер", 100, new Integer[]{ 2 }, componentList);
log.info(product.toString());
Assertions.assertNotNull(product.getId());
List<Product> productList = new ArrayList<>();
productList.add(productService.findProduct(product.getId()));
final Order order = orderService.addOrder(new Date().toString(), 200, new Integer[]{ 3 }, productList);
log.info(order.toString());
Assertions.assertNotNull(order.getId());
/*//TestRead
final Component findComponent = componentService.findComponent(component.getId());
log.info(findComponent.toString());
Assertions.assertEquals(component, findComponent);
final Product findProduct = productService.findProduct(product.getId());
log.info(findProduct.toString());
Assertions.assertEquals(product, findProduct);
final Order findOrder = orderService.findOrder(order.getId());
log.info(findOrder.toString());
Assertions.assertEquals(order, findOrder);
//TestReadAll
final List<Component> components = componentService.findAllComponent();
log.info(components.toString());
Assertions.assertEquals(components.size(), 1);
final List<Product> products = productService.findAllProduct();
log.info(products.toString());
Assertions.assertEquals(products.size(), 1);
final List<Order> orders = orderService.findAllOrder();
log.info(orders.toString());
Assertions.assertEquals(orders.size(), 1);
//TestReadNotFound
componentService.deleteAllComponent();
productService.deleteAllProduct();
orderService.deleteAllOrder();
Assertions.assertThrows(EntityNotFoundException.class, () -> componentService.findComponent(-1L));
Assertions.assertThrows(EntityNotFoundException.class, () -> productService.findProduct(-1L));
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(-1L));
//TestReadAllEmpty
final List<Component> newComponents = componentService.findAllComponent();
log.info(newComponents.toString());
Assertions.assertEquals(newComponents.size(), 0);
final List<Product> newProducts = productService.findAllProduct();
log.info(newProducts.toString());
Assertions.assertEquals(newProducts.size(), 0);
final List<Order> newOrders = orderService.findAllOrder();
log.info(newOrders.toString());
Assertions.assertEquals(newOrders.size(), 0);*/
}
}