updated inventory backend to have filter by store and added more search features to andriod

This commit is contained in:
Alex
2026-04-07 05:09:48 -06:00
parent 1d00a3b55c
commit 6c832e01f3
15 changed files with 342 additions and 227 deletions

View File

@@ -26,8 +26,9 @@ public class InventoryController {
@GetMapping
public ResponseEntity<Page<InventoryResponse>> getAllInventory(
@RequestParam(required = false) String q,
@RequestParam(required = false) Long storeId,
Pageable pageable) {
return ResponseEntity.ok(inventoryService.getAllInventory(q, pageable));
return ResponseEntity.ok(inventoryService.getAllInventory(q, storeId, pageable));
}
@GetMapping("/{id}")

View File

@@ -20,8 +20,10 @@ public interface InventoryRepository extends JpaRepository<Inventory, Long> {
Optional<Inventory> findByProductIdAndStoreId(@Param("productId") Long productId, @Param("storeId") Long storeId);
@Query("SELECT i FROM Inventory i LEFT JOIN i.store s WHERE " +
"(:q IS NULL OR (" +
"LOWER(i.product.prodName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
"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);
"LOWER(s.storeName) LIKE LOWER(CONCAT('%', :q, '%')))) AND " +
"(:storeId IS NULL OR i.store.storeId = :storeId)")
Page<Inventory> searchInventory(@Param("q") String query, @Param("storeId") Long storeId, Pageable pageable);
}

View File

@@ -28,13 +28,9 @@ public class InventoryService {
this.storeRepository = storeRepository;
}
public Page<InventoryResponse> getAllInventory(String query, Pageable pageable) {
Page<Inventory> inventory;
if (query != null && !query.trim().isEmpty()) {
inventory = inventoryRepository.searchInventory(query, pageable);
} else {
inventory = inventoryRepository.findAll(pageable);
}
public Page<InventoryResponse> getAllInventory(String query, Long storeId, Pageable pageable) {
String normalizedQuery = normalizeFilter(query);
Page<Inventory> inventory = inventoryRepository.searchInventory(normalizedQuery, storeId, pageable);
return inventory.map(this::mapToResponse);
}
@@ -97,6 +93,14 @@ public class InventoryService {
inventoryRepository.deleteAllById(request.getIds());
}
private String normalizeFilter(String value) {
if (value == null) {
return null;
}
String trimmed = value.trim();
return trimmed.isEmpty() ? null : trimmed;
}
private InventoryResponse mapToResponse(Inventory inventory) {
StoreLocation store = inventory.getStore();
return new InventoryResponse(