Третья лабораторная работа. Доп. задание
This commit is contained in:
parent
c6dff060b0
commit
61813642af
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@ -42,6 +42,9 @@ public class Ordered {
|
||||
|
||||
public void setProduct(Product product) {
|
||||
this.product = product;
|
||||
if (!product.getOrders().contains(this)){
|
||||
product.AddOrdered(this);
|
||||
}
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
@ -54,10 +57,16 @@ public class Ordered {
|
||||
|
||||
public void setStore(Store store) {
|
||||
this.store = store;
|
||||
if (!store.getOrders().contains(this)){
|
||||
store.AddOrdered(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void setCustomer(Customer customer) {
|
||||
this.customer = customer;
|
||||
if (!customer.getOrders().contains(this)){
|
||||
customer.AddOrdered(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -57,6 +57,9 @@ public class Product {
|
||||
|
||||
public void setStore(Store store) {
|
||||
this.store = store;
|
||||
if (!store.getProducts().contains(this)){
|
||||
store.AddProduct(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -36,7 +36,7 @@ public class CustomerService {
|
||||
|
||||
@Transactional
|
||||
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
|
||||
|
@ -8,9 +8,11 @@ import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.aspectj.weaver.ast.Or;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@ -18,6 +20,12 @@ public class OrderService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
private ProductService productService;
|
||||
|
||||
public OrderService(ProductService productService){
|
||||
this.productService = productService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Ordered addOrder(Store store, Product product, Customer customer, int quantity){
|
||||
final Ordered order = new Ordered(quantity);
|
||||
@ -36,10 +44,18 @@ public class OrderService {
|
||||
}
|
||||
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
|
||||
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
|
||||
@ -59,7 +75,7 @@ public class OrderService {
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllOrders(){
|
||||
em.createQuery("delete from Ordered");
|
||||
em.createQuery("delete from Ordered").executeUpdate();
|
||||
}
|
||||
|
||||
//product section
|
||||
|
@ -38,7 +38,7 @@ public class ProductService {
|
||||
|
||||
@Transactional
|
||||
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
|
||||
@ -67,6 +67,6 @@ public class ProductService {
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllProducts(){
|
||||
em.createQuery("delete from Customer");
|
||||
em.createQuery("delete from Product");
|
||||
}
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ public class StoreService {
|
||||
|
||||
@Transactional
|
||||
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
|
||||
|
@ -13,6 +13,9 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class JpaTests {
|
||||
@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.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();
|
||||
orderService.deleteAllOrders();
|
||||
customerService.deleteAllCustomers();
|
||||
|
Loading…
Reference in New Issue
Block a user