scope inventory lookup by store on sale
This commit is contained in:
@@ -16,6 +16,9 @@ public interface InventoryRepository extends JpaRepository<Inventory, Long> {
|
|||||||
@Query("SELECT i FROM Inventory i WHERE i.product.prodId = :productId")
|
@Query("SELECT i FROM Inventory i WHERE i.product.prodId = :productId")
|
||||||
Optional<Inventory> findByProductId(@Param("productId") Long productId);
|
Optional<Inventory> findByProductId(@Param("productId") Long productId);
|
||||||
|
|
||||||
|
@Query("SELECT i FROM Inventory i WHERE i.product.prodId = :productId AND i.store.storeId = :storeId")
|
||||||
|
Optional<Inventory> findByProductIdAndStoreId(@Param("productId") Long productId, @Param("storeId") Long storeId);
|
||||||
|
|
||||||
@Query("SELECT i FROM Inventory i LEFT JOIN i.store s WHERE " +
|
@Query("SELECT i FROM Inventory i LEFT JOIN i.store s WHERE " +
|
||||||
"LOWER(i.product.prodName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
|
"LOWER(i.product.prodName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
|
||||||
"LOWER(i.product.category.categoryName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
|
"LOWER(i.product.category.categoryName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
|
||||||
|
|||||||
@@ -134,8 +134,8 @@ public class SaleService {
|
|||||||
" for product: " + product.getProdName());
|
" for product: " + product.getProdName());
|
||||||
}
|
}
|
||||||
|
|
||||||
Inventory inventory = inventoryRepository.findByProductId(itemRequest.getProdId())
|
Inventory inventory = inventoryRepository.findByProductIdAndStoreId(itemRequest.getProdId(), store.getStoreId())
|
||||||
.orElseThrow(() -> new ResourceNotFoundException("Inventory not found for product " + itemRequest.getProdId()));
|
.orElseThrow(() -> new ResourceNotFoundException("Inventory not found for product " + itemRequest.getProdId() + " at store " + store.getStoreId()));
|
||||||
|
|
||||||
inventory.setQuantity(inventory.getQuantity() + itemRequest.getQuantity());
|
inventory.setQuantity(inventory.getQuantity() + itemRequest.getQuantity());
|
||||||
inventoryRepository.save(inventory);
|
inventoryRepository.save(inventory);
|
||||||
@@ -158,8 +158,8 @@ public class SaleService {
|
|||||||
Product product = productRepository.findById(itemRequest.getProdId())
|
Product product = productRepository.findById(itemRequest.getProdId())
|
||||||
.orElseThrow(() -> new ResourceNotFoundException("Product not found with id: " + itemRequest.getProdId()));
|
.orElseThrow(() -> new ResourceNotFoundException("Product not found with id: " + itemRequest.getProdId()));
|
||||||
|
|
||||||
Inventory inventory = inventoryRepository.findByProductId(itemRequest.getProdId())
|
Inventory inventory = inventoryRepository.findByProductIdAndStoreId(itemRequest.getProdId(), store.getStoreId())
|
||||||
.orElseThrow(() -> new ResourceNotFoundException("Inventory not found for product " + itemRequest.getProdId()));
|
.orElseThrow(() -> new ResourceNotFoundException("Inventory not found for product " + itemRequest.getProdId() + " at store " + store.getStoreId()));
|
||||||
|
|
||||||
if (inventory.getQuantity() < itemRequest.getQuantity()) {
|
if (inventory.getQuantity() < itemRequest.getQuantity()) {
|
||||||
throw new BusinessException("Insufficient stock for product: " + product.getProdName() +
|
throw new BusinessException("Insufficient stock for product: " + product.getProdName() +
|
||||||
|
|||||||
Reference in New Issue
Block a user