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>U!d9`Rc@Vip+Y+Lm)^XPuC)le
z7GI#rVMYTi`yp79L#!Xb(O=&VkmvxMZcp0droG->vy*mQgzb2DCGFS;k)%iy`ai*3
zwU&qN(7KiXx5Nv~*&cY~6hs0lA0i=$
zA0i=uA0p31B*gJUcOix!B0rBv2<3B8w+1iEh{E4ydqWcl@vx4@)vfbjQ%|
zLcIol7dKR##~*|a7bwo7Z(EDvJQf?Vbw4QOmk9D$Y4ntmK_4AQaUxF!67t1EHeZks
zkT*=o7Z3c(JLTg_Wv?2nN=T@q`*aJIwOs8^^I~}
zra01YSKn~N{{PkJy2SpUzsxB*UQu;r0l%59TltA^|A$}CiqJS>!mUlj{y$&)|5Mk(
z{%^3J{omk-{h!$Xbz=WtO!j|$F8jZ}IPL!iTVud6;rRFN_+Fm4EUF;06C4
zdH^8n9#=8HC5q1-8KGCg_r$&o3C^ilZG_7}!E|Fc&+(t~kd-Zc#`k(GzYIlBw8V
z0Sm~KA0T|LNKAoj=j63yWF{f<13~xz_MH6y#|Oygb%B8a{yCH@I^)fOAnoxK^o5;!
z!AE&tiZggzwz?PQEVRDP1ATq=hK~^0v4&?wR&|a2zDYMQ_WOyz*dM?MTx0A4&%!J^
zwhBtgHS*8j*ndOW|0DZ*WdFA#{C_~vw=H%7&;L7t{oe}4Jcj=-vuulps9^_)8eE`2
z1=G`@ypGlEYQc2>`$+=)Z{r6a|J8RPiho&Q~^>uXIcexL`YxT1E2}
zLW4kx3fEFB9*mJpR}FTAT=?aAeL7m`w&sGs8RHQTu=Ti%p
zsGL8a!ZUvXpddX~)|;t#KD9C37I`>f14(2T{cH*o>fNhxIFDJ+l2qo}K+gX!BqmDL
zfBakg1rqx|IsZQotN?QUzXT-H;veON!>c!e5*c2-46mCJ75V>1`|oJ~??n55M}_^r
JxB&bA{|69O2owMS
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();