Skip to content

Commit ff008b5

Browse files
authored
[feat] 내 활동: 내가 작성한 댓글 목록 조회 기능 구현 #78 (#85)
* refactor: 컨트롤러 API 경로에 /api 접두사 추가 * refactor: History 패키지 및 클래스 명칭 변경 * feat: 마이페이지 댓글 히스토리 DTO 추가 * feat: 마이페이지 댓글 목록 페이지네이션 DTO 추가 * feat: 마이페이지 댓글 이동 응답 DTO 추가 * feat: 마이페이지 댓글 히스토리 Repository 구현 - `MyHistoryCommentRepository`에 사용자 댓글 조회 기능 추가 - `findMyCommentsFirstPage()`: 사용자 ID를 기반으로 댓글 목록의 첫 페이지를 조회 - `findMyCommentsAfter()`: 커서(lastCreatedAt, lastId)를 사용하여 다음 페이지의 댓글 목록을 효율적으로 조회 - `findByIdAndUserId()`: 특정 사용자의 특정 댓글을 조회하는 기능 추가 * refactor: MyHistoryService로 클래스명 및 패키지명 변경 * feat: 마이페이지 댓글 히스토리 기능 추가 - MyHistoryService에 사용자가 작성한 댓글 목록을 조회하는 getMyComments() 메서드를 추가 - 커서 기반 페이지네이션을 적용 - 댓글을 클릭하면 해당 댓글이 속한 게시글로 이동할 수 있도록 getPostLinkFromMyComment() 메서드를 구현 - 삭제된 게시글에 대한 예외 처리를 추가 * refactor: MyHistoryController로 클래스명 및 패키지 변경 * feat: 마이페이지 댓글 히스토리 API 추가 * fix typo
1 parent 08f273d commit ff008b5

File tree

12 files changed

+267
-104
lines changed

12 files changed

+267
-104
lines changed

src/main/java/com/back/domain/history/controller/MyHistoryController.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/main/java/com/back/domain/history/service/HistoryService.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/main/java/com/back/domain/mybar/controller/MyBarController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.time.LocalDateTime;
1414

1515
@RestController
16-
@RequestMapping("/me/bar")
16+
@RequestMapping("/api/me/bar")
1717
@RequiredArgsConstructor
1818
@Validated
1919
public class MyBarController {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.back.domain.myhistory.controller;
2+
3+
import com.back.domain.myhistory.dto.MyHistoryCommentGoResponseDto;
4+
import com.back.domain.myhistory.dto.MyHistoryCommentListDto;
5+
import com.back.domain.myhistory.dto.MyHistoryPostListDto;
6+
import com.back.domain.myhistory.service.MyHistoryService;
7+
import com.back.global.rsData.RsData;
8+
import jakarta.validation.constraints.Max;
9+
import jakarta.validation.constraints.Min;
10+
import lombok.RequiredArgsConstructor;
11+
import org.springframework.format.annotation.DateTimeFormat;
12+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
13+
import org.springframework.validation.annotation.Validated;
14+
import org.springframework.web.bind.annotation.*;
15+
16+
import java.time.LocalDateTime;
17+
18+
@RestController
19+
@RequestMapping("/api/me")
20+
@RequiredArgsConstructor
21+
@Validated
22+
public class MyHistoryController {
23+
24+
private final MyHistoryService myHistoryService;
25+
26+
@GetMapping("/posts")
27+
public RsData<MyHistoryPostListDto> getMyPosts(
28+
@AuthenticationPrincipal(expression = "id") Long userId,
29+
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime lastCreatedAt,
30+
@RequestParam(required = false) Long lastId,
31+
@RequestParam(defaultValue = "20") @Min(1) @Max(100) int limit
32+
) {
33+
MyHistoryPostListDto body = myHistoryService.getMyPosts(userId, lastCreatedAt, lastId, limit);
34+
return RsData.successOf(body);
35+
}
36+
37+
@GetMapping("/comments")
38+
public RsData<MyHistoryCommentListDto> getMyComments(
39+
@AuthenticationPrincipal(expression = "id") Long userId,
40+
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime lastCreatedAt,
41+
@RequestParam(required = false) Long lastId,
42+
@RequestParam(defaultValue = "20") @Min(1) @Max(100) int limit
43+
) {
44+
MyHistoryCommentListDto body = myHistoryService.getMyComments(userId, lastCreatedAt, lastId, limit);
45+
return RsData.successOf(body);
46+
}
47+
48+
@GetMapping("/comments/{id}")
49+
public RsData<MyHistoryCommentGoResponseDto> goFromComment(
50+
@AuthenticationPrincipal(expression = "id") Long userId,
51+
@PathVariable("id") Long commentId
52+
) {
53+
var body = myHistoryService.getPostLinkFromMyComment(userId, commentId);
54+
return RsData.successOf(body);
55+
}
56+
}
57+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.back.domain.myhistory.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
@Getter
7+
@AllArgsConstructor
8+
public class MyHistoryCommentGoResponseDto {
9+
private Long postId;
10+
private String postApiUrl;
11+
}
12+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.back.domain.myhistory.dto;
2+
3+
import com.back.domain.post.comment.entity.Comment;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
7+
import java.time.LocalDateTime;
8+
9+
@Getter
10+
@Builder
11+
public class MyHistoryCommentItemDto {
12+
private Long id;
13+
private Long postId;
14+
private String postTitle;
15+
private String content;
16+
private LocalDateTime createdAt;
17+
18+
public static MyHistoryCommentItemDto from(Comment c) {
19+
return MyHistoryCommentItemDto.builder()
20+
.id(c.getId())
21+
.postId(c.getPost().getId())
22+
.postTitle(c.getPost().getTitle())
23+
.content(c.getContent())
24+
.createdAt(c.getCreatedAt())
25+
.build();
26+
}
27+
}
28+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.back.domain.myhistory.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
import java.time.LocalDateTime;
7+
import java.util.List;
8+
9+
@Getter
10+
@AllArgsConstructor
11+
public class MyHistoryCommentListDto {
12+
private List<MyHistoryCommentItemDto> items;
13+
private boolean hasNext;
14+
private LocalDateTime nextCreatedAt;
15+
private Long nextId;
16+
}
17+

src/main/java/com/back/domain/history/dto/HistoryPostItemDto.java renamed to src/main/java/com/back/domain/myhistory/dto/MyHistoryPostItemDto.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
package com.back.domain.history.dto;
1+
package com.back.domain.myhistory.dto;
22

33
import com.back.domain.post.post.entity.Post;
4-
import java.time.LocalDateTime;
54
import lombok.Builder;
65
import lombok.Getter;
76

7+
import java.time.LocalDateTime;
8+
89
@Getter
910
@Builder
10-
public class HistoryPostItemDto {
11+
public class MyHistoryPostItemDto {
1112
private Long id;
1213
private String title;
1314
private String imageUrl;
1415
private LocalDateTime createdAt;
1516
private Integer likeCount;
1617
private Integer commentCount;
1718

18-
public static HistoryPostItemDto from(Post p) {
19-
return HistoryPostItemDto.builder()
19+
public static MyHistoryPostItemDto from(Post p) {
20+
return MyHistoryPostItemDto.builder()
2021
.id(p.getId())
2122
.title(p.getTitle())
2223
.imageUrl(p.getImageUrl())

src/main/java/com/back/domain/history/dto/HistoryPostListDto.java renamed to src/main/java/com/back/domain/myhistory/dto/MyHistoryPostListDto.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.back.domain.history.dto;
1+
package com.back.domain.myhistory.dto;
22

33
import lombok.AllArgsConstructor;
44
import lombok.Getter;
@@ -8,8 +8,8 @@
88

99
@Getter
1010
@AllArgsConstructor
11-
public class HistoryPostListDto {
12-
private List<HistoryPostItemDto> items;
11+
public class MyHistoryPostListDto {
12+
private List<MyHistoryPostItemDto> items;
1313
private boolean hasNext;
1414
private LocalDateTime nextCreatedAt;
1515
private Long nextId;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.back.domain.myhistory.repository;
2+
3+
import com.back.domain.post.comment.entity.Comment;
4+
import org.springframework.data.domain.Pageable;
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.query.Param;
8+
import org.springframework.stereotype.Repository;
9+
10+
import java.time.LocalDateTime;
11+
import java.util.List;
12+
13+
@Repository
14+
public interface MyHistoryCommentRepository extends JpaRepository<Comment, Long> {
15+
16+
@Query("""
17+
select c from Comment c
18+
join fetch c.post p
19+
where c.user.id = :userId
20+
order by c.createdAt desc, c.id desc
21+
""")
22+
List<Comment> findMyCommentsFirstPage(@Param("userId") Long userId,
23+
Pageable pageable);
24+
25+
@Query("""
26+
select c from Comment c
27+
join fetch c.post p
28+
where c.user.id = :userId
29+
and (c.createdAt < :lastCreatedAt or (c.createdAt = :lastCreatedAt and c.id < :lastId))
30+
order by c.createdAt desc, c.id desc
31+
""")
32+
List<Comment> findMyCommentsAfter(@Param("userId") Long userId,
33+
@Param("lastCreatedAt") LocalDateTime lastCreatedAt,
34+
@Param("lastId") Long lastId,
35+
Pageable pageable);
36+
37+
@Query("""
38+
select c from Comment c
39+
join fetch c.post p
40+
where c.id = :id and c.user.id = :userId
41+
""")
42+
Comment findByIdAndUserId(@Param("id") Long id, @Param("userId") Long userId);
43+
}

0 commit comments

Comments
 (0)