| 1 |
package com.paymentlink.repository; |
| 2 |
|
| 3 |
import com.paymentlink.model.entity.Product; |
| 4 |
import org.springframework.data.jpa.repository.JpaRepository; |
| 5 |
import org.springframework.data.jpa.repository.Query; |
| 6 |
import org.springframework.data.repository.query.Param; |
| 7 |
import org.springframework.stereotype.Repository; |
| 8 |
|
| 9 |
import java.util.List; |
| 10 |
import java.util.Optional; |
| 11 |
|
| 12 |
@Repository |
| 13 |
public interface ProductRepository extends JpaRepository<Product, Long> { |
| 14 |
|
| 15 |
Optional<Product> findById(Long id); |
| 16 |
|
| 17 |
List<Product> findByCategory(String category); |
| 18 |
|
| 19 |
@Query("SELECT DISTINCT p.category FROM Product p WHERE p.category IS NOT NULL") |
| 20 |
List<String> findDistinctCategories(); |
| 21 |
|
| 22 |
@Query("SELECT p FROM Product p WHERE " + |
| 23 |
"LOWER(p.name) LIKE LOWER(CONCAT('%', :query, '%')) OR " + |
| 24 |
"LOWER(p.description) LIKE LOWER(CONCAT('%', :query, '%'))") |
| 25 |
List<Product> searchProducts(@Param("query") String query); |
| 26 |
|
| 27 |
@Query("SELECT p FROM Product p WHERE p.category = :category AND " + |
| 28 |
"(LOWER(p.name) LIKE LOWER(CONCAT('%', :query, '%')) OR " + |
| 29 |
"LOWER(p.description) LIKE LOWER(CONCAT('%', :query, '%')))") |
| 30 |
List<Product> searchProductsByCategory(@Param("query") String query, @Param("category") String category); |
| 31 |
} |
| 32 |
|
| 33 |
|