Skip to content

Commit 3c85398

Browse files
committed
[Feat]: 게시글 수정 기능 구현
1 parent 3224da2 commit 3c85398

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

back/src/main/java/com/back/domain/post/controller/PostController.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ public ResponseEntity<List<PostResponse>> getPosts() {
4242
public ResponseEntity<PostResponse> getPost(@PathVariable Long postId) {
4343
return ResponseEntity.ok(postService.getPost(postId));
4444
}
45+
46+
@PutMapping("/{postId}")
47+
public ResponseEntity<PostResponse> updatePost(
48+
@PathVariable Long postId,
49+
@RequestBody @Valid PostRequest request) {
50+
Long userId = 1L; // fixme 임시 사용자 ID
51+
return ResponseEntity.ok(postService.updatePost(userId, postId, request));
52+
}
4553
}

back/src/main/java/com/back/domain/post/entity/Post.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@ public class Post extends BaseEntity {
5252
public void assignUser(User user) {
5353
this.user = user;
5454
}
55+
56+
public void updatePost(String title, String content, PostCategory category) {
57+
this.title = title;
58+
this.content = content;
59+
this.category = category;
60+
}
5561
}

back/src/main/java/com/back/domain/post/service/PostService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,18 @@ public List<PostResponse> getPosts() {
5454

5555
return PostMapper.toResponseList(posts);
5656
}
57+
58+
@Transactional
59+
public PostResponse updatePost(Long userId, Long postId, PostRequest request) {
60+
return postRepository.findById(postId)
61+
.map(post -> {
62+
if (!post.getUser().getId().equals(userId)) {
63+
throw new ApiException(ErrorCode.UNAUTHORIZED_USER);
64+
}
65+
post.updatePost(request.title(), request.content(), request.category());
66+
Post updatedPost = postRepository.save(post);
67+
return PostMapper.toResponse(updatedPost);
68+
})
69+
.orElseThrow(() -> new ApiException(ErrorCode.POST_NOT_FOUND));
70+
}
5771
}

0 commit comments

Comments
 (0)