Skip to content

Commit 71fd08a

Browse files
Merge pull request #121 from pknu-wap/feature/jiseob/#120
refactor : 마이페이지 탭 별로 조회 기능
2 parents 6112d95 + f738775 commit 71fd08a

6 files changed

Lines changed: 91 additions & 25 deletions

File tree

src/main/java/com/example/trace/post/dto/cursor/PostCursorRequest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.example.trace.post.domain.PostType;
44
import com.example.trace.post.domain.cursor.SearchType;
5+
import com.example.trace.user.dto.MyPageTab;
56
import io.swagger.v3.oas.annotations.media.Schema;
6-
import jakarta.validation.constraints.Size;
77
import lombok.AllArgsConstructor;
88
import lombok.Builder;
99
import lombok.Data;
@@ -24,6 +24,8 @@ public class PostCursorRequest {
2424
private Integer size = 10; // 기본 페이지 크기
2525
@Schema(description = "게시글 타입", example = "MISSION")
2626
private PostType postType; // 필터링을 위한 게시글 타입 (선택적)
27+
@Schema(description = "마이페이지 탭", example = "WRITTEN_POSTS,COMMENTED_POSTS,REACTED_POSTS")
28+
private MyPageTab myPageTab; // 마이페이지에서 어떤 탭을 볼지에 대한 페이지 탭 타입
2729

2830
// 검색을 위한 필드
2931
@Schema(description = "검색 키워드", example = "")

src/main/java/com/example/trace/post/repository/PostRepositoryCustomImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private BooleanExpression postTypeEq(PostType postType) {
4141
}
4242

4343
StringExpression imageUrlExpr = Expressions.cases()
44-
.when(post.images.isEmpty()).then("")
44+
.when(post.images.isEmpty()).then(Expressions.stringTemplate("null"))
4545
.otherwise(
4646
JPAExpressions
4747
.select(postImage.imageUrl)

src/main/java/com/example/trace/post/service/PostService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ public interface PostService {
2929

3030
CursorResponse<PostFeedDto> getUserEmotedPostsWithCursor(PostCursorRequest request, String providerId);
3131

32+
CursorResponse<PostFeedDto> getMyPagePostsWithCursor(PostCursorRequest request, String providerId);
33+
3234
}

src/main/java/com/example/trace/post/service/PostServiceImpl.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,65 @@ public CursorResponse<PostFeedDto> getUserEmotedPostsWithCursor(PostCursorReques
423423
.build();
424424
}
425425

426+
@Override
427+
@Transactional(readOnly = true)
428+
public CursorResponse<PostFeedDto> getMyPagePostsWithCursor(PostCursorRequest request, String providerId) {
429+
int size = request.getSize() != null ? request.getSize() : 10;
430+
List<PostFeedDto> posts;
431+
432+
switch (request.getMyPageTab()) {
433+
case WRITTEN_POSTS:
434+
posts = postRepository.findUserPosts(
435+
providerId,
436+
request.getCursorDateTime(),
437+
request.getCursorId(),
438+
size + 1
439+
);
440+
break;
441+
case COMMENTED_POSTS:
442+
posts = postRepository.findUserCommentedPosts(
443+
providerId,
444+
request.getCursorDateTime(),
445+
request.getCursorId(),
446+
size + 1
447+
);
448+
break;
449+
case REACTED_POSTS:
450+
posts = postRepository.findUserEmotedPosts(
451+
providerId,
452+
request.getCursorDateTime(),
453+
request.getCursorId(),
454+
size + 1
455+
);
456+
break;
457+
default:
458+
throw new IllegalArgumentException("Invalid MyPageTab type");
459+
}
460+
461+
// 다음 페이지 여부 확인
462+
boolean hasNext = false;
463+
if (posts.size() > size) {
464+
hasNext = true;
465+
posts = posts.subList(0, size);
466+
}
467+
468+
// 커서 메타데이터 생성
469+
CursorResponse.CursorMeta nextCursor = null;
470+
if (!posts.isEmpty() && hasNext) {
471+
PostFeedDto lastPost = posts.get(posts.size() - 1);
472+
nextCursor = CursorResponse.CursorMeta.builder()
473+
.dateTime(lastPost.getCreatedAt())
474+
.id(lastPost.getPostId())
475+
.build();
476+
}
477+
478+
// 응답 생성
479+
return CursorResponse.<PostFeedDto>builder()
480+
.content(posts)
481+
.hasNext(hasNext)
482+
.cursor(nextCursor)
483+
.build();
484+
}
485+
486+
426487
}

src/main/java/com/example/trace/user/UserController.java

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -116,33 +116,14 @@ public ResponseEntity<?> deleteUser(HttpServletRequest request) {
116116
return ResponseEntity.ok().build();
117117
}
118118

119-
@PostMapping("/myPost")
120-
@Operation(summary = "내 게시글 커서 기반 페이징 조회", description = "커서 기반 페이징으로 내 게시글을 조회합니다.")
121-
public ResponseEntity<CursorResponse<PostFeedDto>> getMyPosts(
122-
@RequestBody PostCursorRequest request,
123-
@AuthenticationPrincipal PrincipalDetails principalDetails) {
124-
String providerId = principalDetails.getUser().getProviderId();
125-
CursorResponse<PostFeedDto> response = postService.getMyPostsWithCursor(request, providerId);
126-
return ResponseEntity.ok(response);
127-
}
128-
129-
@PostMapping("/myCommentedPosts")
130-
@Operation(summary = "내가 댓글 단 게시글 커서 기반 페이징 조회", description = "커서 기반 페이징으로 내가 댓글을 단 게시글을 조회합니다.")
131-
public ResponseEntity<CursorResponse<PostFeedDto>> getMyCommentedPosts(
132-
@RequestBody PostCursorRequest request,
133-
@AuthenticationPrincipal PrincipalDetails principalDetails) {
134-
String providerId = principalDetails.getUser().getProviderId();
135-
CursorResponse<PostFeedDto> response = postService.getUserCommentedPostsWithCursor(request, providerId);
136-
return ResponseEntity.ok(response);
137-
}
138119

139-
@PostMapping("/myEmotionPosts")
140-
@Operation(summary = "내가 감정표현한 게시글 커서 기반 페이징 조회", description = "커서 기반 페이징으로 내가 감정표현을 한 게시글을 조회합니다.")
141-
public ResponseEntity<CursorResponse<PostFeedDto>> getMyEmotedPosts(
120+
@PostMapping("/myPosts")
121+
@Operation(summary = "마이페이지 게시글 조회", description = "탭 별로 마이페이지 게시글을 조회합니다.")
122+
public ResponseEntity<CursorResponse<PostFeedDto>> getMyPagePosts(
142123
@RequestBody PostCursorRequest request,
143124
@AuthenticationPrincipal PrincipalDetails principalDetails) {
144125
String providerId = principalDetails.getUser().getProviderId();
145-
CursorResponse<PostFeedDto> response = postService.getUserEmotedPostsWithCursor(request, providerId);
126+
CursorResponse<PostFeedDto> response = postService.getMyPagePostsWithCursor(request, providerId);
146127
return ResponseEntity.ok(response);
147128
}
148129

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.trace.user.dto;
2+
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
6+
@Getter
7+
@RequiredArgsConstructor
8+
public enum MyPageTab {
9+
WRITTEN_POSTS("나의 흔적"),
10+
COMMENTED_POSTS("댓글단 글"),
11+
REACTED_POSTS("반응한 글")
12+
;
13+
14+
private String description;
15+
16+
MyPageTab(String description){
17+
this.description = description;
18+
}
19+
20+
}

0 commit comments

Comments
 (0)