add store dimension to inventory

This commit is contained in:
2026-04-06 20:24:23 -06:00
parent 0482af966e
commit a74e2ac0ef
5 changed files with 75 additions and 8 deletions

View File

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

View File

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

View File

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

View File

@@ -16,8 +16,9 @@ public interface InventoryRepository extends JpaRepository<Inventory, Long> {
@Query("SELECT i FROM Inventory i WHERE i.product.prodId = :productId")
Optional<Inventory> 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<Inventory> searchInventory(@Param("q") String query, Pageable pageable);
}

View File

@@ -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<InventoryResponse> 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()