package com.example.demo.news.repository;

import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.news.model.NewsEntity;

@Repository
public interface NewsRepository extends CrudRepository<NewsEntity, Long>,
        PagingAndSortingRepository<NewsEntity, Long> {

    Optional<NewsEntity> findByNameIgnoreCase(String name);

    Page<NewsEntity> findAll(Pageable pageable);

    Page<NewsEntity> findByDepartmentId(Pageable pageable, long departmentId);

    @Query("select n " +
           "from NewsEntity n " +
           "where lower(n.description) like lower(Concat('%', :description, '%'))")
    Page<NewsEntity> findWithSearch(Pageable pageable, String description);

    @Query("select n " +
           "from NewsEntity n " +
           "where lower(n.description) like lower(Concat('%', :description, '%')) " +
           "and n.department.Id = :departmentId")
    Page<NewsEntity> findByDepartmentIdSearch(Pageable pageable, Long departmentId, String description);
}
