File tree Expand file tree Collapse file tree 6 files changed +70
-0
lines changed
src/main/java/com/example/log4u/domain Expand file tree Collapse file tree 6 files changed +70
-0
lines changed Original file line number Diff line number Diff line change 11package com .example .log4u .domain .diary .controller ;
22
3+ import java .util .List ;
4+
35import org .springframework .http .HttpStatus ;
46import org .springframework .http .ResponseEntity ;
57import org .springframework .security .core .annotation .AuthenticationPrincipal ;
1820import com .example .log4u .domain .diary .SortType ;
1921import com .example .log4u .domain .diary .dto .DiaryRequestDto ;
2022import com .example .log4u .domain .diary .dto .DiaryResponseDto ;
23+ import com .example .log4u .domain .diary .dto .PopularDiaryDto ;
2124import com .example .log4u .domain .diary .facade .DiaryFacade ;
2225
2326import jakarta .validation .Valid ;
@@ -106,4 +109,10 @@ public ResponseEntity<Void> deleteDiary(
106109 diaryFacade .deleteDiary (customOAuth2User .getUserId (), diaryId );
107110 return ResponseEntity .status (HttpStatus .NO_CONTENT ).build ();
108111 }
112+
113+ @ GetMapping ("/popular" )
114+ public ResponseEntity <List <PopularDiaryDto >> getPopularDiaries () {
115+ List <PopularDiaryDto > popularDiaries = diaryFacade .getPopularDiaries (10 );
116+ return ResponseEntity .ok (popularDiaries );
117+ }
109118}
Original file line number Diff line number Diff line change 1+ package com .example .log4u .domain .diary .dto ;
2+
3+ import com .example .log4u .domain .diary .entity .Diary ;
4+
5+ import lombok .Builder ;
6+
7+ @ Builder
8+ public record PopularDiaryDto (
9+ Long diaryId ,
10+ String title
11+ ) {
12+ public static PopularDiaryDto of (Diary diary ) {
13+ return PopularDiaryDto .builder ()
14+ .diaryId (diary .getDiaryId ())
15+ .title (diary .getTitle ())
16+ .build ();
17+ }
18+ }
Original file line number Diff line number Diff line change 1010import com .example .log4u .domain .diary .SortType ;
1111import com .example .log4u .domain .diary .dto .DiaryRequestDto ;
1212import com .example .log4u .domain .diary .dto .DiaryResponseDto ;
13+ import com .example .log4u .domain .diary .dto .PopularDiaryDto ;
1314import com .example .log4u .domain .diary .entity .Diary ;
1415import com .example .log4u .domain .diary .service .DiaryService ;
1516import com .example .log4u .domain .hashtag .service .HashtagService ;
@@ -139,4 +140,16 @@ public PageResponse<DiaryResponseDto> searchDiariesByCursor(
139140 Long nextCursor = !dtoSlice .isEmpty () ? dtoSlice .getContent ().getLast ().diaryId () : null ;
140141 return PageResponse .of (dtoSlice , nextCursor );
141142 }
143+
144+ @ Transactional (readOnly = true )
145+ public List <PopularDiaryDto > getPopularDiaries (int limit ) {
146+ List <Diary > popularDiaries = diaryService .getTop10Diaries ();
147+
148+ if (popularDiaries .isEmpty ()) {
149+ return List .of ();
150+ }
151+ return popularDiaries .stream ()
152+ .map (PopularDiaryDto ::of )
153+ .toList ();
154+ }
142155}
Original file line number Diff line number Diff line change 11package com .example .log4u .domain .diary .repository ;
22
3+ import java .util .List ;
4+
35import org .springframework .data .jpa .repository .JpaRepository ;
46
7+ import com .example .log4u .domain .diary .VisibilityType ;
58import com .example .log4u .domain .diary .entity .Diary ;
69
710public interface DiaryRepository extends JpaRepository <Diary , Long >, CustomDiaryRepository {
11+ List <Diary > findTop10ByVisibilityOrderByLikeCountDesc (VisibilityType visibility );
812}
Original file line number Diff line number Diff line change @@ -273,4 +273,9 @@ public void checkDiaryExists(Long diaryId) {
273273 throw new NotFoundDiaryException ();
274274 }
275275 }
276+
277+ @ Transactional (readOnly = true )
278+ public List <Diary > getTop10Diaries () {
279+ return diaryRepository .findTop10ByVisibilityOrderByLikeCountDesc (VisibilityType .PUBLIC );
280+ }
276281}
Original file line number Diff line number Diff line change @@ -173,4 +173,25 @@ private void validateMediaLimit(List<MediaRequestDto> mediaList) {
173173 throw new MediaLimitExceededException ();
174174 }
175175 }
176+
177+ private boolean isMediaListUnchanged (List <Media > existingMediaList , List <MediaRequestDto > newMediaList ) {
178+ // 개수가 다르면 변경된 것
179+ if (existingMediaList .size () != newMediaList .size ()) {
180+ return false ;
181+ }
182+
183+ Map <Long , Integer > existingMediaMap = existingMediaList .stream ()
184+ .collect (Collectors .toMap (Media ::getMediaId , Media ::getOrderIndex ));
185+
186+ // 모든 새 미이더가 기존 미디어와 ID 및 순서가 동일한지 확인
187+ for (MediaRequestDto newMedia : newMediaList ) {
188+ Integer existingOrder = existingMediaMap .get (newMedia .mediaId ());
189+
190+ // ID가 없거나 순서가 다르면 변경된 것
191+ if (existingOrder == null || !existingOrder .equals (newMedia .orderIndex ())) {
192+ return false ;
193+ }
194+ }
195+ return true ;
196+ }
176197}
You can’t perform that action at this time.
0 commit comments