diff --git a/backend/lab/data.mv.db b/backend/lab/data.mv.db index cdafddd..93cbf33 100644 Binary files a/backend/lab/data.mv.db and b/backend/lab/data.mv.db differ diff --git a/backend/lab/src/main/java/com/example/lab/DataBase/Services/CartService.java b/backend/lab/src/main/java/com/example/lab/DataBase/Services/CartService.java index 1fbd0ac..8137d7b 100644 --- a/backend/lab/src/main/java/com/example/lab/DataBase/Services/CartService.java +++ b/backend/lab/src/main/java/com/example/lab/DataBase/Services/CartService.java @@ -2,6 +2,7 @@ package com.example.lab.DataBase.Services; import com.example.lab.DataBase.Models.Customer; import com.example.lab.DataBase.Models.Product; +import com.example.lab.DataBase.Models.CountProduct; import jakarta.persistence.*; import jakarta.transaction.Transactional; import org.springframework.stereotype.Service; @@ -59,14 +60,14 @@ public class CartService { } @Transactional - public Product addProduct(Long cartId, Long productId){ + public CountProduct addProduct(Long cartId, Long productId){ Product product = productService.getProduct(productId); Cart cart = getCart(cartId); var countProduct = countProductService.getCountProduct(productId, cartId); - if(countProduct == null) countProduct = countProductService.addCountProduct(product, cart); - countProduct.incrementAmount(); + if(countProduct == null) countProductService.addCountProduct(product, cart); + var resProd = countProductService.incrementProduct(productId, cartId); em.persist(cart); - return product; + return resProd; } @Transactional diff --git a/backend/lab/src/main/java/com/example/lab/DataBase/Services/CountProductService.java b/backend/lab/src/main/java/com/example/lab/DataBase/Services/CountProductService.java index 80ccf8b..804f9b8 100644 --- a/backend/lab/src/main/java/com/example/lab/DataBase/Services/CountProductService.java +++ b/backend/lab/src/main/java/com/example/lab/DataBase/Services/CountProductService.java @@ -65,4 +65,14 @@ public class CountProductService { public List getAllCountProducts(){ return em.createQuery("from CountProduct", CountProduct.class).getResultList(); } + + @Transactional + public List getFilteredCountProduct(Long customerId, int amount, int minSum){ + return em.createQuery("SELECT c FROM CountProduct c" + + " WHERE c.cart.customer.id = ?1 AND c.amount = ?2 AND c.product.price > ?3",CountProduct.class) + .setParameter(1, customerId) + .setParameter(2, amount) + .setParameter(3, minSum) + .getResultList(); + } } \ No newline at end of file diff --git a/backend/lab/src/main/java/com/example/lab/DataBase/Services/CustomerService.java b/backend/lab/src/main/java/com/example/lab/DataBase/Services/CustomerService.java index fd4e52c..e13c9d0 100644 --- a/backend/lab/src/main/java/com/example/lab/DataBase/Services/CustomerService.java +++ b/backend/lab/src/main/java/com/example/lab/DataBase/Services/CustomerService.java @@ -1,4 +1,6 @@ package com.example.lab.DataBase.Services; +import com.example.lab.DataBase.Models.Cart; +import com.example.lab.DataBase.Models.CountProduct; import jakarta.persistence.*; import jakarta.transaction.Transactional; import org.springframework.stereotype.Service; diff --git a/backend/lab/src/test/java/com/example/lab/DBTests.java b/backend/lab/src/test/java/com/example/lab/DBTests.java index af2b6e4..94e3f4b 100644 --- a/backend/lab/src/test/java/com/example/lab/DBTests.java +++ b/backend/lab/src/test/java/com/example/lab/DBTests.java @@ -10,6 +10,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.util.*; @SpringBootTest public class DBTests { @@ -107,6 +108,31 @@ public class DBTests { cleanAll(); } + @Test + void testCountProductService(){ + cleanAll(); + ProductCategory productCategory = productCategoryService + .addProductCategory("exampleCategory"); + Product product1 = productService + .addProduct("exampleProduct1", (float)50, productCategory); + Product product2 = productService + .addProduct("exampleProduct2", (float)500, productCategory); + Product product3 = productService + .addProduct("exampleProduct2", (float)200, productCategory); + Customer customer = customerService.addCustomer("Ivan", + "Ivanov", "cityExample"); + Cart cart = cartService.addCart(customer); + CountProduct countProduct = cartService.addProduct(cart.getId(), product1.getId()); + CountProduct countProduct2 = cartService.addProduct(cart.getId(), product2.getId()); + CountProduct countProduct3 = cartService.addProduct(cart.getId(), product3.getId()); + List expected = new ArrayList<>(); + countProduct2 = countProductService.incrementProduct(countProduct2.getProduct().getId(), countProduct2.getCart().getId()); + countProduct2 = countProductService.incrementProduct(countProduct2.getProduct().getId(), countProduct2.getCart().getId()); + expected.add(countProduct2); + Assertions.assertEquals(expected, countProductService.getFilteredCountProduct(customer.getId(), 3,100)); + cleanAll(); + } + public void cleanAll(){ countProductService.deleteAll(); productService.deleteAllProducts();