Skip to content

Commit

Permalink
fix: Instance 검색 응답 데이터에 파일이 포함되어 있지 않은 버그 픽스
Browse files Browse the repository at this point in the history
  • Loading branch information
SSung023 committed Apr 21, 2024
1 parent c0cd332 commit 95086b8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<InstanceSearchResponse> search(Progress progress, String title, Pageable pageable);
Page<Instance> search(Progress progress, String title, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,7 +22,7 @@ public SearchRepositoryImpl(EntityManager em) {
}

@Override
public Page<InstanceSearchResponse> search(Progress progressCond, String titleCond, Pageable pageable) {
public Page<Instance> search(Progress progressCond, String titleCond, Pageable pageable) {
BooleanBuilder builder = new BooleanBuilder();

if (progressCond != null) {
Expand All @@ -33,11 +32,8 @@ public Page<InstanceSearchResponse> search(Progress progressCond, String titleCo
builder.and(instance.title.contains(titleCond));
}

List<InstanceSearchResponse> content = queryFactory
.select(new QInstanceSearchResponse(
instance.topic.id, instance.id, instance.title, instance.pointPerPerson,
instance.participantCount))
.from(instance)
List<Instance> content = queryFactory
.selectFrom(instance)
.where(builder)
.orderBy(instance.startedDate.desc())
.offset(pageable.getOffset())
Expand All @@ -51,4 +47,4 @@ public Page<InstanceSearchResponse> search(Progress progressCond, String titleCo

return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<InstanceSearchResponse> searchInstances(String keyword, String progress, Pageable pageable){
public Page<InstanceSearchResponse> searchInstances(String keyword, String progress, Pageable pageable) {

Page<Instance> search;
Progress convertProgress;
boolean flag = false;

Expand All @@ -32,9 +37,22 @@ public Page<InstanceSearchResponse> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<InstanceSearchResponse> result = searchRepository.search(null, null, pageRequest);
for (InstanceSearchResponse instanceSearchResponse : result) {
System.out.println("instanceSearchResponse = " + instanceSearchResponse.getInstanceId());
Page<Instance> result = searchRepository.search(null, null, pageRequest);
for (Instance instance : result) {
System.out.println("instanceSearchResponse = " + instance.getId());
}
System.out.println("========== " + i + 1 + " 번째 끝 =========");
}
Expand All @@ -120,10 +119,10 @@ public void setup() throws IOException {
@Test
public void 챌린지_제목으로_검색_테스트() throws Exception {
PageRequest pageRequest = PageRequest.of(0, 10);
Page<InstanceSearchResponse> result = searchRepository.search(null, "2", pageRequest);
Page<Instance> result = searchRepository.search(null, "2", pageRequest);
int cnt = 0;
for (InstanceSearchResponse instanceSearchResponse : result) {
if (instanceSearchResponse != null) {
for (Instance instance : result) {
if (instance != null) {
cnt++;
}
}
Expand All @@ -134,10 +133,10 @@ public void setup() throws IOException {
@Test
public void 챌린지_현황으로_검색_테스트() throws Exception {
PageRequest pageRequest = PageRequest.of(0, 10);
Page<InstanceSearchResponse> result = searchRepository.search(PREACTIVITY, null, pageRequest);
Page<Instance> result = searchRepository.search(PREACTIVITY, null, pageRequest);
int cnt = 0;
for (InstanceSearchResponse instanceSearchResponse : result) {
if (instanceSearchResponse != null) {
for (Instance instance : result) {
if (instance != null) {
cnt++;
}
}
Expand All @@ -147,10 +146,10 @@ public void setup() throws IOException {
@Test
public void 챌린지_현황으로_검색_테스트2() throws Exception {
PageRequest pageRequest = PageRequest.of(0, 10);
Page<InstanceSearchResponse> result = searchRepository.search(DONE, null, pageRequest);
Page<Instance> result = searchRepository.search(DONE, null, pageRequest);
int cnt = 0;
for (InstanceSearchResponse instanceSearchResponse : result) {
if (instanceSearchResponse != null) {
for (Instance instance : result) {
if (instance != null) {
cnt++;
}
}
Expand All @@ -160,10 +159,10 @@ public void setup() throws IOException {
@Test
public void 챌린지_현황으로_검색_테스트3() throws Exception {
PageRequest pageRequest = PageRequest.of(0, 10);
Page<InstanceSearchResponse> result = searchRepository.search(ACTIVITY, null, pageRequest);
Page<Instance> result = searchRepository.search(ACTIVITY, null, pageRequest);
int cnt = 0;
for (InstanceSearchResponse instanceSearchResponse : result) {
if (instanceSearchResponse != null) {
for (Instance instance : result) {
if (instance != null) {
cnt++;
}
}
Expand All @@ -173,10 +172,10 @@ public void setup() throws IOException {
@Test
public void 챌린지_현황과_챌린지_제목으로_검색_테스트() throws Exception {
PageRequest pageRequest = PageRequest.of(0, 10);
Page<InstanceSearchResponse> result = searchRepository.search(PREACTIVITY, "3", pageRequest);
Page<Instance> result = searchRepository.search(PREACTIVITY, "3", pageRequest);
int cnt = 0;
for (InstanceSearchResponse instanceSearchResponse : result) {
if (instanceSearchResponse != null) {
for (Instance instance : result) {
if (instance != null) {
cnt++;
}
}
Expand Down

0 comments on commit 95086b8

Please sign in to comment.