diff --git a/src/main/java/com/javatechie/crud/example/repository/ProductRepository.java b/src/main/java/com/javatechie/crud/example/repository/ProductRepository.java index 15ddb8a..248134a 100644 --- a/src/main/java/com/javatechie/crud/example/repository/ProductRepository.java +++ b/src/main/java/com/javatechie/crud/example/repository/ProductRepository.java @@ -2,8 +2,23 @@ import com.javatechie.crud.example.entity.Product; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; + +public interface ProductRepository extends JpaRepository { -public interface ProductRepository extends JpaRepository { Product findByName(String name); + + // ✅ Advanced Search Query + @Query("SELECT p FROM Product p WHERE " + + "(:name IS NULL OR LOWER(p.name) LIKE LOWER(CONCAT('%', :name, '%'))) " + + "AND (:minPrice IS NULL OR p.price >= :minPrice) " + + "AND (:maxPrice IS NULL OR p.price <= :maxPrice)") + List advancedSearch(@Param("name") String name, + @Param("minPrice") Double minPrice, + @Param("maxPrice") Double maxPrice); } +