1616import com .example .log4u .domain .diary .VisibilityType ;
1717import com .example .log4u .domain .diary .dto .DiaryRequestDto ;
1818import com .example .log4u .domain .diary .dto .DiaryResponseDto ;
19+ import com .example .log4u .domain .diary .dto .DiaryWithAuthorDto ;
1920import com .example .log4u .domain .diary .entity .Diary ;
2021import com .example .log4u .domain .diary .exception .NotFoundDiaryException ;
2122import com .example .log4u .domain .diary .exception .OwnerAccessDeniedException ;
@@ -54,27 +55,27 @@ public Slice<DiaryResponseDto> searchDiariesByCursor(
5455 Long cursorId ,
5556 int size
5657 ) {
57- Slice <Diary > diaries = diaryRepository .searchDiariesByCursor (
58+ Slice <DiaryWithAuthorDto > diaries = diaryRepository .searchDiariesByCursor (
5859 keyword ,
5960 List .of (VisibilityType .PUBLIC ),
6061 sort ,
6162 cursorId != null ? cursorId : Long .MAX_VALUE ,
6263 PageRequest .of (0 , size )
6364 );
64- return mapToDtoSlice (diaries );
65+ return this . mapDiaryWithAuthorToDtoSlice (diaries );
6566 }
6667
6768 // 다이어리 목록 (프로필 페이지)
6869 @ Transactional (readOnly = true )
6970 public Slice <DiaryResponseDto > getDiaryResponseDtoSlice (Long userId , Long targetUserId , Long cursorId , int size ) {
7071 List <VisibilityType > visibilities = determineAccessibleVisibilities (userId , targetUserId );
71- Slice <Diary > diaries = diaryRepository .findByUserIdAndVisibilityInAndCursorId (
72+ Slice <DiaryWithAuthorDto > diaries = diaryRepository .findByUserIdAndVisibilityInAndCursorId (
7273 targetUserId ,
7374 visibilities ,
7475 cursorId != null ? cursorId : Long .MAX_VALUE ,
7576 PageRequest .of (0 , size )
7677 );
77- return mapToDtoSlice (diaries );
78+ return this . mapDiaryWithAuthorToDtoSlice (diaries );
7879 }
7980
8081 // 다이어리 수정
@@ -90,25 +91,93 @@ public void deleteDiary(Diary diary) {
9091 diaryRepository .delete (diary );
9192 }
9293
94+ @ Transactional (readOnly = true )
95+ public PageResponse <DiaryResponseDto > getMyDiariesByCursor (Long userId , VisibilityType visibilityType ,
96+ Long cursorId , int size ) {
97+ List <VisibilityType > visibilities =
98+ visibilityType == null ? List .of (VisibilityType .PUBLIC , VisibilityType .PRIVATE , VisibilityType .FOLLOWER ) :
99+ List .of (visibilityType );
100+
101+ Slice <DiaryWithAuthorDto > diaries = diaryRepository .findByUserIdAndVisibilityInAndCursorId (
102+ userId ,
103+ visibilities ,
104+ cursorId != null ? cursorId : Long .MAX_VALUE ,
105+ PageRequest .of (0 , size )
106+ );
107+
108+ Slice <DiaryResponseDto > dtoSlice = this .mapDiaryWithAuthorToDtoSlice (diaries );
109+
110+ Long nextCursor = !dtoSlice .isEmpty () ? dtoSlice .getContent ().getLast ().diaryId () : null ;
111+
112+ return PageResponse .of (dtoSlice , nextCursor );
113+ }
114+
115+ @ Transactional (readOnly = true )
116+ public PageResponse <DiaryResponseDto > getLikeDiariesByCursor (Long userId , Long targetUserId , Long cursorId ,
117+ int size ) {
118+ List <VisibilityType > visibilities = determineAccessibleVisibilities (userId , targetUserId );
119+
120+ Slice <Diary > diaries = diaryRepository .getLikeDiarySliceByUserId (
121+ targetUserId ,
122+ visibilities ,
123+ cursorId != null ? cursorId : Long .MAX_VALUE ,
124+ PageRequest .of (0 , size )
125+ );
126+
127+ Slice <DiaryResponseDto > dtoSlice = this .mapDiarySliceToDtoSlice (diaries );
128+
129+ Long nextCursor = !dtoSlice .isEmpty () ? dtoSlice .getContent ().getLast ().diaryId () : null ;
130+
131+ return PageResponse .of (dtoSlice , nextCursor );
132+ }
133+
93134 private Diary findDiaryOrThrow (Long diaryId ) {
94135 return diaryRepository .findById (diaryId )
95136 .orElseThrow (NotFoundDiaryException ::new );
96137 }
97138
98139 // Page용 매핑 메서드
99140 private Page <DiaryResponseDto > mapToDtoPage (Page <Diary > page ) {
100- List <DiaryResponseDto > content = getDiaryResponsesWithMediaAndHashtags (page .getContent ());
141+ List <DiaryResponseDto > content = getDiaryResponse (page .getContent ());
101142 return new PageImpl <>(content , page .getPageable (), page .getTotalElements ());
102143 }
103144
104145 // Slice용 매핑 메서드
105- private Slice <DiaryResponseDto > mapToDtoSlice (Slice <Diary > slice ) {
106- List <DiaryResponseDto > content = getDiaryResponsesWithMediaAndHashtags (slice .getContent ());
146+ private Slice <DiaryResponseDto > mapDiarySliceToDtoSlice (Slice <Diary > slice ) {
147+ List <DiaryResponseDto > content = getDiaryResponse (slice .getContent ());
148+ return new SliceImpl <>(content , slice .getPageable (), slice .hasNext ());
149+ }
150+
151+ // DiaryWithAuthor Slice 매핑 메서드
152+ private Slice <DiaryResponseDto > mapDiaryWithAuthorToDtoSlice (Slice <DiaryWithAuthorDto > slice ) {
153+ List <DiaryResponseDto > content = getDiaryResponseWithAuthor (slice .getContent ());
107154 return new SliceImpl <>(content , slice .getPageable (), slice .hasNext ());
108155 }
109156
157+ // 다이어리 + 작성자 + 미디어 + 해시태그 같이 반환
158+ private List <DiaryResponseDto > getDiaryResponseWithAuthor (List <DiaryWithAuthorDto > diaryWithAuthorDtoList ) {
159+ if (diaryWithAuthorDtoList .isEmpty ()) {
160+ return List .of ();
161+ }
162+
163+ List <Long > diaryIds = diaryWithAuthorDtoList .stream ()
164+ .map (dto -> dto .diary ().getDiaryId ())
165+ .toList ();
166+
167+ Map <Long , List <Media >> mediaMap = mediaService .getMediaMapByDiaryIds (diaryIds );
168+ Map <Long , List <String >> hashtagMap = hashtagService .getHashtagMapByDiaryIds (diaryIds );
169+
170+ return diaryWithAuthorDtoList .stream ()
171+ .map (dto -> DiaryResponseDto .of (
172+ dto ,
173+ mediaMap .getOrDefault (dto .diary ().getDiaryId (), List .of ()),
174+ hashtagMap .getOrDefault (dto .diary ().getDiaryId (), List .of ())
175+ ))
176+ .toList ();
177+ }
178+
110179 // 다이어리 + 미디어 + 해시태그 같이 반환
111- private List <DiaryResponseDto > getDiaryResponsesWithMediaAndHashtags (List <Diary > diaries ) {
180+ private List <DiaryResponseDto > getDiaryResponse (List <Diary > diaries ) {
112181 if (diaries .isEmpty ()) {
113182 return List .of ();
114183 }
@@ -204,44 +273,4 @@ public void checkDiaryExists(Long diaryId) {
204273 throw new NotFoundDiaryException ();
205274 }
206275 }
207-
208- @ Transactional (readOnly = true )
209- public PageResponse <DiaryResponseDto > getMyDiariesByCursor (Long userId , VisibilityType visibilityType ,
210- Long cursorId , int size ) {
211- List <VisibilityType > visibilities =
212- visibilityType == null ? List .of (VisibilityType .PUBLIC , VisibilityType .PRIVATE , VisibilityType .FOLLOWER ) :
213- List .of (visibilityType );
214-
215- Slice <Diary > diaries = diaryRepository .findByUserIdAndVisibilityInAndCursorId (
216- userId ,
217- visibilities ,
218- cursorId != null ? cursorId : Long .MAX_VALUE ,
219- PageRequest .of (0 , size )
220- );
221-
222- Slice <DiaryResponseDto > dtoSlice = mapToDtoSlice (diaries );
223-
224- Long nextCursor = !dtoSlice .isEmpty () ? dtoSlice .getContent ().getLast ().diaryId () : null ;
225-
226- return PageResponse .of (dtoSlice , nextCursor );
227- }
228-
229- @ Transactional (readOnly = true )
230- public PageResponse <DiaryResponseDto > getLikeDiariesByCursor (Long userId , Long targetUserId , Long cursorId ,
231- int size ) {
232- List <VisibilityType > visibilities = determineAccessibleVisibilities (userId , targetUserId );
233-
234- Slice <Diary > diaries = diaryRepository .getLikeDiarySliceByUserId (
235- targetUserId ,
236- visibilities ,
237- cursorId != null ? cursorId : Long .MAX_VALUE ,
238- PageRequest .of (0 , size )
239- );
240-
241- Slice <DiaryResponseDto > dtoSlice = mapToDtoSlice (diaries );
242-
243- Long nextCursor = !dtoSlice .isEmpty () ? dtoSlice .getContent ().getLast ().diaryId () : null ;
244-
245- return PageResponse .of (dtoSlice , nextCursor );
246- }
247276}
0 commit comments