Skip to content

Commit

Permalink
fix: 학교 검색 시 발생하는 NPE 수정
Browse files Browse the repository at this point in the history
- 학교 식별자 목록으로 최근 축제 시작일 받아오는 클래스 이름 변경
- SchoolSearchUpcomingFestivalV1QueryService -> SchoolUpcomingFestivalStartDateV1QueryService
  • Loading branch information
seokjin8678 committed Apr 25, 2024
1 parent b231a7b commit 69170c4
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@
import static java.util.stream.Collectors.toUnmodifiableMap;

import com.festago.festival.repository.RecentSchoolFestivalV1QueryDslRepository;
import com.festago.school.application.v1.SchoolSearchUpcomingFestivalV1QueryService;
import com.festago.school.dto.v1.SchoolSearchUpcomingFestivalV1Response;
import com.festago.school.application.v1.SchoolUpcomingFestivalStartDateV1QueryService;
import com.festago.school.dto.v1.SchoolUpcomingFestivalStartDateV1Response;
import java.time.Clock;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class QueryDslSchoolSearchUpcomingFestivalV1QueryService implements SchoolSearchUpcomingFestivalV1QueryService {
public class QueryDslSchoolUpcomingFestivalStartDateV1QueryService implements
SchoolUpcomingFestivalStartDateV1QueryService {

private final RecentSchoolFestivalV1QueryDslRepository recentSchoolFestivalV1QueryDslRepository;
private final Clock clock;

@Override
public Map<Long, SchoolSearchUpcomingFestivalV1Response> searchUpcomingFestivals(List<Long> schoolIds) {
public Map<Long, LocalDate> getSchoolIdToUpcomingFestivalStartDate(List<Long> schoolIds) {
return recentSchoolFestivalV1QueryDslRepository.findRecentSchoolFestivals(schoolIds, LocalDate.now(clock))
.stream()
.collect(toUnmodifiableMap(SchoolSearchUpcomingFestivalV1Response::schoolId, Function.identity()));
.collect(toUnmodifiableMap(SchoolUpcomingFestivalStartDateV1Response::schoolId,
SchoolUpcomingFestivalStartDateV1Response::startDate));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import com.festago.common.querydsl.QueryDslRepositorySupport;
import com.festago.festival.domain.Festival;
import com.festago.school.dto.v1.QSchoolSearchUpcomingFestivalV1Response;
import com.festago.school.dto.v1.SchoolSearchUpcomingFestivalV1Response;
import com.festago.school.dto.v1.QSchoolUpcomingFestivalStartDateV1Response;
import com.festago.school.dto.v1.SchoolUpcomingFestivalStartDateV1Response;
import java.time.LocalDate;
import java.util.List;
import org.springframework.stereotype.Repository;
Expand All @@ -17,12 +17,12 @@ public RecentSchoolFestivalV1QueryDslRepository() {
super(Festival.class);
}

public List<SchoolSearchUpcomingFestivalV1Response> findRecentSchoolFestivals(
public List<SchoolUpcomingFestivalStartDateV1Response> findRecentSchoolFestivals(
List<Long> schoolIds,
LocalDate now
) {
return select(
new QSchoolSearchUpcomingFestivalV1Response(
new QSchoolUpcomingFestivalStartDateV1Response(
festival.school.id,
festival.festivalDuration.startDate.min()
))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.festago.school.dto.v1.SchoolSearchV1Response;
import com.festago.school.dto.v1.SchoolTotalSearchV1Response;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -13,17 +15,25 @@
public class SchoolTotalSearchV1QueryService {

private final SchoolSearchV1QueryService schoolSearchV1QueryService;
private final SchoolSearchUpcomingFestivalV1QueryService schoolSearchUpcomingFestivalV1QueryService;
private final SchoolUpcomingFestivalStartDateV1QueryService schoolUpcomingFestivalStartDateV1QueryService;

public List<SchoolTotalSearchV1Response> searchSchools(String keyword) {
var schoolSearchResponses = schoolSearchV1QueryService.searchSchools(keyword);
List<SchoolSearchV1Response> schoolSearchResponses = schoolSearchV1QueryService.searchSchools(keyword);
List<Long> schoolIds = schoolSearchResponses.stream()
.map(SchoolSearchV1Response::id)
.toList();
var schoolIdToUpcomingFestivalResponse = schoolSearchUpcomingFestivalV1QueryService.searchUpcomingFestivals(
schoolIds);
Map<Long, LocalDate> schoolIdToUpcomingFestivalStartDate = getSchoolIdToUpcomingFestivalStartDate(schoolIds);
return schoolSearchResponses.stream()
.map(it -> SchoolTotalSearchV1Response.of(it, schoolIdToUpcomingFestivalResponse.get(it.id())))
.map(schoolSearchResponse -> new SchoolTotalSearchV1Response(
schoolSearchResponse.id(),
schoolSearchResponse.name(),
schoolSearchResponse.logoUrl(),
schoolIdToUpcomingFestivalStartDate.get(schoolSearchResponse.id())
))
.toList();
}

private Map<Long, LocalDate> getSchoolIdToUpcomingFestivalStartDate(List<Long> schoolIds) {
return schoolUpcomingFestivalStartDateV1QueryService.getSchoolIdToUpcomingFestivalStartDate(schoolIds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.festago.school.application.v1;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

public interface SchoolUpcomingFestivalStartDateV1QueryService {

Map<Long, LocalDate> getSchoolIdToUpcomingFestivalStartDate(List<Long> schoolIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,4 @@ public record SchoolTotalSearchV1Response(
@Nullable LocalDate upcomingFestivalStartDate
) {

public static SchoolTotalSearchV1Response of(
SchoolSearchV1Response schoolSearchV1Response,
SchoolSearchUpcomingFestivalV1Response schoolSearchUpcomingFestivalV1Response
) {
return new SchoolTotalSearchV1Response(
schoolSearchV1Response.id(),
schoolSearchV1Response.name(),
schoolSearchV1Response.logoUrl(),
schoolSearchUpcomingFestivalV1Response.startDate()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import com.querydsl.core.annotations.QueryProjection;
import java.time.LocalDate;

public record SchoolSearchUpcomingFestivalV1Response(
public record SchoolUpcomingFestivalStartDateV1Response(
Long schoolId,
LocalDate startDate
) {

@QueryProjection
public SchoolSearchUpcomingFestivalV1Response {
public SchoolUpcomingFestivalStartDateV1Response {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class QueryDslSchoolSearchRecentFestivalV1QueryServiceIntegrationTest extends Ap
private static final String POSTER_IMAGE_URL = "https://image.com/posterimage.png";

@Autowired
QueryDslSchoolSearchUpcomingFestivalV1QueryService queryDslSchoolSearchRecentFestivalV1QueryService;
QueryDslSchoolUpcomingFestivalStartDateV1QueryService schoolUpcomingFestivalStartDateV1QueryService;

@Autowired
SchoolCommandService schoolCommandService;
Expand Down Expand Up @@ -78,13 +78,13 @@ void setUp() {
.willReturn(TimeInstantProvider.from(_6월_14일));

// when
var actual = queryDslSchoolSearchRecentFestivalV1QueryService.searchUpcomingFestivals(
var actual = schoolUpcomingFestivalStartDateV1QueryService.getSchoolIdToUpcomingFestivalStartDate(
List.of(테코대학교_식별자, 우테대학교_식별자)
);

// then
assertThat(actual.get(테코대학교_식별자).startDate()).isEqualTo(_6월_15일);
assertThat(actual.get(우테대학교_식별자).startDate()).isEqualTo(_6월_16일);
assertThat(actual.get(테코대학교_식별자)).isEqualTo(_6월_15일);
assertThat(actual.get(우테대학교_식별자)).isEqualTo(_6월_16일);
}

@Test
Expand All @@ -94,13 +94,13 @@ void setUp() {
.willReturn(TimeInstantProvider.from(_6월_15일));

// when
var actual = queryDslSchoolSearchRecentFestivalV1QueryService.searchUpcomingFestivals(
var actual = schoolUpcomingFestivalStartDateV1QueryService.getSchoolIdToUpcomingFestivalStartDate(
List.of(테코대학교_식별자, 우테대학교_식별자)
);

// then
assertThat(actual.get(테코대학교_식별자).startDate()).isEqualTo(_6월_15일);
assertThat(actual.get(우테대학교_식별자).startDate()).isEqualTo(_6월_16일);
assertThat(actual.get(테코대학교_식별자)).isEqualTo(_6월_15일);
assertThat(actual.get(우테대학교_식별자)).isEqualTo(_6월_16일);
}

@Test
Expand All @@ -110,13 +110,13 @@ void setUp() {
.willReturn(TimeInstantProvider.from(_6월_16일));

// when
var actual = queryDslSchoolSearchRecentFestivalV1QueryService.searchUpcomingFestivals(
var actual = schoolUpcomingFestivalStartDateV1QueryService.getSchoolIdToUpcomingFestivalStartDate(
List.of(테코대학교_식별자, 우테대학교_식별자)
);

// then
assertThat(actual.get(테코대학교_식별자).startDate()).isEqualTo(_6월_16일);
assertThat(actual.get(우테대학교_식별자).startDate()).isEqualTo(_6월_16일);
assertThat(actual.get(테코대학교_식별자)).isEqualTo(_6월_16일);
assertThat(actual.get(우테대학교_식별자)).isEqualTo(_6월_16일);
}

@Test
Expand All @@ -126,13 +126,13 @@ void setUp() {
.willReturn(TimeInstantProvider.from(_6월_17일));

// when
var actual = queryDslSchoolSearchRecentFestivalV1QueryService.searchUpcomingFestivals(
var actual = schoolUpcomingFestivalStartDateV1QueryService.getSchoolIdToUpcomingFestivalStartDate(
List.of(테코대학교_식별자, 우테대학교_식별자)
);

// then
assertThat(actual.get(테코대학교_식별자)).isNull();
assertThat(actual.get(우테대학교_식별자).startDate()).isEqualTo(_6월_16일);
assertThat(actual.get(우테대학교_식별자)).isEqualTo(_6월_16일);
}

@Test
Expand All @@ -142,7 +142,7 @@ void setUp() {
.willReturn(TimeInstantProvider.from(_6월_18일));

// when
var actual = queryDslSchoolSearchRecentFestivalV1QueryService.searchUpcomingFestivals(
var actual = schoolUpcomingFestivalStartDateV1QueryService.getSchoolIdToUpcomingFestivalStartDate(
List.of(테코대학교_식별자, 우테대학교_식별자)
);

Expand Down

0 comments on commit 69170c4

Please sign in to comment.