Skip to content

Commit bd517c0

Browse files
authored
[feat] 내활동: 내가 쓴 글 목록에서 클릭시 해당 게시글로 이동 구현 #86 (#88)
* feat: 마이페이지 게시글 이동 응답 DTO 추가 * eat: MyHistoryPostRepository에 게시글 조회 메서드 추가 - findByIdAndUserId(): 게시글 ID와 사용자 ID를 모두 사용하여 특정 게시글을 조회하는 메서드를 추가 * feat: 마이페이지 게시글 이동 서비스 로직 추가 - MyHistoryService에 getPostLinkFromMyPost() 메서드를 추가하여 특정 게시글로 이동하는 기능을 구현 * feat: 마이페이지 게시글 이동 API 추가
1 parent e23176a commit bd517c0

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ public RsData<MyHistoryPostListDto> getMyPosts(
3333
MyHistoryPostListDto body = myHistoryService.getMyPosts(userId, lastCreatedAt, lastId, limit);
3434
return RsData.successOf(body);
3535
}
36+
37+
@GetMapping("/posts/{id}")
38+
public RsData<com.back.domain.myhistory.dto.MyHistoryPostGoResponseDto> goFromPost(
39+
@AuthenticationPrincipal(expression = "id") Long userId,
40+
@PathVariable("id") Long postId
41+
) {
42+
var body = myHistoryService.getPostLinkFromMyPost(userId, postId);
43+
return RsData.successOf(body);
44+
}
3645

3746
@GetMapping("/comments")
3847
public RsData<MyHistoryCommentListDto> getMyComments(
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 MyHistoryPostGoResponseDto {
9+
private Long postId;
10+
private String postApiUrl;
11+
}
12+

src/main/java/com/back/domain/myhistory/repository/MyHistoryPostRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ List<Post> findMyPostsAfter(@Param("userId") Long userId,
3636
@Param("lastCreatedAt") LocalDateTime lastCreatedAt,
3737
@Param("lastId") Long lastId,
3838
Pageable pageable);
39+
40+
@Query("""
41+
select p from Post p
42+
where p.id = :id and p.user.id = :userId
43+
""")
44+
Post findByIdAndUserId(@Param("id") Long id, @Param("userId") Long userId);
3945
}

src/main/java/com/back/domain/myhistory/service/MyHistoryService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,17 @@ public MyHistoryCommentGoResponseDto getPostLinkFromMyComment(Long userId, Long
9595
String apiUrl = "/api/posts/" + postId;
9696
return new MyHistoryCommentGoResponseDto(postId, apiUrl);
9797
}
98+
99+
@Transactional(readOnly = true)
100+
public MyHistoryPostGoResponseDto getPostLinkFromMyPost(Long userId, Long postId) {
101+
Post p = myHistoryPostRepository.findByIdAndUserId(postId, userId);
102+
if (p == null) {
103+
throw new ServiceException(404, "게시글을 찾을 수 없습니다.");
104+
}
105+
if (p.getStatus() == PostStatus.DELETED) {
106+
throw new ServiceException(410, "삭제된 게시글입니다.");
107+
}
108+
String apiUrl = "/api/posts/" + p.getId();
109+
return new MyHistoryPostGoResponseDto(p.getId(), apiUrl);
110+
}
98111
}

0 commit comments

Comments
 (0)