Третья лабораторная работа. Фикс сервиса продукта + фикс установки связей OneToMany
This commit is contained in:
parent
49195ca6b4
commit
c6dff060b0
Binary file not shown.
@ -19,7 +19,12 @@ public class Customer {
|
|||||||
private String middleName;
|
private String middleName;
|
||||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
|
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
|
||||||
private List<Ordered> orders;
|
private List<Ordered> orders;
|
||||||
|
public void AddOrdered(Ordered ordered){
|
||||||
|
this.orders.add(ordered);
|
||||||
|
if (ordered.getCustomer() != this){
|
||||||
|
ordered.setCustomer(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
public Customer(){}
|
public Customer(){}
|
||||||
|
|
||||||
public Customer(String lastName, String firstName, String middleName){
|
public Customer(String lastName, String firstName, String middleName){
|
||||||
|
@ -20,10 +20,7 @@ public class Ordered {
|
|||||||
@Column
|
@Column
|
||||||
private int quantity;
|
private int quantity;
|
||||||
public Ordered(){}
|
public Ordered(){}
|
||||||
public Ordered(Store store, Product product, Customer customer, int quantity){
|
public Ordered(int quantity){
|
||||||
this.store = store;
|
|
||||||
this.product = product;
|
|
||||||
this.customer = customer;
|
|
||||||
this.quantity = quantity;
|
this.quantity = quantity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,6 +52,14 @@ public class Ordered {
|
|||||||
this.quantity = quantity;
|
this.quantity = quantity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setStore(Store store) {
|
||||||
|
this.store = store;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomer(Customer customer) {
|
||||||
|
this.customer = customer;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj) return true;
|
if (this == obj) return true;
|
||||||
|
@ -28,6 +28,12 @@ public class Product {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
this.orders = new ArrayList<Ordered>();
|
this.orders = new ArrayList<Ordered>();
|
||||||
}
|
}
|
||||||
|
public void AddOrdered(Ordered ordered){
|
||||||
|
this.orders.add(ordered);
|
||||||
|
if (ordered.getProduct() != this){
|
||||||
|
ordered.setProduct(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.example.ipLab.StoreDataBase.Model;
|
package com.example.ipLab.StoreDataBase.Model;
|
||||||
|
|
||||||
|
import com.example.ipLab.StoreDataBase.Service.ProductService;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -39,6 +40,18 @@ public class Store {
|
|||||||
public List<Product> getProducts() {
|
public List<Product> getProducts() {
|
||||||
return products;
|
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) {
|
public void setStoreName(String storeName) {
|
||||||
this.storeName = storeName;
|
this.storeName = storeName;
|
||||||
|
@ -20,10 +20,10 @@ public class OrderService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Ordered addOrder(Store store, Product product, Customer customer, int quantity){
|
public Ordered addOrder(Store store, Product product, Customer customer, int quantity){
|
||||||
final Ordered order = new Ordered(store, product, customer, quantity);
|
final Ordered order = new Ordered(quantity);
|
||||||
order.getStore().getOrders().add(order);
|
product.AddOrdered(order);
|
||||||
order.getCustomer().getOrders().add(order);
|
customer.AddOrdered(order);
|
||||||
order.getProduct().getOrders().add(order);
|
store.AddOrdered(order);
|
||||||
em.persist(order);
|
em.persist(order);
|
||||||
return order;
|
return order;
|
||||||
}
|
}
|
||||||
|
@ -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<Product> 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");
|
||||||
|
}
|
||||||
|
}
|
@ -16,6 +16,11 @@ import java.util.List;
|
|||||||
public class StoreService {
|
public class StoreService {
|
||||||
@PersistenceContext
|
@PersistenceContext
|
||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
private ProductService productService;
|
||||||
|
|
||||||
|
public StoreService(ProductService productService){
|
||||||
|
this.productService = productService;
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Store addStore(String storeName){
|
public Store addStore(String storeName){
|
||||||
@ -64,69 +69,45 @@ public class StoreService {
|
|||||||
|
|
||||||
//product section
|
//product section
|
||||||
@Transactional
|
@Transactional
|
||||||
public Product addProduct(Long storeId, String productName){
|
public Product addProduct(Long storeId, Long productId){
|
||||||
if (!StringUtils.hasText(productName)){
|
Store store = getStore(storeId);
|
||||||
throw new IllegalArgumentException("Product name is null or empty");
|
Product product = productService.getProduct(productId);
|
||||||
}
|
store.AddProduct(product);
|
||||||
Store store = em.find(Store.class, storeId);
|
em.persist(store);
|
||||||
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);
|
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional()
|
@Transactional()
|
||||||
public Product getProduct(Long productId, Long storeId){
|
public Product getProductFromStore(Long productId, Long storeId){
|
||||||
Store store = em.find(Store.class, storeId);
|
Store store = getStore(storeId);
|
||||||
var prFind = store.getProducts().stream().filter(pr -> pr.getId().equals(productId)).findFirst();
|
var prFind = store.getProducts().stream().filter(pr -> pr.getId().equals(productId)).findFirst();
|
||||||
if (prFind.isPresent()) {
|
if (prFind.isPresent()) {
|
||||||
return prFind.get();
|
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
|
@Transactional
|
||||||
public List<Product> getAllProducts(Long storeId){
|
public List<Product> getAllProductsFromStore(Long storeId) {
|
||||||
return em.find(Store.class, storeId).getProducts();
|
Store store = getStore(storeId);
|
||||||
|
return store.getProducts();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Product updateProduct(Long storeId, Long productId, String productName){
|
public Product deleteProductFromStore(Long storeId, Long productId){
|
||||||
if (!StringUtils.hasText(productName)){
|
Store store = getStore(storeId);
|
||||||
throw new IllegalArgumentException("Product name is null or empty");
|
Product product = getProductFromStore(productId, storeId);
|
||||||
}
|
store.getProducts().remove(product);
|
||||||
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);
|
|
||||||
em.remove(product);
|
em.remove(product);
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAllProducts(Long storeId){
|
public void deleteAllProducts(Long storeId){
|
||||||
Store store = em.find(Store.class, storeId);
|
Store store = getStore(storeId);
|
||||||
if (store == null){
|
|
||||||
throw new EntityNotFoundException(String.format("Store with id = %s isn't found", storeId));
|
|
||||||
}
|
|
||||||
List<Product> storeProducts = store.getProducts();
|
List<Product> storeProducts = store.getProducts();
|
||||||
for (Product pr:
|
for (Product pr:
|
||||||
storeProducts) {
|
storeProducts) {
|
||||||
|
pr.setStore(null);
|
||||||
store.getProducts().remove(pr);
|
store.getProducts().remove(pr);
|
||||||
em.remove(pr);
|
em.remove(pr);
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import com.example.ipLab.StoreDataBase.Model.Product;
|
|||||||
import com.example.ipLab.StoreDataBase.Model.Store;
|
import com.example.ipLab.StoreDataBase.Model.Store;
|
||||||
import com.example.ipLab.StoreDataBase.Service.CustomerService;
|
import com.example.ipLab.StoreDataBase.Service.CustomerService;
|
||||||
import com.example.ipLab.StoreDataBase.Service.OrderService;
|
import com.example.ipLab.StoreDataBase.Service.OrderService;
|
||||||
|
import com.example.ipLab.StoreDataBase.Service.ProductService;
|
||||||
import com.example.ipLab.StoreDataBase.Service.StoreService;
|
import com.example.ipLab.StoreDataBase.Service.StoreService;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -20,8 +21,11 @@ public class JpaTests {
|
|||||||
CustomerService customerService;
|
CustomerService customerService;
|
||||||
@Autowired
|
@Autowired
|
||||||
StoreService storeService;
|
StoreService storeService;
|
||||||
|
@Autowired
|
||||||
|
ProductService productService;
|
||||||
@Test
|
@Test
|
||||||
void testStore(){
|
void testStore(){
|
||||||
|
productService.deleteAllProducts();
|
||||||
orderService.deleteAllOrders();
|
orderService.deleteAllOrders();
|
||||||
customerService.deleteAllCustomers();
|
customerService.deleteAllCustomers();
|
||||||
storeService.deleteAllStores();
|
storeService.deleteAllStores();
|
||||||
@ -32,6 +36,7 @@ public class JpaTests {
|
|||||||
storeService.updateStore(store.getId(), "newName");
|
storeService.updateStore(store.getId(), "newName");
|
||||||
Assertions.assertEquals("newName", storeService.getStore(store.getId()).getStoreName());
|
Assertions.assertEquals("newName", storeService.getStore(store.getId()).getStoreName());
|
||||||
Assertions.assertEquals("newName", storeService.deleteStore(store.getId()).getStoreName());
|
Assertions.assertEquals("newName", storeService.deleteStore(store.getId()).getStoreName());
|
||||||
|
productService.deleteAllProducts();
|
||||||
orderService.deleteAllOrders();
|
orderService.deleteAllOrders();
|
||||||
customerService.deleteAllCustomers();
|
customerService.deleteAllCustomers();
|
||||||
storeService.deleteAllStores();
|
storeService.deleteAllStores();
|
||||||
@ -39,6 +44,7 @@ public class JpaTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCustomer(){
|
void testCustomer(){
|
||||||
|
productService.deleteAllProducts();
|
||||||
orderService.deleteAllOrders();
|
orderService.deleteAllOrders();
|
||||||
customerService.deleteAllCustomers();
|
customerService.deleteAllCustomers();
|
||||||
storeService.deleteAllStores();
|
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.updateCustomer(c.getId(), c.getLastName(), "1", c.getMiddleName()).getFirstName());
|
||||||
Assertions.assertEquals("1", customerService.deleteCustomer(c.getId()).getFirstName());
|
Assertions.assertEquals("1", customerService.deleteCustomer(c.getId()).getFirstName());
|
||||||
|
|
||||||
|
productService.deleteAllProducts();
|
||||||
orderService.deleteAllOrders();
|
orderService.deleteAllOrders();
|
||||||
customerService.deleteAllCustomers();
|
customerService.deleteAllCustomers();
|
||||||
storeService.deleteAllStores();
|
storeService.deleteAllStores();
|
||||||
@ -56,6 +63,7 @@ public class JpaTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testProduct(){
|
void testProduct(){
|
||||||
|
productService.deleteAllProducts();
|
||||||
orderService.deleteAllOrders();
|
orderService.deleteAllOrders();
|
||||||
customerService.deleteAllCustomers();
|
customerService.deleteAllCustomers();
|
||||||
storeService.deleteAllStores();
|
storeService.deleteAllStores();
|
||||||
@ -63,13 +71,15 @@ public class JpaTests {
|
|||||||
Store store = storeService.addStore("example");
|
Store store = storeService.addStore("example");
|
||||||
Assertions.assertEquals("example", store.getStoreName());
|
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", p.getName());
|
||||||
|
|
||||||
Assertions.assertEquals("product", storeService.getProduct(p.getId(), store.getId()).getName());
|
Assertions.assertEquals("product", storeService.addProduct(store.getId(), p.getId()).getName());
|
||||||
Assertions.assertEquals("productUpd", storeService.updateProduct(store.getId(), p.getId(), "productUpd").getName());
|
Assertions.assertEquals("product", storeService.getProductFromStore(p.getId(), store.getId()).getName());
|
||||||
Assertions.assertEquals("productUpd", storeService.deleteProduct(store.getId(), p.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();
|
orderService.deleteAllOrders();
|
||||||
customerService.deleteAllCustomers();
|
customerService.deleteAllCustomers();
|
||||||
storeService.deleteAllStores();
|
storeService.deleteAllStores();
|
||||||
@ -77,6 +87,7 @@ public class JpaTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testOrder(){
|
void testOrder(){
|
||||||
|
productService.deleteAllProducts();
|
||||||
orderService.deleteAllOrders();
|
orderService.deleteAllOrders();
|
||||||
customerService.deleteAllCustomers();
|
customerService.deleteAllCustomers();
|
||||||
storeService.deleteAllStores();
|
storeService.deleteAllStores();
|
||||||
@ -84,7 +95,8 @@ public class JpaTests {
|
|||||||
Store store = storeService.addStore("example");
|
Store store = storeService.addStore("example");
|
||||||
Assertions.assertEquals("example", store.getStoreName());
|
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());
|
Assertions.assertEquals("product", p.getName());
|
||||||
|
|
||||||
Customer c = customerService.addCustomer("1", "2", "3");
|
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.updateOrder(order.getId(), 6).getQuantity()));
|
||||||
Assertions.assertEquals("6", Integer.toString(orderService.deleteOrder(order.getId()).getQuantity()));
|
Assertions.assertEquals("6", Integer.toString(orderService.deleteOrder(order.getId()).getQuantity()));
|
||||||
|
|
||||||
|
productService.deleteAllProducts();
|
||||||
orderService.deleteAllOrders();
|
orderService.deleteAllOrders();
|
||||||
customerService.deleteAllCustomers();
|
customerService.deleteAllCustomers();
|
||||||
storeService.deleteAllStores();
|
storeService.deleteAllStores();
|
||||||
|
Loading…
Reference in New Issue
Block a user