diff --git a/backend/ipLab/data.mv.db b/backend/ipLab/data.mv.db index 86807f4..2bdee94 100644 Binary files a/backend/ipLab/data.mv.db and b/backend/ipLab/data.mv.db differ diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Customer.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Customer.java index cbf92db..6605858 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Customer.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Customer.java @@ -19,7 +19,12 @@ public class Customer { private String middleName; @OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL) private List orders; - + public void AddOrdered(Ordered ordered){ + this.orders.add(ordered); + if (ordered.getCustomer() != this){ + ordered.setCustomer(this); + } + } public Customer(){} public Customer(String lastName, String firstName, String middleName){ diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Ordered.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Ordered.java index f307076..007dd5b 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Ordered.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Ordered.java @@ -20,10 +20,7 @@ public class Ordered { @Column private int quantity; public Ordered(){} - public Ordered(Store store, Product product, Customer customer, int quantity){ - this.store = store; - this.product = product; - this.customer = customer; + public Ordered(int quantity){ this.quantity = quantity; } @@ -55,6 +52,14 @@ public class Ordered { this.quantity = quantity; } + public void setStore(Store store) { + this.store = store; + } + + public void setCustomer(Customer customer) { + this.customer = customer; + } + @Override public boolean equals(Object obj) { if (this == obj) return true; diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Product.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Product.java index 53c0980..70cf9d0 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Product.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Product.java @@ -28,6 +28,12 @@ public class Product { this.name = name; this.orders = new ArrayList(); } + public void AddOrdered(Ordered ordered){ + this.orders.add(ordered); + if (ordered.getProduct() != this){ + ordered.setProduct(this); + } + } public Long getId() { return id; diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Store.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Store.java index 130974e..b9ddadb 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Store.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Store.java @@ -1,5 +1,6 @@ package com.example.ipLab.StoreDataBase.Model; +import com.example.ipLab.StoreDataBase.Service.ProductService; import jakarta.persistence.*; import java.util.ArrayList; @@ -39,6 +40,18 @@ public class Store { public List getProducts() { return products; } + public void AddProduct(Product product){ + this.products.add(product); + if (product.getStore() != this){ + product.setStore(this); + } + } + public void AddOrdered(Ordered ordered){ + this.orders.add(ordered); + if (ordered.getStore() != this){ + ordered.setStore(this); + } + } public void setStoreName(String storeName) { this.storeName = storeName; diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/OrderService.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/OrderService.java index ea29793..7f2d506 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/OrderService.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/OrderService.java @@ -20,10 +20,10 @@ public class OrderService { @Transactional public Ordered addOrder(Store store, Product product, Customer customer, int quantity){ - final Ordered order = new Ordered(store, product, customer, quantity); - order.getStore().getOrders().add(order); - order.getCustomer().getOrders().add(order); - order.getProduct().getOrders().add(order); + final Ordered order = new Ordered(quantity); + product.AddOrdered(order); + customer.AddOrdered(order); + store.AddOrdered(order); em.persist(order); return order; } diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/ProductService.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/ProductService.java new file mode 100644 index 0000000..9ba3db5 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/ProductService.java @@ -0,0 +1,72 @@ +package com.example.ipLab.StoreDataBase.Service; + +import com.example.ipLab.StoreDataBase.Model.Customer; +import com.example.ipLab.StoreDataBase.Model.Ordered; +import com.example.ipLab.StoreDataBase.Model.Product; +import com.example.ipLab.StoreDataBase.Model.Store; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import jakarta.transaction.Transactional; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import java.util.List; +@Service +public class ProductService { + @PersistenceContext + private EntityManager em; + @Transactional + public Product addProduct(String productName){ + if (!StringUtils.hasText(productName)){ + throw new IllegalArgumentException("Product name is null or empty"); + } + final Product product = new Product(productName); + em.persist(product); + return product; + } + + @Transactional() + public Product getProduct(Long id){ + Product product = em.find(Product.class, id); + if (product == null){ + throw new EntityNotFoundException(String.format("Product with id = %s isn't found", id)); + } + em.persist(product); + return product; + } + + @Transactional + public List getAllProducts(){ + return em.createQuery("get p from Product p", Product.class).getResultList(); + } + + @Transactional + public Product updateProduct(Long id, String productName){ + if (!StringUtils.hasText(productName)){ + throw new IllegalArgumentException("Product name is null or empty"); + } + final Product product = getProduct(id); + if (product == null){ + throw new EntityNotFoundException(String.format("Product with id = %s isn't found", id)); + } + product.setName(productName); + return em.merge(product); + } + + @Transactional + public Product deleteProduct(Long id){ + final Product product = getProduct(id); + if (product == null){ + throw new EntityNotFoundException(String.format("Product with id = %s isn't found", id)); + } + Store store = product.getStore(); + if (store != null) store.getProducts().remove(product); + em.remove(product); + return product; + } + @Transactional + public void deleteAllProducts(){ + em.createQuery("delete from Customer"); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/StoreService.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/StoreService.java index 04f142d..3622cc8 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/StoreService.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/StoreService.java @@ -16,6 +16,11 @@ import java.util.List; public class StoreService { @PersistenceContext private EntityManager em; + private ProductService productService; + + public StoreService(ProductService productService){ + this.productService = productService; + } @Transactional public Store addStore(String storeName){ @@ -64,69 +69,45 @@ public class StoreService { //product section @Transactional - public Product addProduct(Long storeId, String productName){ - if (!StringUtils.hasText(productName)){ - throw new IllegalArgumentException("Product name is null or empty"); - } - Store store = em.find(Store.class, storeId); - if (store == null){ - throw new EntityNotFoundException(String.format("Store with id = %s isn't found", storeId)); - } - final Product product = new Product(productName); - em.persist(product); - store.getProducts().add(product); - product.setStore(store); + public Product addProduct(Long storeId, Long productId){ + Store store = getStore(storeId); + Product product = productService.getProduct(productId); + store.AddProduct(product); + em.persist(store); return product; } @Transactional() - public Product getProduct(Long productId, Long storeId){ - Store store = em.find(Store.class, storeId); + public Product getProductFromStore(Long productId, Long storeId){ + Store store = getStore(storeId); var prFind = store.getProducts().stream().filter(pr -> pr.getId().equals(productId)).findFirst(); if (prFind.isPresent()) { return prFind.get(); } - else throw new EntityNotFoundException(String.format("Product with id = %s isn't found", productId)); + else throw new EntityNotFoundException(String.format("Product with id = %s isn't found in store with id = %s", productId, storeId)); } @Transactional - public List getAllProducts(Long storeId){ - return em.find(Store.class, storeId).getProducts(); + public List getAllProductsFromStore(Long storeId) { + Store store = getStore(storeId); + return store.getProducts(); } @Transactional - public Product updateProduct(Long storeId, Long productId, String productName){ - if (!StringUtils.hasText(productName)){ - throw new IllegalArgumentException("Product name is null or empty"); - } - final Product product = getProduct(productId, storeId); - if (product == null){ - throw new EntityNotFoundException(String.format("Product with id = %s isn't found", productId)); - } - product.setName(productName); - return em.merge(product); - } - - @Transactional - public Product deleteProduct(Long storeId, Long productId){ - final Product product = getProduct(productId, storeId); - if (product == null){ - throw new EntityNotFoundException(String.format("Product with id = %s isn't found", productId)); - } - Store store = product.getStore(); - if (store != null) store.getProducts().remove(product); + public Product deleteProductFromStore(Long storeId, Long productId){ + Store store = getStore(storeId); + Product product = getProductFromStore(productId, storeId); + store.getProducts().remove(product); em.remove(product); return product; } @Transactional public void deleteAllProducts(Long storeId){ - Store store = em.find(Store.class, storeId); - if (store == null){ - throw new EntityNotFoundException(String.format("Store with id = %s isn't found", storeId)); - } + Store store = getStore(storeId); List storeProducts = store.getProducts(); for (Product pr: storeProducts) { + pr.setStore(null); store.getProducts().remove(pr); em.remove(pr); } diff --git a/backend/ipLab/src/test/java/com/example/ipLab/JpaTests.java b/backend/ipLab/src/test/java/com/example/ipLab/JpaTests.java index 3bad1fb..ca5b5de 100644 --- a/backend/ipLab/src/test/java/com/example/ipLab/JpaTests.java +++ b/backend/ipLab/src/test/java/com/example/ipLab/JpaTests.java @@ -6,6 +6,7 @@ import com.example.ipLab.StoreDataBase.Model.Product; import com.example.ipLab.StoreDataBase.Model.Store; import com.example.ipLab.StoreDataBase.Service.CustomerService; import com.example.ipLab.StoreDataBase.Service.OrderService; +import com.example.ipLab.StoreDataBase.Service.ProductService; import com.example.ipLab.StoreDataBase.Service.StoreService; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -20,8 +21,11 @@ public class JpaTests { CustomerService customerService; @Autowired StoreService storeService; + @Autowired + ProductService productService; @Test void testStore(){ + productService.deleteAllProducts(); orderService.deleteAllOrders(); customerService.deleteAllCustomers(); storeService.deleteAllStores(); @@ -32,6 +36,7 @@ public class JpaTests { storeService.updateStore(store.getId(), "newName"); Assertions.assertEquals("newName", storeService.getStore(store.getId()).getStoreName()); Assertions.assertEquals("newName", storeService.deleteStore(store.getId()).getStoreName()); + productService.deleteAllProducts(); orderService.deleteAllOrders(); customerService.deleteAllCustomers(); storeService.deleteAllStores(); @@ -39,6 +44,7 @@ public class JpaTests { @Test void testCustomer(){ + productService.deleteAllProducts(); orderService.deleteAllOrders(); customerService.deleteAllCustomers(); storeService.deleteAllStores(); @@ -49,6 +55,7 @@ public class JpaTests { Assertions.assertEquals("1", customerService.updateCustomer(c.getId(), c.getLastName(), "1", c.getMiddleName()).getFirstName()); Assertions.assertEquals("1", customerService.deleteCustomer(c.getId()).getFirstName()); + productService.deleteAllProducts(); orderService.deleteAllOrders(); customerService.deleteAllCustomers(); storeService.deleteAllStores(); @@ -56,6 +63,7 @@ public class JpaTests { @Test void testProduct(){ + productService.deleteAllProducts(); orderService.deleteAllOrders(); customerService.deleteAllCustomers(); storeService.deleteAllStores(); @@ -63,13 +71,15 @@ public class JpaTests { Store store = storeService.addStore("example"); Assertions.assertEquals("example", store.getStoreName()); - Product p = storeService.addProduct(store.getId(), "product"); + Product p = productService.addProduct("product"); Assertions.assertEquals("product", p.getName()); - Assertions.assertEquals("product", storeService.getProduct(p.getId(), store.getId()).getName()); - Assertions.assertEquals("productUpd", storeService.updateProduct(store.getId(), p.getId(), "productUpd").getName()); - Assertions.assertEquals("productUpd", storeService.deleteProduct(store.getId(), p.getId()).getName()); + Assertions.assertEquals("product", storeService.addProduct(store.getId(), p.getId()).getName()); + Assertions.assertEquals("product", storeService.getProductFromStore(p.getId(), store.getId()).getName()); + Assertions.assertEquals("productUpd", productService.updateProduct(p.getId(), "productUpd").getName()); + Assertions.assertEquals("productUpd", storeService.deleteProductFromStore(store.getId(), p.getId()).getName()); + productService.deleteAllProducts(); orderService.deleteAllOrders(); customerService.deleteAllCustomers(); storeService.deleteAllStores(); @@ -77,6 +87,7 @@ public class JpaTests { @Test void testOrder(){ + productService.deleteAllProducts(); orderService.deleteAllOrders(); customerService.deleteAllCustomers(); storeService.deleteAllStores(); @@ -84,7 +95,8 @@ public class JpaTests { Store store = storeService.addStore("example"); Assertions.assertEquals("example", store.getStoreName()); - Product p = storeService.addProduct(store.getId(), "product"); + Product p = productService.addProduct("product"); + storeService.addProduct(store.getId(), p.getId()); Assertions.assertEquals("product", p.getName()); Customer c = customerService.addCustomer("1", "2", "3"); @@ -97,6 +109,7 @@ public class JpaTests { Assertions.assertEquals("6", Integer.toString(orderService.updateOrder(order.getId(), 6).getQuantity())); Assertions.assertEquals("6", Integer.toString(orderService.deleteOrder(order.getId()).getQuantity())); + productService.deleteAllProducts(); orderService.deleteAllOrders(); customerService.deleteAllCustomers(); storeService.deleteAllStores();