Третья лабораторная работа. Дополнительное задание + сопутствующие фиксы.
This commit is contained in:
parent
fd9b52de8a
commit
0361b622e9
Binary file not shown.
@ -2,6 +2,7 @@ package com.example.lab.DataBase.Services;
|
|||||||
|
|
||||||
import com.example.lab.DataBase.Models.Customer;
|
import com.example.lab.DataBase.Models.Customer;
|
||||||
import com.example.lab.DataBase.Models.Product;
|
import com.example.lab.DataBase.Models.Product;
|
||||||
|
import com.example.lab.DataBase.Models.CountProduct;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import jakarta.transaction.Transactional;
|
import jakarta.transaction.Transactional;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -59,14 +60,14 @@ public class CartService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Product addProduct(Long cartId, Long productId){
|
public CountProduct addProduct(Long cartId, Long productId){
|
||||||
Product product = productService.getProduct(productId);
|
Product product = productService.getProduct(productId);
|
||||||
Cart cart = getCart(cartId);
|
Cart cart = getCart(cartId);
|
||||||
var countProduct = countProductService.getCountProduct(productId, cartId);
|
var countProduct = countProductService.getCountProduct(productId, cartId);
|
||||||
if(countProduct == null) countProduct = countProductService.addCountProduct(product, cart);
|
if(countProduct == null) countProductService.addCountProduct(product, cart);
|
||||||
countProduct.incrementAmount();
|
var resProd = countProductService.incrementProduct(productId, cartId);
|
||||||
em.persist(cart);
|
em.persist(cart);
|
||||||
return product;
|
return resProd;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@ -65,4 +65,14 @@ public class CountProductService {
|
|||||||
public List<CountProduct> getAllCountProducts(){
|
public List<CountProduct> getAllCountProducts(){
|
||||||
return em.createQuery("from CountProduct", CountProduct.class).getResultList();
|
return em.createQuery("from CountProduct", CountProduct.class).getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<CountProduct> 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();
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,4 +1,6 @@
|
|||||||
package com.example.lab.DataBase.Services;
|
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.persistence.*;
|
||||||
import jakarta.transaction.Transactional;
|
import jakarta.transaction.Transactional;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -10,6 +10,7 @@ import org.junit.jupiter.api.Assertions;
|
|||||||
import org.junit.jupiter.api.Test;
|
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.*;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class DBTests {
|
public class DBTests {
|
||||||
@ -107,6 +108,31 @@ public class DBTests {
|
|||||||
cleanAll();
|
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<CountProduct> 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(){
|
public void cleanAll(){
|
||||||
countProductService.deleteAll();
|
countProductService.deleteAll();
|
||||||
productService.deleteAllProducts();
|
productService.deleteAllProducts();
|
||||||
|
Loading…
Reference in New Issue
Block a user