Skip to content

Commit f86b5f8

Browse files
committed
refactor: 내 다이어리 조회와 캘린더 조회 DTO 분리
1 parent 073134a commit f86b5f8

5 files changed

Lines changed: 78 additions & 10 deletions

File tree

src/main/java/apptive/team5/diary/controller/DiaryController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ public ResponseEntity<Page<UserDiaryResponseDto>> getUserDiaries(
7070
}
7171

7272
@GetMapping("/my/calendar")
73-
public ResponseEntity<List<MyDiaryResponseDto>> getMyDiariesByPeriod(
73+
public ResponseEntity<List<CalendarDiaryResponseDto>> getMyDiariesByPeriod(
7474
@AuthenticationPrincipal
7575
Long userId,
7676
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
7777
LocalDate start,
7878
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
7979
LocalDate end
8080
) {
81-
List<MyDiaryResponseDto> response = diaryService.getMyDiariesByPeriod(userId, start, end);
81+
List<CalendarDiaryResponseDto> response = diaryService.getMyDiariesByPeriod(userId, start, end);
8282
return ResponseEntity.status(HttpStatus.OK).body(response);
8383
}
8484

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package apptive.team5.diary.dto;
2+
3+
import apptive.team5.diary.domain.DiaryEntity;
4+
import apptive.team5.diary.domain.DiaryScope;
5+
6+
import java.time.LocalDateTime;
7+
8+
public record CalendarDiaryResponseDto(
9+
Long diaryId,
10+
String artist,
11+
String musicTitle,
12+
String albumImageUrl,
13+
String content,
14+
String videoUrl,
15+
DiaryScope scope,
16+
String duration,
17+
String totalDuration,
18+
String start,
19+
String end,
20+
LocalDateTime createDate,
21+
LocalDateTime updateDate
22+
) implements DiaryResponseDto {
23+
public static CalendarDiaryResponseDto from(DiaryEntity diary) {
24+
return new CalendarDiaryResponseDto(
25+
diary.getId(),
26+
diary.getArtist(),
27+
diary.getMusicTitle(),
28+
diary.getAlbumImageUrl(),
29+
diary.getContent(),
30+
diary.getVideoUrl(),
31+
diary.getScope(),
32+
diary.getDuration(),
33+
diary.getTotalDuration(),
34+
diary.getStart(),
35+
diary.getEnd(),
36+
diary.getCreateDateTime(),
37+
diary.getUpdateDateTime()
38+
);
39+
}
40+
41+
}
42+

src/main/java/apptive/team5/diary/dto/MyDiaryResponseDto.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ public record MyDiaryResponseDto(
1818
String start,
1919
String end,
2020
LocalDateTime createDate,
21-
LocalDateTime updateDate
21+
LocalDateTime updateDate,
22+
boolean isLiked,
23+
Long likeCount,
24+
Long userId
2225
) implements DiaryResponseDto {
23-
public static MyDiaryResponseDto from(DiaryEntity diary) {
26+
public static MyDiaryResponseDto from(DiaryEntity diary, boolean isLiked, Long likeCount, Long userId) {
2427
return new MyDiaryResponseDto(
2528
diary.getId(),
2629
diary.getArtist(),
@@ -34,7 +37,10 @@ public static MyDiaryResponseDto from(DiaryEntity diary) {
3437
diary.getStart(),
3538
diary.getEnd(),
3639
diary.getCreateDateTime(),
37-
diary.getUpdateDateTime()
40+
diary.getUpdateDateTime(),
41+
isLiked,
42+
likeCount,
43+
userId
3844
);
3945
}
4046
}

src/main/java/apptive/team5/diary/service/DiaryService.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,23 @@ public class DiaryService {
3535
public Page<MyDiaryResponseDto> getMyDiaries(Long userId, Pageable pageable) {
3636
UserEntity foundUser = userLowService.getReferenceById(userId);
3737

38-
return diaryLowService.findDiaryByUser(foundUser, pageable)
39-
.map(MyDiaryResponseDto::from);
38+
Page<DiaryEntity> diaryPage = diaryLowService.findDiaryByUser(foundUser, pageable);
39+
40+
List<Long> diaryIds = diaryPage.stream()
41+
.map(DiaryEntity::getId)
42+
.toList();
43+
44+
45+
Set<Long> likedDiaryIds = diaryLikeLowService.findLikedDiaryIdsByUser(userId, diaryIds);
46+
Map<Long, Long> likeCountsMaps = diaryLikeLowService.findLikeCountsByDiaryIds(diaryIds);
47+
48+
return diaryResponseMapper.mapToResponseDto(
49+
diaryPage,
50+
likedDiaryIds,
51+
likeCountsMaps,
52+
userId,
53+
MyDiaryResponseDto::from
54+
);
4055
}
4156

4257
@Transactional(readOnly = true)
@@ -96,13 +111,13 @@ public Page<UserDiaryResponseDto> getUserDiaries(Long targetUserId, Long current
96111
}
97112

98113
@Transactional(readOnly = true)
99-
public List<MyDiaryResponseDto> getMyDiariesByPeriod(Long userId, LocalDate startDate, LocalDate endDate) {
114+
public List<CalendarDiaryResponseDto> getMyDiariesByPeriod(Long userId, LocalDate startDate, LocalDate endDate) {
100115
LocalDateTime startDateTime = startDate.atStartOfDay();
101116
LocalDateTime endDateTime = endDate.atTime(LocalTime.MAX);
102117

103118
return diaryLowService.findByUserIdAndPeriod(userId, startDateTime, endDateTime)
104119
.stream()
105-
.map(MyDiaryResponseDto::from)
120+
.map(CalendarDiaryResponseDto::from)
106121
.toList();
107122
}
108123

src/test/java/apptive/team5/diary/controller/DiaryControllerTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ void getMyMusicDiary() throws Exception {
8383
// given
8484
DiaryEntity firstDiary = diaryRepository.save(TestUtil.makeDiaryEntity(testUser));
8585
DiaryEntity secondDiary = diaryRepository.save(TestUtil.makeDiaryEntity(testUser));
86+
diaryLikeLowService.saveDiaryLike(new DiaryLikeEntity(testUser, secondDiary)); // 두번째 다이어리에 좋아요
8687

8788
TestSecurityContextHolderInjection.inject(testUser.getId(), testUser.getRoleType());
8889

@@ -115,6 +116,10 @@ void getMyMusicDiary() throws Exception {
115116
softly.assertThat(diaryResponse.totalDuration()).isEqualTo(secondDiary.getTotalDuration());
116117
softly.assertThat(diaryResponse.start()).isEqualTo(secondDiary.getStart());
117118
softly.assertThat(diaryResponse.end()).isEqualTo(secondDiary.getEnd());
119+
softly.assertThat(diaryResponse.isLiked()).isTrue();
120+
softly.assertThat(diaryResponse.likeCount()).isEqualTo(1);
121+
softly.assertThat(content.getLast().likeCount()).isEqualTo(0);
122+
softly.assertThat(content.getLast().isLiked()).isFalse();
118123
softly.assertThat(content.getFirst().diaryId() > content.getLast().diaryId()).isTrue();
119124
});
120125
}
@@ -196,7 +201,7 @@ void getMyDiariesByPeriod() throws Exception {
196201
.getContentAsString();
197202

198203
// then
199-
List<MyDiaryResponseDto> content = objectMapper.readValue(response, new TypeReference<>() {});
204+
List<CalendarDiaryResponseDto> content = objectMapper.readValue(response, new TypeReference<>() {});
200205

201206
assertSoftly(softly -> {
202207
softly.assertThat(content.getFirst().diaryId() > content.getLast().diaryId()).isTrue();

0 commit comments

Comments
 (0)