-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchService.java
More file actions
50 lines (40 loc) · 1.86 KB
/
SearchService.java
File metadata and controls
50 lines (40 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cc.backend.search;
import cc.backend.amateurShow.entity.AmateurShow;
import cc.backend.amateurShow.repository.AmateurShowRepository;
import cc.backend.amateurShow.repository.specification.AmateurShowSpecification;
import cc.backend.apiPayLoad.PageResponse;
import cc.backend.search.dto.SearchShowResponseDTO;
import io.micrometer.core.instrument.search.Search;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class SearchService {
private final AmateurShowRepository amateurShowRepository;
public SearchShowResponseDTO.SearchShowDTO.SearchShowResultDTO searchAmateurShows(String keyword, int page, int size) {
String kw = StringUtils.hasText(keyword) ? keyword.trim() : null;
Pageable pageable = PageRequest.of(
page,
size,
Sort.by(Sort.Direction.DESC, "createdAt")
);
Specification<AmateurShow> spec = Specification
.where(AmateurShowSpecification.nameOrPerformerContains(kw))
.and(AmateurShowSpecification.isApproved());
// Page로 조회
Page<AmateurShow> pageResult = amateurShowRepository.findAll(spec, pageable);
// DTO 변환
Page<SearchShowResponseDTO.SearchShowDTO> dtoPage = pageResult.map(SearchShowResponseDTO.SearchShowDTO::from);
// 결과 반환
return SearchShowResponseDTO.SearchShowDTO.SearchShowResultDTO.builder()
.searchShowDTOs(dtoPage.getContent())
.total(pageResult.getTotalElements())
.build();
}
}