Skip to content

Commit 6d4fa8f

Browse files
committed
[Feat]: 게시글 삭제 기능 구현
1 parent 3c85398 commit 6d4fa8f

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,11 @@ public ResponseEntity<PostResponse> updatePost(
5050
Long userId = 1L; // fixme 임시 사용자 ID
5151
return ResponseEntity.ok(postService.updatePost(userId, postId, request));
5252
}
53+
54+
@DeleteMapping("/{postId}")
55+
public ResponseEntity<Void> deletePost(@PathVariable Long postId) {
56+
Long userId = 1L; // fixme 임시 사용자 ID
57+
postService.deletePost(userId, postId);
58+
return ResponseEntity.ok().build();
59+
}
5360
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import com.back.domain.post.enums.PostCategory;
44
import com.back.domain.user.entity.User;
55
import com.back.global.baseentity.BaseEntity;
6+
import com.back.global.exception.ApiException;
7+
import com.back.global.exception.ErrorCode;
68
import jakarta.persistence.*;
79
import lombok.*;
810
import org.hibernate.annotations.ColumnDefault;
11+
import org.hibernate.service.spi.ServiceException;
912
import org.springframework.data.annotation.LastModifiedDate;
1013

1114
import java.time.LocalDateTime;
@@ -58,4 +61,9 @@ public void updatePost(String title, String content, PostCategory category) {
5861
this.content = content;
5962
this.category = category;
6063
}
64+
65+
public void checkUser(User targetUser) {
66+
if (!user.equals(targetUser))
67+
throw new ApiException(ErrorCode.UNAUTHORIZED_USER);
68+
}
6169
}

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,28 @@ public List<PostResponse> getPosts() {
5757

5858
@Transactional
5959
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-
})
60+
Post post = validatePostOwnership(userId, postId);
61+
62+
post.updatePost(request.title(), request.content(), request.category());
63+
64+
return PostMapper.toResponse(post);
65+
}
66+
67+
@Transactional
68+
public void deletePost(Long userId, Long postId) {
69+
Post post = validatePostOwnership(userId, postId);
70+
postRepository.delete(post);
71+
}
72+
73+
private Post validatePostOwnership(Long userId, Long postId) {
74+
Post post = postRepository.findById(postId)
6975
.orElseThrow(() -> new ApiException(ErrorCode.POST_NOT_FOUND));
76+
77+
User requestUser = userRepository.findById(userId)
78+
.orElseThrow(() -> new ApiException(ErrorCode.USER_NOT_FOUND));
79+
80+
post.checkUser(requestUser);
81+
82+
return post;
7083
}
7184
}

0 commit comments

Comments
 (0)