Skip to content

Commit

Permalink
Merge pull request #260 from tipi-tapi/feature/258
Browse files Browse the repository at this point in the history
[월별 일기 목록 API] 대표 이미지가 없을 경우 분기 처리 추가
  • Loading branch information
akalswl14 authored Oct 11, 2023
2 parents c1e0063 + 135d4c5 commit b392f60
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class GetMonthlyDiariesResponse {
@Schema(description = "일기 아이디", requiredMode = RequiredMode.REQUIRED)
private final Long id;

@Schema(description = "이미지 URL", requiredMode = RequiredMode.REQUIRED)
@Schema(description = "대표 이미지 URL", requiredMode = RequiredMode.REQUIRED)
@Setter
private String imageUrl;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public interface ImageQueryRepository {

Optional<Image> findByImageIdAndDiaryUser(Long imageId, User user);

Optional<Image> findRecentByDiary(Long diaryId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@ public Optional<Image> findByImageIdAndDiaryUser(Long imageId, User user) {
.and(image.deletedAt.isNull()))
.fetchFirst());
}

@Override
public Optional<Image> findRecentByDiary(Long diaryId) {
return Optional.ofNullable(queryFactory
.selectFrom(image)
.where(image.diary.diaryId.eq(diaryId).and(image.deletedAt.isNull()))
.orderBy(image.createdAt.desc())
.fetchFirst());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,16 @@ public GetDiaryResponse getDiary(Long userId, Long diaryId, Language language) {
return GetDiaryResponse.of(diary, selectedImageUrl, sortedImages, emotionText, promptText);
}

@Transactional
public List<GetMonthlyDiariesResponse> getMonthlyDiaries(Long userId, int year, int month) {
validateUserService.validateUserById(userId);
LocalDateTime startMonth = DateUtils.getStartDate(year, month);
LocalDateTime endMonth = DateUtils.getEndDate(year, month);
List<GetMonthlyDiariesResponse> monthlyDiaries = diaryRepository.getMonthlyDiaries(
userId, startMonth, endMonth);

for (int i = 0; i < monthlyDiaries.size(); i++) {
GetMonthlyDiariesResponse monthlyDiary = monthlyDiaries.get(i);
if (monthlyDiary.getImageUrl() == null) {
log.error("DiaryId가 {}에 해당하는 이미지가 없습니다.", monthlyDiary.getId());
monthlyDiaries.remove(i--);
} else {
monthlyDiary.setImageUrl(
r2PreSignedService.getCustomDomainUrl(monthlyDiary.getImageUrl()));
}
}
validateSelectedImageAndConvertUrl(monthlyDiaries);

return monthlyDiaries;
}

Expand Down Expand Up @@ -145,4 +138,25 @@ public GetDiaryLimitResponse getDrawLimit(Long userId) {

return GetDiaryLimitResponse.of(available, lastDiaryDate, ticketCreatedAt);
}

private void validateSelectedImageAndConvertUrl(
List<GetMonthlyDiariesResponse> monthlyDiaries) {
for (int i = 0; i < monthlyDiaries.size(); i++) {
GetMonthlyDiariesResponse diaryResponse = monthlyDiaries.get(i);
if (diaryResponse.getImageUrl() == null) {
log.error("DiaryId가 {}인 일기에 해당하는 대표 이미지가 없습니다.", diaryResponse.getId());
Optional<Image> latestImage = imageService.getOneLatestImage(diaryResponse.getId());
if (latestImage.isPresent()) {
latestImage.get().setSelected(true);
diaryResponse.setImageUrl(
r2PreSignedService.getCustomDomainUrl(latestImage.get().getImageUrl()));
} else {
monthlyDiaries.remove(i--);
}
} else {
diaryResponse.setImageUrl(
r2PreSignedService.getCustomDomainUrl(diaryResponse.getImageUrl()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Date;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -34,6 +35,10 @@ public List<Image> getLatestImages(Diary diary) {
return imageRepository.findLatestByDiary(diary.getDiaryId());
}

public Optional<Image> getOneLatestImage(Long diaryId) {
return imageRepository.findRecentByDiary(diaryId);
}

public Image createImage(Diary diary, String imagePath, boolean isSelected) {
return imageRepository.save(Image.create(diary, imagePath, isSelected));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static tipitapi.drawmytoday.common.testdata.TestDiary.createDiaryWithId;
import static tipitapi.drawmytoday.common.testdata.TestDiary.createDiaryWithIdAndCreatedAt;
Expand Down Expand Up @@ -237,25 +238,62 @@ void more_12_then_throws_BusinessException(int value) {
@DisplayName("주어진 유저의 일기가 존재할 경우")
class if_diary_of_user_exists {

@Test
@DisplayName("일기에 해당하는 이미지가 없을 경우 해당 일기는 반환하지 않는다.")
void it_returns_diaries_without_image() {
// given
User user = createUserWithId(1L);
GetMonthlyDiariesResponse response = new GetMonthlyDiariesResponse(
1L, null, LocalDateTime.of(2023, 6, 1, 0, 0));
List<GetMonthlyDiariesResponse> responses = new ArrayList<>();
responses.add(response);
given(validateUserService.validateUserById(1L)).willReturn(user);
given(diaryRepository.getMonthlyDiaries(any(Long.class), any(LocalDateTime.class),
any(LocalDateTime.class))).willReturn(responses);
@Nested
@DisplayName("일기에 해당하는 대표 이미지가 없을 경우")
class If_selected_image_not_exist {

// when
List<GetMonthlyDiariesResponse> monthlyDiaries = diaryService.getMonthlyDiaries(
1L, 2023, 6);
@Test
@DisplayName("가장 최신 이미지를 대표 이미지로 설정한 이후 해당 일기를 포함한 전체 일기를 반환한다.")
void change_latest_image_to_selected_image_then_return() {
// given
User user = createUserWithId(1L);
Long diaryId = 2L;
Diary diary = createDiaryWithId(diaryId, user, createEmotion());
Image latestImage = createImage(diary);
latestImage.setSelected(false);
GetMonthlyDiariesResponse response = new GetMonthlyDiariesResponse(
diaryId, null, LocalDateTime.of(2023, 6, 1, 0, 0));
List<GetMonthlyDiariesResponse> responses = new ArrayList<>();
responses.add(response);

given(validateUserService.validateUserById(1L)).willReturn(user);
given(diaryRepository.getMonthlyDiaries(any(Long.class),
any(LocalDateTime.class), any(LocalDateTime.class)))
.willReturn(responses);
given(imageService.getOneLatestImage(eq(diaryId)))
.willReturn(Optional.of(latestImage));

// when
List<GetMonthlyDiariesResponse> monthlyDiaries = diaryService.getMonthlyDiaries(
1L, 2023, 6);

// then
assertThat(monthlyDiaries).hasSize(1);
assertThat(latestImage.isSelected()).isTrue();
}

// then
assertThat(monthlyDiaries).hasSize(0);
@Test
@DisplayName("가장 최신 이미지도 없을 경우 전체 일기 중 해당 일기는 반환하지 않는다.")
void If_latest_image_not_exist_then_return_diaries_without_image() {
// given
User user = createUserWithId(1L);
Long diaryId = 2L;
GetMonthlyDiariesResponse response = new GetMonthlyDiariesResponse(
diaryId, null, LocalDateTime.of(2023, 6, 1, 0, 0));
List<GetMonthlyDiariesResponse> responses = new ArrayList<>();
responses.add(response);
given(validateUserService.validateUserById(1L)).willReturn(user);
given(diaryRepository.getMonthlyDiaries(any(Long.class),
any(LocalDateTime.class), any(LocalDateTime.class))).willReturn(responses);
given(imageService.getOneLatestImage(eq(diaryId))).willReturn(Optional.empty());

// when
List<GetMonthlyDiariesResponse> monthlyDiaries = diaryService.getMonthlyDiaries(
1L, 2023, 6);

// then
assertThat(monthlyDiaries).hasSize(0);
}
}

@Test
Expand All @@ -272,8 +310,7 @@ void it_returns_diaries() {
any(LocalDateTime.class))).willReturn(responses);

List<GetMonthlyDiariesResponse> getDiaryResponses = diaryService.getMonthlyDiaries(
1L,
2023, 6);
1L, 2023, 6);

assertThat(getDiaryResponses).hasSize(1);
assertThat(getDiaryResponses.get(0).getId()).isEqualTo(diaryId);
Expand Down

0 comments on commit b392f60

Please sign in to comment.