Четвертая лабораторная работа. Back-end всё.

This commit is contained in:
ksenianeva 2023-05-16 12:52:26 +04:00
parent 807957816b
commit 93166df773
4 changed files with 23 additions and 22 deletions

View File

@ -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<CountProduct, Integer> {
public interface CountProductRepository extends JpaRepository<CountProduct, Long> {
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -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<Product> 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();
}
}