Четвертая лабораторная работа. Back-end всё.
This commit is contained in:
parent
807957816b
commit
93166df773
@ -3,5 +3,5 @@ package com.example.lab.DataBase.Repositories;
|
|||||||
import com.example.lab.DataBase.Models.CountProduct;
|
import com.example.lab.DataBase.Models.CountProduct;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
public interface CountProductRepository extends JpaRepository<CountProduct, Integer> {
|
public interface CountProductRepository extends JpaRepository<CountProduct, Long> {
|
||||||
}
|
}
|
@ -65,7 +65,7 @@ public class CartService {
|
|||||||
var countProduct = countProductService.getCountProduct(productId, cartId);
|
var countProduct = countProductService.getCountProduct(productId, cartId);
|
||||||
if(countProduct == null) countProduct = countProductService.addCountProduct(product, cart);
|
if(countProduct == null) countProduct = countProductService.addCountProduct(product, cart);
|
||||||
countProduct.incrementAmount();
|
countProduct.incrementAmount();
|
||||||
em.persist(cart);
|
cartRepository.save(cart);
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ public class CartService {
|
|||||||
var countProduct = countProductService.getCountProduct(productId, cartId);
|
var countProduct = countProductService.getCountProduct(productId, cartId);
|
||||||
if(countProduct == null) return null;
|
if(countProduct == null) return null;
|
||||||
countProductService.deleteCountProduct(productId,cartId);
|
countProductService.deleteCountProduct(productId,cartId);
|
||||||
em.persist(cart);
|
cartRepository.save(cart);
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,8 +86,8 @@ public class CartService {
|
|||||||
Cart cart = getCart(cartId);
|
Cart cart = getCart(cartId);
|
||||||
var countProduct = countProductService.getCountProduct(productId, cartId);
|
var countProduct = countProductService.getCountProduct(productId, cartId);
|
||||||
if(countProduct == null) return null;
|
if(countProduct == null) return null;
|
||||||
countProduct.decrementAmount();
|
var resProd = countProductService.decrementProduct(productId, cartId);
|
||||||
em.persist(countProduct);
|
cartRepository.save(cart);
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,21 +31,30 @@ public class CountProductService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public CountProduct getCountProduct(long productId, long cartId){
|
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
|
@Transactional
|
||||||
public CountProduct incrementProduct (long productId, long cartId){
|
public CountProduct incrementProduct (long productId, long cartId){
|
||||||
CountProduct countProduct = getCountProduct(productId, cartId);
|
CountProduct countProduct = getCountProduct(productId, cartId);
|
||||||
countProduct.incrementAmount();
|
countProduct.incrementAmount();
|
||||||
return em.merge(countProduct);
|
return countProductRepository.save(countProduct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public CountProduct decrementProduct (long productId, long cartId){
|
public CountProduct decrementProduct (long productId, long cartId){
|
||||||
CountProduct countProduct = getCountProduct(productId, cartId);
|
CountProduct countProduct = getCountProduct(productId, cartId);
|
||||||
countProduct.decrementAmount();
|
countProduct.decrementAmount();
|
||||||
return em.merge(countProduct);
|
return countProductRepository.save(countProduct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.example.lab.DataBase.Services;
|
package com.example.lab.DataBase.Services;
|
||||||
|
|
||||||
import com.example.lab.DataBase.Exceptions.ProductNotFoundException;
|
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.Models.ProductCategory;
|
||||||
import com.example.lab.DataBase.Repositories.ProductRepository;
|
import com.example.lab.DataBase.Repositories.ProductRepository;
|
||||||
import com.example.lab.DataBase.util.Validation.ValidatorUtil;
|
import com.example.lab.DataBase.util.Validation.ValidatorUtil;
|
||||||
@ -36,35 +37,26 @@ public class ProductService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public List<Product> getAllProducts(){
|
public List<Product> getAllProducts(){
|
||||||
return em.createQuery("from Product", Product.class).getResultList();
|
return productRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Product updateProduct(Long id, String name, float price){
|
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);
|
final Product product = getProduct(id);
|
||||||
product.setName(name);
|
product.setName(name);
|
||||||
product.setPrice(price);
|
product.setPrice(price);
|
||||||
return em.merge(product);
|
validatorUtil.validate(product);
|
||||||
|
return productRepository.save(product);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Product deleteProduct(Long id){
|
public Product deleteProduct(Long id){
|
||||||
final Product product = getProduct(id);
|
final Product product = getProduct(id);
|
||||||
em.remove(product);
|
productRepository.delete(product);
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAllProducts(){
|
public void deleteAllProducts(){
|
||||||
for (var product:
|
productRepository.deleteAll();
|
||||||
getAllProducts()) {
|
|
||||||
product.deleteThis();
|
|
||||||
em.remove(product);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user