diff --git a/src/main/java/com/genius/gitget/challenge/instance/dto/search/InstanceSearchResponse.java b/src/main/java/com/genius/gitget/challenge/instance/dto/search/InstanceSearchResponse.java index a618cb8f..129ecbbe 100644 --- a/src/main/java/com/genius/gitget/challenge/instance/dto/search/InstanceSearchResponse.java +++ b/src/main/java/com/genius/gitget/challenge/instance/dto/search/InstanceSearchResponse.java @@ -1,5 +1,6 @@ package com.genius.gitget.challenge.instance.dto.search; +import com.genius.gitget.global.file.dto.FileResponse; import com.querydsl.core.annotations.QueryProjection; import lombok.Builder; import lombok.Data; @@ -13,15 +14,17 @@ public class InstanceSearchResponse { private String keyword; private int pointPerPerson; private int participantCount; + private FileResponse fileResponse; @Builder @QueryProjection public InstanceSearchResponse(Long topicId, Long instanceId, String keyword, int pointPerPerson, - int participantCount) { + int participantCount, FileResponse fileResponse) { this.topicId = topicId; this.instanceId = instanceId; this.keyword = keyword; this.pointPerPerson = pointPerPerson; this.participantCount = participantCount; + this.fileResponse = fileResponse; } } \ No newline at end of file diff --git a/src/main/java/com/genius/gitget/challenge/instance/repository/SearchRepositoryCustom.java b/src/main/java/com/genius/gitget/challenge/instance/repository/SearchRepositoryCustom.java index 206366c8..95b22c7a 100644 --- a/src/main/java/com/genius/gitget/challenge/instance/repository/SearchRepositoryCustom.java +++ b/src/main/java/com/genius/gitget/challenge/instance/repository/SearchRepositoryCustom.java @@ -1,10 +1,10 @@ package com.genius.gitget.challenge.instance.repository; +import com.genius.gitget.challenge.instance.domain.Instance; import com.genius.gitget.challenge.instance.domain.Progress; -import com.genius.gitget.challenge.instance.dto.search.InstanceSearchResponse; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; public interface SearchRepositoryCustom { - Page search(Progress progress, String title, Pageable pageable); + Page search(Progress progress, String title, Pageable pageable); } diff --git a/src/main/java/com/genius/gitget/challenge/instance/repository/SearchRepositoryImpl.java b/src/main/java/com/genius/gitget/challenge/instance/repository/SearchRepositoryImpl.java index d147362c..90b791c7 100644 --- a/src/main/java/com/genius/gitget/challenge/instance/repository/SearchRepositoryImpl.java +++ b/src/main/java/com/genius/gitget/challenge/instance/repository/SearchRepositoryImpl.java @@ -2,9 +2,8 @@ import static com.genius.gitget.challenge.instance.domain.QInstance.instance; +import com.genius.gitget.challenge.instance.domain.Instance; import com.genius.gitget.challenge.instance.domain.Progress; -import com.genius.gitget.challenge.instance.dto.search.InstanceSearchResponse; -import com.genius.gitget.challenge.instance.dto.search.QInstanceSearchResponse; import com.querydsl.core.BooleanBuilder; import com.querydsl.jpa.impl.JPAQuery; import com.querydsl.jpa.impl.JPAQueryFactory; @@ -23,7 +22,7 @@ public SearchRepositoryImpl(EntityManager em) { } @Override - public Page search(Progress progressCond, String titleCond, Pageable pageable) { + public Page search(Progress progressCond, String titleCond, Pageable pageable) { BooleanBuilder builder = new BooleanBuilder(); if (progressCond != null) { @@ -33,11 +32,8 @@ public Page search(Progress progressCond, String titleCo builder.and(instance.title.contains(titleCond)); } - List content = queryFactory - .select(new QInstanceSearchResponse( - instance.topic.id, instance.id, instance.title, instance.pointPerPerson, - instance.participantCount)) - .from(instance) + List content = queryFactory + .selectFrom(instance) .where(builder) .orderBy(instance.startedDate.desc()) .offset(pageable.getOffset()) @@ -51,4 +47,4 @@ public Page search(Progress progressCond, String titleCo return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne); } -} +} \ No newline at end of file diff --git a/src/main/java/com/genius/gitget/challenge/instance/service/InstanceSearchService.java b/src/main/java/com/genius/gitget/challenge/instance/service/InstanceSearchService.java index b449b0ec..cbc921c5 100644 --- a/src/main/java/com/genius/gitget/challenge/instance/service/InstanceSearchService.java +++ b/src/main/java/com/genius/gitget/challenge/instance/service/InstanceSearchService.java @@ -1,8 +1,11 @@ package com.genius.gitget.challenge.instance.service; +import com.genius.gitget.challenge.instance.domain.Instance; import com.genius.gitget.challenge.instance.domain.Progress; import com.genius.gitget.challenge.instance.dto.search.InstanceSearchResponse; import com.genius.gitget.challenge.instance.repository.SearchRepository; +import com.genius.gitget.global.file.dto.FileResponse; +import com.genius.gitget.global.file.service.FilesService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; @@ -15,13 +18,15 @@ @Transactional(readOnly = true) @Slf4j public class InstanceSearchService { - + private final FilesService filesService; private final SearchRepository searchRepository; private final StringToEnum stringToEnum; private final String[] progressData = {"PREACTIVITY", "ACTIVITY", "DONE"}; - public Page searchInstances(String keyword, String progress, Pageable pageable){ + public Page searchInstances(String keyword, String progress, Pageable pageable) { + + Page search; Progress convertProgress; boolean flag = false; @@ -32,9 +37,22 @@ public Page searchInstances(String keyword, String progr } if (flag) { convertProgress = stringToEnum.convert(progress); - return searchRepository.search(convertProgress, keyword, pageable); + search = searchRepository.search(convertProgress, keyword, pageable); } else { - return searchRepository.search(null, keyword, pageable); + search = searchRepository.search(null, keyword, pageable); } + return search.map(this::convertToSearchResponse); + } + + private InstanceSearchResponse convertToSearchResponse(Instance instance) { + FileResponse fileResponse = filesService.convertToFileResponse(instance.getFiles()); + return InstanceSearchResponse.builder() + .topicId(instance.getTopic().getId()) + .instanceId(instance.getId()) + .keyword(instance.getTitle()) + .pointPerPerson(instance.getPointPerPerson()) + .participantCount(instance.getParticipantCount()) + .fileResponse(fileResponse) + .build(); } } diff --git a/src/test/java/com/genius/gitget/challenge/instance/repository/InstanceSearchRepositoryTest.java b/src/test/java/com/genius/gitget/challenge/instance/repository/InstanceSearchRepositoryTest.java index f0e11a1f..6c61ed1b 100644 --- a/src/test/java/com/genius/gitget/challenge/instance/repository/InstanceSearchRepositoryTest.java +++ b/src/test/java/com/genius/gitget/challenge/instance/repository/InstanceSearchRepositoryTest.java @@ -8,7 +8,6 @@ import com.genius.gitget.admin.topic.repository.TopicRepository; import com.genius.gitget.challenge.instance.domain.Instance; import com.genius.gitget.challenge.instance.dto.crud.InstanceCreateRequest; -import com.genius.gitget.challenge.instance.dto.search.InstanceSearchResponse; import com.genius.gitget.challenge.instance.service.InstanceSearchService; import com.genius.gitget.challenge.instance.service.InstanceService; import com.genius.gitget.util.file.FileTestUtil; @@ -109,9 +108,9 @@ public void setup() throws IOException { public void 검색_조건_없이_테스트() throws Exception { for (int i = 0; i < 5; i++) { PageRequest pageRequest = PageRequest.of(i, 2); - Page result = searchRepository.search(null, null, pageRequest); - for (InstanceSearchResponse instanceSearchResponse : result) { - System.out.println("instanceSearchResponse = " + instanceSearchResponse.getInstanceId()); + Page result = searchRepository.search(null, null, pageRequest); + for (Instance instance : result) { + System.out.println("instanceSearchResponse = " + instance.getId()); } System.out.println("========== " + i + 1 + " 번째 끝 ========="); } @@ -120,10 +119,10 @@ public void setup() throws IOException { @Test public void 챌린지_제목으로_검색_테스트() throws Exception { PageRequest pageRequest = PageRequest.of(0, 10); - Page result = searchRepository.search(null, "2", pageRequest); + Page result = searchRepository.search(null, "2", pageRequest); int cnt = 0; - for (InstanceSearchResponse instanceSearchResponse : result) { - if (instanceSearchResponse != null) { + for (Instance instance : result) { + if (instance != null) { cnt++; } } @@ -134,10 +133,10 @@ public void setup() throws IOException { @Test public void 챌린지_현황으로_검색_테스트() throws Exception { PageRequest pageRequest = PageRequest.of(0, 10); - Page result = searchRepository.search(PREACTIVITY, null, pageRequest); + Page result = searchRepository.search(PREACTIVITY, null, pageRequest); int cnt = 0; - for (InstanceSearchResponse instanceSearchResponse : result) { - if (instanceSearchResponse != null) { + for (Instance instance : result) { + if (instance != null) { cnt++; } } @@ -147,10 +146,10 @@ public void setup() throws IOException { @Test public void 챌린지_현황으로_검색_테스트2() throws Exception { PageRequest pageRequest = PageRequest.of(0, 10); - Page result = searchRepository.search(DONE, null, pageRequest); + Page result = searchRepository.search(DONE, null, pageRequest); int cnt = 0; - for (InstanceSearchResponse instanceSearchResponse : result) { - if (instanceSearchResponse != null) { + for (Instance instance : result) { + if (instance != null) { cnt++; } } @@ -160,10 +159,10 @@ public void setup() throws IOException { @Test public void 챌린지_현황으로_검색_테스트3() throws Exception { PageRequest pageRequest = PageRequest.of(0, 10); - Page result = searchRepository.search(ACTIVITY, null, pageRequest); + Page result = searchRepository.search(ACTIVITY, null, pageRequest); int cnt = 0; - for (InstanceSearchResponse instanceSearchResponse : result) { - if (instanceSearchResponse != null) { + for (Instance instance : result) { + if (instance != null) { cnt++; } } @@ -173,10 +172,10 @@ public void setup() throws IOException { @Test public void 챌린지_현황과_챌린지_제목으로_검색_테스트() throws Exception { PageRequest pageRequest = PageRequest.of(0, 10); - Page result = searchRepository.search(PREACTIVITY, "3", pageRequest); + Page result = searchRepository.search(PREACTIVITY, "3", pageRequest); int cnt = 0; - for (InstanceSearchResponse instanceSearchResponse : result) { - if (instanceSearchResponse != null) { + for (Instance instance : result) { + if (instance != null) { cnt++; } }