From 6682cdc0472d39b87cc6ffd7e34c628d238ab19f Mon Sep 17 00:00:00 2001 From: Harkamal Randhawa Date: Mon, 6 Apr 2026 20:24:23 -0600 Subject: [PATCH] add store dimension to inventory --- .../dto/inventory/InventoryRequest.java | 16 +++++++++-- .../dto/inventory/InventoryResponse.java | 28 +++++++++++++++++-- .../com/petshop/backend/entity/Inventory.java | 13 +++++++++ .../repository/InventoryRepository.java | 5 ++-- .../backend/service/InventoryService.java | 21 +++++++++++++- 5 files changed, 75 insertions(+), 8 deletions(-) diff --git a/backend/src/main/java/com/petshop/backend/dto/inventory/InventoryRequest.java b/backend/src/main/java/com/petshop/backend/dto/inventory/InventoryRequest.java index 2dd953c6..7ad02d3a 100644 --- a/backend/src/main/java/com/petshop/backend/dto/inventory/InventoryRequest.java +++ b/backend/src/main/java/com/petshop/backend/dto/inventory/InventoryRequest.java @@ -12,6 +12,8 @@ public class InventoryRequest { @PositiveOrZero(message = "Quantity must be zero or positive") private Integer quantity; + private Long storeId; + public Long getProdId() { return prodId; } @@ -28,18 +30,27 @@ public class InventoryRequest { this.quantity = quantity; } + public Long getStoreId() { + return storeId; + } + + public void setStoreId(Long storeId) { + this.storeId = storeId; + } + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; InventoryRequest that = (InventoryRequest) o; return Objects.equals(prodId, that.prodId) && - Objects.equals(quantity, that.quantity); + Objects.equals(quantity, that.quantity) && + Objects.equals(storeId, that.storeId); } @Override public int hashCode() { - return Objects.hash(prodId, quantity); + return Objects.hash(prodId, quantity, storeId); } @Override @@ -47,6 +58,7 @@ public class InventoryRequest { return "InventoryRequest{" + "prodId=" + prodId + ", quantity=" + quantity + + ", storeId=" + storeId + '}'; } } diff --git a/backend/src/main/java/com/petshop/backend/dto/inventory/InventoryResponse.java b/backend/src/main/java/com/petshop/backend/dto/inventory/InventoryResponse.java index 710dcbf4..5879a554 100644 --- a/backend/src/main/java/com/petshop/backend/dto/inventory/InventoryResponse.java +++ b/backend/src/main/java/com/petshop/backend/dto/inventory/InventoryResponse.java @@ -8,6 +8,8 @@ public class InventoryResponse { private Long prodId; private String productName; private String categoryName; + private Long storeId; + private String storeName; private Integer quantity; private LocalDateTime createdAt; private LocalDateTime updatedAt; @@ -15,11 +17,13 @@ public class InventoryResponse { public InventoryResponse() { } - public InventoryResponse(Long inventoryId, Long prodId, String productName, String categoryName, Integer quantity, LocalDateTime createdAt, LocalDateTime updatedAt) { + public InventoryResponse(Long inventoryId, Long prodId, String productName, String categoryName, Long storeId, String storeName, Integer quantity, LocalDateTime createdAt, LocalDateTime updatedAt) { this.inventoryId = inventoryId; this.prodId = prodId; this.productName = productName; this.categoryName = categoryName; + this.storeId = storeId; + this.storeName = storeName; this.quantity = quantity; this.createdAt = createdAt; this.updatedAt = updatedAt; @@ -57,6 +61,22 @@ public class InventoryResponse { this.categoryName = categoryName; } + public Long getStoreId() { + return storeId; + } + + public void setStoreId(Long storeId) { + this.storeId = storeId; + } + + public String getStoreName() { + return storeName; + } + + public void setStoreName(String storeName) { + this.storeName = storeName; + } + public Integer getQuantity() { return quantity; } @@ -86,12 +106,12 @@ public class InventoryResponse { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; InventoryResponse that = (InventoryResponse) o; - return Objects.equals(inventoryId, that.inventoryId) && Objects.equals(prodId, that.prodId) && Objects.equals(productName, that.productName) && Objects.equals(categoryName, that.categoryName) && Objects.equals(quantity, that.quantity) && Objects.equals(createdAt, that.createdAt) && Objects.equals(updatedAt, that.updatedAt); + return Objects.equals(inventoryId, that.inventoryId) && Objects.equals(prodId, that.prodId) && Objects.equals(productName, that.productName) && Objects.equals(categoryName, that.categoryName) && Objects.equals(storeId, that.storeId) && Objects.equals(storeName, that.storeName) && Objects.equals(quantity, that.quantity) && Objects.equals(createdAt, that.createdAt) && Objects.equals(updatedAt, that.updatedAt); } @Override public int hashCode() { - return Objects.hash(inventoryId, prodId, productName, categoryName, quantity, createdAt, updatedAt); + return Objects.hash(inventoryId, prodId, productName, categoryName, storeId, storeName, quantity, createdAt, updatedAt); } @Override @@ -101,6 +121,8 @@ public class InventoryResponse { ", prodId=" + prodId + ", productName='" + productName + '\'' + ", categoryName='" + categoryName + '\'' + + ", storeId=" + storeId + + ", storeName='" + storeName + '\'' + ", quantity=" + quantity + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + diff --git a/backend/src/main/java/com/petshop/backend/entity/Inventory.java b/backend/src/main/java/com/petshop/backend/entity/Inventory.java index 07b93501..ac859c64 100644 --- a/backend/src/main/java/com/petshop/backend/entity/Inventory.java +++ b/backend/src/main/java/com/petshop/backend/entity/Inventory.java @@ -19,6 +19,10 @@ public class Inventory { @JoinColumn(name = "prodId", nullable = false) private Product product; + @ManyToOne + @JoinColumn(name = "storeId") + private StoreLocation store; + @Column(nullable = false) private Integer quantity = 0; @@ -57,6 +61,14 @@ public class Inventory { this.product = product; } + public StoreLocation getStore() { + return store; + } + + public void setStore(StoreLocation store) { + this.store = store; + } + public Integer getQuantity() { return quantity; } @@ -99,6 +111,7 @@ public class Inventory { return "Inventory{" + "inventoryId=" + inventoryId + ", product=" + product + + ", store=" + store + ", quantity=" + quantity + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + diff --git a/backend/src/main/java/com/petshop/backend/repository/InventoryRepository.java b/backend/src/main/java/com/petshop/backend/repository/InventoryRepository.java index 0e9d358c..69ff16ed 100644 --- a/backend/src/main/java/com/petshop/backend/repository/InventoryRepository.java +++ b/backend/src/main/java/com/petshop/backend/repository/InventoryRepository.java @@ -16,8 +16,9 @@ public interface InventoryRepository extends JpaRepository { @Query("SELECT i FROM Inventory i WHERE i.product.prodId = :productId") Optional findByProductId(@Param("productId") Long productId); - @Query("SELECT i FROM Inventory i 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.category.categoryName) LIKE LOWER(CONCAT('%', :q, '%'))") + "LOWER(i.product.category.categoryName) LIKE LOWER(CONCAT('%', :q, '%')) OR " + + "LOWER(s.storeName) LIKE LOWER(CONCAT('%', :q, '%'))") Page searchInventory(@Param("q") String query, Pageable pageable); } diff --git a/backend/src/main/java/com/petshop/backend/service/InventoryService.java b/backend/src/main/java/com/petshop/backend/service/InventoryService.java index ee63aea7..499e8dd5 100644 --- a/backend/src/main/java/com/petshop/backend/service/InventoryService.java +++ b/backend/src/main/java/com/petshop/backend/service/InventoryService.java @@ -5,9 +5,11 @@ import com.petshop.backend.dto.inventory.InventoryRequest; import com.petshop.backend.dto.inventory.InventoryResponse; import com.petshop.backend.entity.Inventory; import com.petshop.backend.entity.Product; +import com.petshop.backend.entity.StoreLocation; import com.petshop.backend.exception.ResourceNotFoundException; import com.petshop.backend.repository.InventoryRepository; import com.petshop.backend.repository.ProductRepository; +import com.petshop.backend.repository.StoreRepository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -18,10 +20,12 @@ public class InventoryService { private final InventoryRepository inventoryRepository; private final ProductRepository productRepository; + private final StoreRepository storeRepository; - public InventoryService(InventoryRepository inventoryRepository, ProductRepository productRepository) { + public InventoryService(InventoryRepository inventoryRepository, ProductRepository productRepository, StoreRepository storeRepository) { this.inventoryRepository = inventoryRepository; this.productRepository = productRepository; + this.storeRepository = storeRepository; } public Page getAllInventory(String query, Pageable pageable) { @@ -45,8 +49,14 @@ public class InventoryService { Product product = productRepository.findById(request.getProdId()) .orElseThrow(() -> new ResourceNotFoundException("Product not found with id: " + request.getProdId())); + StoreLocation store = request.getStoreId() != null + ? storeRepository.findById(request.getStoreId()) + .orElseThrow(() -> new ResourceNotFoundException("Store not found with id: " + request.getStoreId())) + : null; + Inventory inventory = new Inventory(); inventory.setProduct(product); + inventory.setStore(store); inventory.setQuantity(request.getQuantity()); inventory = inventoryRepository.save(inventory); @@ -61,7 +71,13 @@ public class InventoryService { Product product = productRepository.findById(request.getProdId()) .orElseThrow(() -> new ResourceNotFoundException("Product not found with id: " + request.getProdId())); + StoreLocation store = request.getStoreId() != null + ? storeRepository.findById(request.getStoreId()) + .orElseThrow(() -> new ResourceNotFoundException("Store not found with id: " + request.getStoreId())) + : null; + inventory.setProduct(product); + inventory.setStore(store); inventory.setQuantity(request.getQuantity()); inventory = inventoryRepository.save(inventory); @@ -82,11 +98,14 @@ public class InventoryService { } private InventoryResponse mapToResponse(Inventory inventory) { + StoreLocation store = inventory.getStore(); return new InventoryResponse( inventory.getInventoryId(), inventory.getProduct().getProdId(), inventory.getProduct().getProdName(), inventory.getProduct().getCategory().getCategoryName(), + store != null ? store.getStoreId() : null, + store != null ? store.getStoreName() : null, inventory.getQuantity(), inventory.getCreatedAt(), inventory.getUpdatedAt()