Третья лабораторная работа. Доп. задание

This commit is contained in:
abazov73 2023-04-11 14:42:54 +04:00
parent c6dff060b0
commit 61813642af
9 changed files with 2489 additions and 6 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -42,6 +42,9 @@ public class Ordered {
public void setProduct(Product product) { public void setProduct(Product product) {
this.product = product; this.product = product;
if (!product.getOrders().contains(this)){
product.AddOrdered(this);
}
} }
public int getQuantity() { public int getQuantity() {
@ -54,10 +57,16 @@ public class Ordered {
public void setStore(Store store) { public void setStore(Store store) {
this.store = store; this.store = store;
if (!store.getOrders().contains(this)){
store.AddOrdered(this);
}
} }
public void setCustomer(Customer customer) { public void setCustomer(Customer customer) {
this.customer = customer; this.customer = customer;
if (!customer.getOrders().contains(this)){
customer.AddOrdered(this);
}
} }
@Override @Override

View File

@ -57,6 +57,9 @@ public class Product {
public void setStore(Store store) { public void setStore(Store store) {
this.store = store; this.store = store;
if (!store.getProducts().contains(this)){
store.AddProduct(this);
}
} }
@Override @Override

View File

@ -36,7 +36,7 @@ public class CustomerService {
@Transactional @Transactional
public List<Customer> getAllCustomers(){ public List<Customer> getAllCustomers(){
return em.createQuery("get c from Customer c", Customer.class).getResultList(); return em.createQuery("SELECT c from Customer c", Customer.class).getResultList();
} }
@Transactional @Transactional

View File

@ -8,9 +8,11 @@ import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Service @Service
@ -18,6 +20,12 @@ public class OrderService {
@PersistenceContext @PersistenceContext
private EntityManager em; private EntityManager em;
private ProductService productService;
public OrderService(ProductService productService){
this.productService = productService;
}
@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(quantity); final Ordered order = new Ordered(quantity);
@ -36,10 +44,18 @@ public class OrderService {
} }
return order; return order;
} }
@Transactional
public List<Ordered> getOrdersWithProduct(Long productId, int minQuantity, int maxQuantity){
return em.createQuery("SELECT o FROM Ordered o WHERE o.product.id = ?1 AND o.quantity >= ?2 AND o.quantity <= ?3",Ordered.class)
.setParameter(1, productId)
.setParameter(2, minQuantity)
.setParameter(3, maxQuantity)
.getResultList();
}
@Transactional @Transactional
public List<Ordered> getAllOrders(){ public List<Ordered> getAllOrders(){
return em.createQuery("get p from Ordered p", Ordered.class).getResultList(); return em.createQuery("SELECT o FROM Ordered o", Ordered.class).getResultList();
} }
@Transactional @Transactional
@ -59,7 +75,7 @@ public class OrderService {
} }
@Transactional @Transactional
public void deleteAllOrders(){ public void deleteAllOrders(){
em.createQuery("delete from Ordered"); em.createQuery("delete from Ordered").executeUpdate();
} }
//product section //product section

View File

@ -38,7 +38,7 @@ public class ProductService {
@Transactional @Transactional
public List<Product> getAllProducts(){ public List<Product> getAllProducts(){
return em.createQuery("get p from Product p", Product.class).getResultList(); return em.createQuery("SELECT p FROM Product p", Product.class).getResultList();
} }
@Transactional @Transactional
@ -67,6 +67,6 @@ public class ProductService {
} }
@Transactional @Transactional
public void deleteAllProducts(){ public void deleteAllProducts(){
em.createQuery("delete from Customer"); em.createQuery("delete from Product");
} }
} }

View File

@ -43,7 +43,7 @@ public class StoreService {
@Transactional @Transactional
public List<Store> getAllStores(){ public List<Store> getAllStores(){
return em.createQuery("get s from Store s", Store.class).getResultList(); return em.createQuery("SELECT s FROM Store s", Store.class).getResultList();
} }
@Transactional @Transactional

View File

@ -13,6 +13,9 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest @SpringBootTest
public class JpaTests { public class JpaTests {
@Autowired @Autowired
@ -109,6 +112,44 @@ 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();
customerService.deleteAllCustomers();
storeService.deleteAllStores();
}
@Test
void FilterOrderTest(){
productService.deleteAllProducts();
orderService.deleteAllOrders();
customerService.deleteAllCustomers();
storeService.deleteAllStores();
Store store = storeService.addStore("example");
Assertions.assertEquals("example", store.getStoreName());
Product p1 = productService.addProduct("product");
Product p2 = productService.addProduct("product2");
storeService.addProduct(store.getId(), p1.getId());
Assertions.assertEquals("product", p1.getName());
storeService.addProduct(store.getId(), p2.getId());
Assertions.assertEquals("product2", p2.getName());
Customer c = customerService.addCustomer("1", "2", "3");
Assertions.assertEquals("2", c.getFirstName());
Ordered order1 = orderService.addOrder(store, p1, c, 0);
Ordered order2 = orderService.addOrder(store, p2, c, 6);
Ordered order3 = orderService.addOrder(store, p1, c, 2);
Ordered order4 = orderService.addOrder(store, p2, c, 2);
Ordered order5 = orderService.addOrder(store, p1, c, 3);
List<Ordered> expectedResult = new ArrayList<>();
expectedResult.add(order3);
expectedResult.add(order5);
orderService.getAllOrders();
Assertions.assertEquals(expectedResult, orderService.getOrdersWithProduct(p1.getId(), 1, 5));
productService.deleteAllProducts(); productService.deleteAllProducts();
orderService.deleteAllOrders(); orderService.deleteAllOrders();
customerService.deleteAllCustomers(); customerService.deleteAllCustomers();