Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package com.bamboo.log.diary.dto.response;

import com.bamboo.log.diary.domain.Diary;
import jakarta.validation.constraints.NotEmpty;
import lombok.Builder;

import java.time.LocalDateTime;
import java.util.Base64;

@Builder
public record CheckDiaryResponse(@NotEmpty(message = "Date shouldn't be empty") LocalDateTime date,
@NotEmpty(message = "Diary Description shouldn't be empty") String diaryDescription,
@NotEmpty(message = "Summary Image shouldn't be empty") byte[] summaryImage
public record CheckDiaryResponse(
@NotEmpty(message = "Date shouldn't be empty") LocalDateTime date,
@NotEmpty(message = "Diary Description shouldn't be empty") String diaryDescription,
String summaryImage // 👈 Base64 문자열로 변경 (byte[] 대신 String)
) {
}
public static CheckDiaryResponse from(Diary diary, byte[] imageData) {
return new CheckDiaryResponse(
diary.getCreatedAt(),
diary.getContext(),
imageData != null ? Base64.getEncoder().encodeToString(imageData) : null
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bamboo.log.diary.dto.response;

import lombok.Builder;

import java.time.LocalDateTime;
import java.util.List;

Expand All @@ -11,12 +10,10 @@ public record GetDiariesOfMonthResponse(
String date,
List<DiaryOfMonth> diaries
) {

@Builder
public record DiaryOfMonth(
LocalDateTime createdAt,
String context,
byte[] summaryImage
String summaryImage
) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -70,15 +71,17 @@ public ResponseEntity getDiariesByMonth(String date) {
parseYearMonth.getStartOfMonth(), parseYearMonth.getEndOfMonth());

List<Long> diaryIds = diaries.stream().map(Diary::getId).toList();
Map<Long, byte[]> summaryImageMap = todaySummaryRepository.findByDiaryIdIn(diaryIds)
Map<Long, String> summaryImageMap = todaySummaryRepository.findByDiaryIdIn(diaryIds)
.stream()
.collect(Collectors.toMap(TodaySummary::getDiaryId, TodaySummary::getImageData));
.collect(Collectors.toMap(
TodaySummary::getDiaryId,
summary -> Base64.getEncoder().encodeToString(summary.getImageData()))); // ✅ Base64 변환

List<DiaryOfMonth> diaryOfMonthList = diaries.stream()
.map(diary -> new DiaryOfMonth(
diary.getCreatedAt(),
diary.getContext(),
summaryImageMap.getOrDefault(diary.getId(), null)
summaryImageMap.getOrDefault(diary.getId(), null) // Base64 문자열 전달
))
.toList();

Expand Down Expand Up @@ -110,19 +113,15 @@ public ResponseEntity getDiaryByDate(LocalDateTime date) {
Optional<TodaySummary> summaryImage = todaySummaryRepository.findByDiaryId(diaryByDate.getId());

return ResponseHandler.create200Response(new ResponseForm(),
CheckDiaryResponse.builder()
.date(diaryByDate.getCreatedAt())
.diaryDescription(diaryByDate.getContext())
.summaryImage(summaryImage.map(TodaySummary::getImageData).orElse(null))
.build());

CheckDiaryResponse.from(diaryByDate, summaryImage.map(TodaySummary::getImageData).orElse(null)));
} catch (RuntimeException e) {
return ResponseHandler.create404Error(new ResponseForm(), new IllegalArgumentException("해당 날짜에 작성된 일기가 없습니다."));
} catch (Exception e) {
return ResponseHandler.create500Error(new ResponseForm(), e);
}
}


private Diary saveDiary(CreateDiaryRequest createDiaryRequest, LocalDateTime localDateTime) {
UserEntity user = userContextUtil.getUserEntity();

Expand Down