changed backend so can sortBy productName and added search to productSupplier

This commit is contained in:
Alex
2026-04-07 05:48:24 -06:00
parent cbd038d8fb
commit 5c9d04fc88
9 changed files with 270 additions and 66 deletions

View File

@@ -26,8 +26,10 @@ public class ProductSupplierController {
@GetMapping
public ResponseEntity<Page<ProductSupplierResponse>> getAllProductSuppliers(
@RequestParam(required = false) String q,
@RequestParam(required = false) Long productId,
@RequestParam(required = false) Long supplierId,
Pageable pageable) {
return ResponseEntity.ok(productSupplierService.getAllProductSuppliers(q, pageable));
return ResponseEntity.ok(productSupplierService.getAllProductSuppliers(q, productId, supplierId, pageable));
}
@GetMapping("/{productId}/{supplierId}")

View File

@@ -12,7 +12,13 @@ import org.springframework.stereotype.Repository;
public interface ProductSupplierRepository extends JpaRepository<ProductSupplier, ProductSupplier.ProductSupplierId> {
@Query("SELECT ps FROM ProductSupplier ps WHERE " +
"LOWER(ps.product.prodName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
"LOWER(ps.supplier.supCompany) LIKE LOWER(CONCAT('%', :q, '%'))")
Page<ProductSupplier> searchProductSuppliers(@Param("q") String query, Pageable pageable);
"(:q IS NULL OR (LOWER(ps.product.prodName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
"LOWER(ps.supplier.supCompany) LIKE LOWER(CONCAT('%', :q, '%')))) AND " +
"(:productId IS NULL OR ps.product.prodId = :productId) AND " +
"(:supplierId IS NULL OR ps.supplier.supId = :supplierId)")
Page<ProductSupplier> searchProductSuppliers(
@Param("q") String query,
@Param("productId") Long productId,
@Param("supplierId") Long supplierId,
Pageable pageable);
}

View File

@@ -11,10 +11,15 @@ import com.petshop.backend.repository.ProductRepository;
import com.petshop.backend.repository.ProductSupplierRepository;
import com.petshop.backend.repository.SupplierRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
@Service
public class ProductSupplierService {
@@ -28,13 +33,10 @@ public class ProductSupplierService {
this.supplierRepository = supplierRepository;
}
public Page<ProductSupplierResponse> getAllProductSuppliers(String query, Pageable pageable) {
Page<ProductSupplier> productSuppliers;
if (query != null && !query.trim().isEmpty()) {
productSuppliers = productSupplierRepository.searchProductSuppliers(query, pageable);
} else {
productSuppliers = productSupplierRepository.findAll(pageable);
}
public Page<ProductSupplierResponse> getAllProductSuppliers(String query, Long productId, Long supplierId, Pageable pageable) {
String normalizedQuery = normalizeFilter(query);
Pageable mappedPageable = mapSortProperties(pageable);
Page<ProductSupplier> productSuppliers = productSupplierRepository.searchProductSuppliers(normalizedQuery, productId, supplierId, mappedPageable);
return productSuppliers.map(this::mapToResponse);
}
@@ -95,6 +97,33 @@ public class ProductSupplierService {
});
}
private String normalizeFilter(String value) {
if (value == null) {
return null;
}
String trimmed = value.trim();
return trimmed.isEmpty() ? null : trimmed;
}
private Pageable mapSortProperties(Pageable pageable) {
if (pageable.getSort().isUnsorted()) {
return pageable;
}
List<Sort.Order> orders = new ArrayList<>();
for (Sort.Order order : pageable.getSort()) {
String property = order.getProperty();
if ("productName".equalsIgnoreCase(property)) {
orders.add(new Sort.Order(order.getDirection(), "product.prodName"));
} else if ("supplierName".equalsIgnoreCase(property)) {
orders.add(new Sort.Order(order.getDirection(), "supplier.supCompany"));
} else {
orders.add(order);
}
}
return PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by(orders));
}
private ProductSupplierResponse mapToResponse(ProductSupplier productSupplier) {
return new ProductSupplierResponse(
productSupplier.getProduct().getProdId(),