File tree Expand file tree Collapse file tree 3 files changed +28
-0
lines changed
back/src/main/java/com/back/domain/post Expand file tree Collapse file tree 3 files changed +28
-0
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments