top5 조회 로직 구현 

- data JPA의 내장 기능 사용 Top5로 호출하면 limit5를 걸어서 조회해줌

public interface CafeRepository extends JpaRepository<Cafe, Long>{

    List<Cafe> findTop5ByOrderByCreatedDateDesc();
}

 

service 로직

디비에서 받아온 List를 Cafe -> CafeListResponseDto로 필요한 데이터만 담아 클라이언트로 반환

	List<Cafe> cafeList = cafeRepository.findTop5ByOrderByCreatedDateDesc();

        List<CafeListResponseDto> cafeListResponseDtos = cafeList.stream()
                .map(c -> new CafeListResponseDto(c))
                .collect(Collectors.toList());

+ Recent posts