diff --git a/backend/lab/src/main/java/com/example/lab/DataBase/Repositories/CountProductRepository.java b/backend/lab/src/main/java/com/example/lab/DataBase/Repositories/CountProductRepository.java index a865c45..b49c973 100644 --- a/backend/lab/src/main/java/com/example/lab/DataBase/Repositories/CountProductRepository.java +++ b/backend/lab/src/main/java/com/example/lab/DataBase/Repositories/CountProductRepository.java @@ -3,5 +3,5 @@ package com.example.lab.DataBase.Repositories; import com.example.lab.DataBase.Models.CountProduct; import org.springframework.data.jpa.repository.JpaRepository; -public interface CountProductRepository extends JpaRepository { +public interface CountProductRepository extends JpaRepository { } \ No newline at end of file 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 db9146b..c93260c 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 @@ -65,7 +65,7 @@ public class CartService { var countProduct = countProductService.getCountProduct(productId, cartId); if(countProduct == null) countProduct = countProductService.addCountProduct(product, cart); countProduct.incrementAmount(); - em.persist(cart); + cartRepository.save(cart); return product; } @@ -76,7 +76,7 @@ public class CartService { var countProduct = countProductService.getCountProduct(productId, cartId); if(countProduct == null) return null; countProductService.deleteCountProduct(productId,cartId); - em.persist(cart); + cartRepository.save(cart); return product; } @@ -86,8 +86,8 @@ public class CartService { Cart cart = getCart(cartId); var countProduct = countProductService.getCountProduct(productId, cartId); if(countProduct == null) return null; - countProduct.decrementAmount(); - em.persist(countProduct); + var resProd = countProductService.decrementProduct(productId, cartId); + cartRepository.save(cart); return product; } } 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 d5d8962..f138c5f 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 @@ -31,21 +31,30 @@ public class CountProductService { @Transactional public CountProduct getCountProduct(long productId, long cartId){ - return countProductRepository.findById(productId, cartId).orElseThrow(() -> new CustomerNotFoundException(id)); + var count = getAllCountProducts(); + var countProduct = count.stream().filter(x -> x.getProduct().getId() == productId + && x.getCart().getId() == cartId ).findFirst(); + if(countProduct.isEmpty()) return null; + else return countProduct.get(); + } + + @Transactional + public CountProduct getCountProductGeneral(long id){ + return countProductRepository.findById(id).orElseThrow(() -> new CustomerNotFoundException(id)); } @Transactional public CountProduct incrementProduct (long productId, long cartId){ CountProduct countProduct = getCountProduct(productId, cartId); countProduct.incrementAmount(); - return em.merge(countProduct); + return countProductRepository.save(countProduct); } @Transactional public CountProduct decrementProduct (long productId, long cartId){ CountProduct countProduct = getCountProduct(productId, cartId); countProduct.decrementAmount(); - return em.merge(countProduct); + return countProductRepository.save(countProduct); } @Transactional diff --git a/backend/lab/src/main/java/com/example/lab/DataBase/Services/ProductService.java b/backend/lab/src/main/java/com/example/lab/DataBase/Services/ProductService.java index c1bc43a..ec0a66e 100644 --- a/backend/lab/src/main/java/com/example/lab/DataBase/Services/ProductService.java +++ b/backend/lab/src/main/java/com/example/lab/DataBase/Services/ProductService.java @@ -1,6 +1,7 @@ package com.example.lab.DataBase.Services; import com.example.lab.DataBase.Exceptions.ProductNotFoundException; +import com.example.lab.DataBase.Models.Customer; import com.example.lab.DataBase.Models.ProductCategory; import com.example.lab.DataBase.Repositories.ProductRepository; import com.example.lab.DataBase.util.Validation.ValidatorUtil; @@ -36,35 +37,26 @@ public class ProductService { @Transactional public List getAllProducts(){ - return em.createQuery("from Product", Product.class).getResultList(); + return productRepository.findAll(); } @Transactional public Product updateProduct(Long id, String name, float price){ - if (!StringUtils.hasText(name)){ - throw new IllegalArgumentException("Product name cant be null"); - } - else if(price <= 0){ - throw new IllegalArgumentException("Product price cant be under 0"); - } final Product product = getProduct(id); product.setName(name); product.setPrice(price); - return em.merge(product); + validatorUtil.validate(product); + return productRepository.save(product); } @Transactional public Product deleteProduct(Long id){ final Product product = getProduct(id); - em.remove(product); + productRepository.delete(product); return product; } @Transactional public void deleteAllProducts(){ - for (var product: - getAllProducts()) { - product.deleteThis(); - em.remove(product); - } + productRepository.deleteAll(); } } \ No newline at end of file