add store dimension to inventory
This commit is contained in:
@@ -12,6 +12,8 @@ public class InventoryRequest {
|
|||||||
@PositiveOrZero(message = "Quantity must be zero or positive")
|
@PositiveOrZero(message = "Quantity must be zero or positive")
|
||||||
private Integer quantity;
|
private Integer quantity;
|
||||||
|
|
||||||
|
private Long storeId;
|
||||||
|
|
||||||
public Long getProdId() {
|
public Long getProdId() {
|
||||||
return prodId;
|
return prodId;
|
||||||
}
|
}
|
||||||
@@ -28,18 +30,27 @@ public class InventoryRequest {
|
|||||||
this.quantity = quantity;
|
this.quantity = quantity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getStoreId() {
|
||||||
|
return storeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStoreId(Long storeId) {
|
||||||
|
this.storeId = storeId;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
InventoryRequest that = (InventoryRequest) o;
|
InventoryRequest that = (InventoryRequest) o;
|
||||||
return Objects.equals(prodId, that.prodId) &&
|
return Objects.equals(prodId, that.prodId) &&
|
||||||
Objects.equals(quantity, that.quantity);
|
Objects.equals(quantity, that.quantity) &&
|
||||||
|
Objects.equals(storeId, that.storeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(prodId, quantity);
|
return Objects.hash(prodId, quantity, storeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -47,6 +58,7 @@ public class InventoryRequest {
|
|||||||
return "InventoryRequest{" +
|
return "InventoryRequest{" +
|
||||||
"prodId=" + prodId +
|
"prodId=" + prodId +
|
||||||
", quantity=" + quantity +
|
", quantity=" + quantity +
|
||||||
|
", storeId=" + storeId +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ public class InventoryResponse {
|
|||||||
private Long prodId;
|
private Long prodId;
|
||||||
private String productName;
|
private String productName;
|
||||||
private String categoryName;
|
private String categoryName;
|
||||||
|
private Long storeId;
|
||||||
|
private String storeName;
|
||||||
private Integer quantity;
|
private Integer quantity;
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
private LocalDateTime updatedAt;
|
private LocalDateTime updatedAt;
|
||||||
@@ -15,11 +17,13 @@ public class InventoryResponse {
|
|||||||
public 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.inventoryId = inventoryId;
|
||||||
this.prodId = prodId;
|
this.prodId = prodId;
|
||||||
this.productName = productName;
|
this.productName = productName;
|
||||||
this.categoryName = categoryName;
|
this.categoryName = categoryName;
|
||||||
|
this.storeId = storeId;
|
||||||
|
this.storeName = storeName;
|
||||||
this.quantity = quantity;
|
this.quantity = quantity;
|
||||||
this.createdAt = createdAt;
|
this.createdAt = createdAt;
|
||||||
this.updatedAt = updatedAt;
|
this.updatedAt = updatedAt;
|
||||||
@@ -57,6 +61,22 @@ public class InventoryResponse {
|
|||||||
this.categoryName = categoryName;
|
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() {
|
public Integer getQuantity() {
|
||||||
return quantity;
|
return quantity;
|
||||||
}
|
}
|
||||||
@@ -86,12 +106,12 @@ public class InventoryResponse {
|
|||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
InventoryResponse that = (InventoryResponse) o;
|
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
|
@Override
|
||||||
public int hashCode() {
|
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
|
@Override
|
||||||
@@ -101,6 +121,8 @@ public class InventoryResponse {
|
|||||||
", prodId=" + prodId +
|
", prodId=" + prodId +
|
||||||
", productName='" + productName + '\'' +
|
", productName='" + productName + '\'' +
|
||||||
", categoryName='" + categoryName + '\'' +
|
", categoryName='" + categoryName + '\'' +
|
||||||
|
", storeId=" + storeId +
|
||||||
|
", storeName='" + storeName + '\'' +
|
||||||
", quantity=" + quantity +
|
", quantity=" + quantity +
|
||||||
", createdAt=" + createdAt +
|
", createdAt=" + createdAt +
|
||||||
", updatedAt=" + updatedAt +
|
", updatedAt=" + updatedAt +
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ public class Inventory {
|
|||||||
@JoinColumn(name = "prodId", nullable = false)
|
@JoinColumn(name = "prodId", nullable = false)
|
||||||
private Product product;
|
private Product product;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "storeId")
|
||||||
|
private StoreLocation store;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private Integer quantity = 0;
|
private Integer quantity = 0;
|
||||||
|
|
||||||
@@ -57,6 +61,14 @@ public class Inventory {
|
|||||||
this.product = product;
|
this.product = product;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StoreLocation getStore() {
|
||||||
|
return store;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStore(StoreLocation store) {
|
||||||
|
this.store = store;
|
||||||
|
}
|
||||||
|
|
||||||
public Integer getQuantity() {
|
public Integer getQuantity() {
|
||||||
return quantity;
|
return quantity;
|
||||||
}
|
}
|
||||||
@@ -99,6 +111,7 @@ public class Inventory {
|
|||||||
return "Inventory{" +
|
return "Inventory{" +
|
||||||
"inventoryId=" + inventoryId +
|
"inventoryId=" + inventoryId +
|
||||||
", product=" + product +
|
", product=" + product +
|
||||||
|
", store=" + store +
|
||||||
", quantity=" + quantity +
|
", quantity=" + quantity +
|
||||||
", createdAt=" + createdAt +
|
", createdAt=" + createdAt +
|
||||||
", updatedAt=" + updatedAt +
|
", updatedAt=" + updatedAt +
|
||||||
|
|||||||
@@ -16,8 +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 " +
|
@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, '%'))")
|
"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);
|
Page<Inventory> searchInventory(@Param("q") String query, Pageable pageable);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,11 @@ import com.petshop.backend.dto.inventory.InventoryRequest;
|
|||||||
import com.petshop.backend.dto.inventory.InventoryResponse;
|
import com.petshop.backend.dto.inventory.InventoryResponse;
|
||||||
import com.petshop.backend.entity.Inventory;
|
import com.petshop.backend.entity.Inventory;
|
||||||
import com.petshop.backend.entity.Product;
|
import com.petshop.backend.entity.Product;
|
||||||
|
import com.petshop.backend.entity.StoreLocation;
|
||||||
import com.petshop.backend.exception.ResourceNotFoundException;
|
import com.petshop.backend.exception.ResourceNotFoundException;
|
||||||
import com.petshop.backend.repository.InventoryRepository;
|
import com.petshop.backend.repository.InventoryRepository;
|
||||||
import com.petshop.backend.repository.ProductRepository;
|
import com.petshop.backend.repository.ProductRepository;
|
||||||
|
import com.petshop.backend.repository.StoreRepository;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -18,10 +20,12 @@ public class InventoryService {
|
|||||||
|
|
||||||
private final InventoryRepository inventoryRepository;
|
private final InventoryRepository inventoryRepository;
|
||||||
private final ProductRepository productRepository;
|
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.inventoryRepository = inventoryRepository;
|
||||||
this.productRepository = productRepository;
|
this.productRepository = productRepository;
|
||||||
|
this.storeRepository = storeRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Page<InventoryResponse> getAllInventory(String query, Pageable pageable) {
|
public Page<InventoryResponse> getAllInventory(String query, Pageable pageable) {
|
||||||
@@ -45,8 +49,14 @@ public class InventoryService {
|
|||||||
Product product = productRepository.findById(request.getProdId())
|
Product product = productRepository.findById(request.getProdId())
|
||||||
.orElseThrow(() -> new ResourceNotFoundException("Product not found with id: " + 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 inventory = new Inventory();
|
||||||
inventory.setProduct(product);
|
inventory.setProduct(product);
|
||||||
|
inventory.setStore(store);
|
||||||
inventory.setQuantity(request.getQuantity());
|
inventory.setQuantity(request.getQuantity());
|
||||||
|
|
||||||
inventory = inventoryRepository.save(inventory);
|
inventory = inventoryRepository.save(inventory);
|
||||||
@@ -61,7 +71,13 @@ public class InventoryService {
|
|||||||
Product product = productRepository.findById(request.getProdId())
|
Product product = productRepository.findById(request.getProdId())
|
||||||
.orElseThrow(() -> new ResourceNotFoundException("Product not found with id: " + 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.setProduct(product);
|
||||||
|
inventory.setStore(store);
|
||||||
inventory.setQuantity(request.getQuantity());
|
inventory.setQuantity(request.getQuantity());
|
||||||
|
|
||||||
inventory = inventoryRepository.save(inventory);
|
inventory = inventoryRepository.save(inventory);
|
||||||
@@ -82,11 +98,14 @@ public class InventoryService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private InventoryResponse mapToResponse(Inventory inventory) {
|
private InventoryResponse mapToResponse(Inventory inventory) {
|
||||||
|
StoreLocation store = inventory.getStore();
|
||||||
return new InventoryResponse(
|
return new InventoryResponse(
|
||||||
inventory.getInventoryId(),
|
inventory.getInventoryId(),
|
||||||
inventory.getProduct().getProdId(),
|
inventory.getProduct().getProdId(),
|
||||||
inventory.getProduct().getProdName(),
|
inventory.getProduct().getProdName(),
|
||||||
inventory.getProduct().getCategory().getCategoryName(),
|
inventory.getProduct().getCategory().getCategoryName(),
|
||||||
|
store != null ? store.getStoreId() : null,
|
||||||
|
store != null ? store.getStoreName() : null,
|
||||||
inventory.getQuantity(),
|
inventory.getQuantity(),
|
||||||
inventory.getCreatedAt(),
|
inventory.getCreatedAt(),
|
||||||
inventory.getUpdatedAt()
|
inventory.getUpdatedAt()
|
||||||
|
|||||||
Reference in New Issue
Block a user