Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/main/java/com/back/domain/board/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,35 @@ public ResponseEntity<RsData<PostDetailResponse>> getPost(
response
));
}

// 게시글 수정
@PutMapping("/{postId}")
public ResponseEntity<RsData<PostResponse>> updatePost(
@PathVariable Long postId,
@RequestBody @Valid PostRequest request,
@AuthenticationPrincipal CustomUserDetails user
) {
PostResponse response = postService.updatePost(postId, request, user.getUserId());
return ResponseEntity
.status(HttpStatus.OK)
.body(RsData.success(
"게시글이 수정되었습니다.",
response
));
}

// 게시글 삭제
@DeleteMapping("/{postId}")
public ResponseEntity<RsData<Void>> deletePost(
@PathVariable Long postId,
@AuthenticationPrincipal CustomUserDetails user
) {
postService.deletePost(postId, user.getUserId());
return ResponseEntity
.status(HttpStatus.OK)
.body(RsData.success(
"게시글이 삭제되었습니다.",
null
));
}
}
263 changes: 263 additions & 0 deletions src/main/java/com/back/domain/board/controller/PostControllerDocs.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,267 @@ ResponseEntity<RsData<PageResponse<PostListResponse>>> getPosts(
ResponseEntity<RsData<PostDetailResponse>> getPost(
@PathVariable Long postId
);

@Operation(
summary = "게시글 수정",
description = "로그인한 사용자가 자신의 게시글을 수정합니다."
)
@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "게시글 수정 성공",
content = @Content(
mediaType = "application/json",
examples = @ExampleObject(value = """
{
"success": true,
"code": "SUCCESS_200",
"message": "게시글이 수정되었습니다.",
"data": {
"postId": 101,
"author": {
"id": 5,
"nickname": "홍길동"
},
"title": "수정된 게시글",
"content": "안녕하세요, 수정했습니다!",
"categories": [
{ "id": 1, "name": "공지사항" },
{ "id": 2, "name": "자유게시판" }
],
"createdAt": "2025-09-22T10:30:00",
"updatedAt": "2025-09-22T10:30:00"
}
}
""")
)
),
@ApiResponse(
responseCode = "400",
description = "잘못된 요청 (필드 누락 등)",
content = @Content(
mediaType = "application/json",
examples = @ExampleObject(value = """
{
"success": false,
"code": "COMMON_400",
"message": "잘못된 요청입니다.",
"data": null
}
""")
)
),
@ApiResponse(
responseCode = "401",
description = "인증 실패 (토큰 없음/만료/잘못됨)",
content = @Content(
mediaType = "application/json",
examples = {
@ExampleObject(name = "토큰 없음", value = """
{
"success": false,
"code": "AUTH_001",
"message": "인증이 필요합니다.",
"data": null
}
"""),
@ExampleObject(name = "잘못된 토큰", value = """
{
"success": false,
"code": "AUTH_002",
"message": "유효하지 않은 액세스 토큰입니다.",
"data": null
}
"""),
@ExampleObject(name = "만료된 토큰", value = """
{
"success": false,
"code": "AUTH_004",
"message": "만료된 액세스 토큰입니다.",
"data": null
}
""")
}
)
),
@ApiResponse(
responseCode = "403",
description = "권한 없음 (작성자 아님)",
content = @Content(
mediaType = "application/json",
examples = @ExampleObject(value = """
{
"success": false,
"code": "POST_002",
"message": "게시글 작성자만 수정/삭제할 수 있습니다.",
"data": null
}
""")
)
),
@ApiResponse(
responseCode = "404",
description = "존재하지 않는 게시글 또는 카테고리",
content = @Content(
mediaType = "application/json",
examples = {
@ExampleObject(name = "존재하지 않는 게시글", value = """
{
"success": false,
"code": "POST_001",
"message": "존재하지 않는 게시글입니다.",
"data": null
}
"""),
@ExampleObject(name = "존재하지 않는 카테고리", value = """
{
"success": false,
"code": "POST_003",
"message": "존재하지 않는 카테고리입니다.",
"data": null
}
""")
}
)
),
@ApiResponse(
responseCode = "500",
description = "서버 내부 오류",
content = @Content(
mediaType = "application/json",
examples = @ExampleObject(value = """
{
"success": false,
"code": "COMMON_500",
"message": "서버 오류가 발생했습니다.",
"data": null
}
""")
)
)
})
ResponseEntity<RsData<PostResponse>> updatePost(
@PathVariable Long postId,
@RequestBody PostRequest request,
@AuthenticationPrincipal CustomUserDetails user
);

@Operation(
summary = "게시글 삭제",
description = "로그인한 사용자가 자신의 게시글을 삭제합니다."
)
@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "게시글 삭제 성공",
content = @Content(
mediaType = "application/json",
examples = @ExampleObject(value = """
{
"success": true,
"code": "SUCCESS_200",
"message": "게시글이 삭제되었습니다.",
"data": null
}
""")
)
),
@ApiResponse(
responseCode = "400",
description = "잘못된 요청 (필드 누락 등)",
content = @Content(
mediaType = "application/json",
examples = @ExampleObject(value = """
{
"success": false,
"code": "COMMON_400",
"message": "잘못된 요청입니다.",
"data": null
}
""")
)
),
@ApiResponse(
responseCode = "401",
description = "인증 실패 (토큰 없음/만료/잘못됨)",
content = @Content(
mediaType = "application/json",
examples = {
@ExampleObject(name = "토큰 없음", value = """
{
"success": false,
"code": "AUTH_001",
"message": "인증이 필요합니다.",
"data": null
}
"""),
@ExampleObject(name = "잘못된 토큰", value = """
{
"success": false,
"code": "AUTH_002",
"message": "유효하지 않은 액세스 토큰입니다.",
"data": null
}
"""),
@ExampleObject(name = "만료된 토큰", value = """
{
"success": false,
"code": "AUTH_004",
"message": "만료된 액세스 토큰입니다.",
"data": null
}
""")
}
)
),
@ApiResponse(
responseCode = "403",
description = "권한 없음 (작성자 아님)",
content = @Content(
mediaType = "application/json",
examples = @ExampleObject(value = """
{
"success": false,
"code": "POST_002",
"message": "게시글 작성자만 수정/삭제할 수 있습니다.",
"data": null
}
""")
)
),
@ApiResponse(
responseCode = "404",
description = "존재하지 않는 게시글",
content = @Content(
mediaType = "application/json",
examples = @ExampleObject(value = """
{
"success": false,
"code": "POST_001",
"message": "존재하지 않는 게시글입니다.",
"data": null
}
""")
)
),
@ApiResponse(
responseCode = "500",
description = "서버 내부 오류",
content = @Content(
mediaType = "application/json",
examples = @ExampleObject(value = """
{
"success": false,
"code": "COMMON_500",
"message": "서버 오류가 발생했습니다.",
"data": null
}
""")
)
)
})
ResponseEntity<RsData<Void>> deletePost(
@PathVariable Long postId,
@AuthenticationPrincipal CustomUserDetails user
);
}
6 changes: 6 additions & 0 deletions src/main/java/com/back/domain/board/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public Post(User user, String title, String content) {
}

// -------------------- 비즈니스 메서드 --------------------
// 게시글 업데이트
public void update(String title, String content) {
this.title = title;
this.content = content;
}

// 카테고리 업데이트
public void updateCategories(List<PostCategory> categories) {
this.postCategoryMappings.clear();
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/back/domain/board/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,55 @@ public PostDetailResponse getPost(Long postId) {
// 응답 반환
return PostDetailResponse.from(post);
}

/**
* 게시글 수정 서비스
* 1. Post 조회
* 2. 작성자 검증
* 3. Post 업데이트 (제목, 내용, 카테고리)
* 4. PostResponse 반환
*/
public PostResponse updatePost(Long postId, PostRequest request, Long userId) {
// Post 조회
Post post = postRepository.findById(postId)
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND));

// 작성자 검증
if (!post.getUser().getId().equals(userId)) {
throw new CustomException(ErrorCode.POST_NO_PERMISSION);
}

// Post 업데이트
post.update(request.title(), request.content());

// Category 매핑 업데이트
List<PostCategory> categories = postCategoryRepository.findAllById(request.categoryIds());
if (categories.size() != request.categoryIds().size()) {
throw new CustomException(ErrorCode.CATEGORY_NOT_FOUND);
}
post.updateCategories(categories);

// 응답 반환
return PostResponse.from(post);
}

/**
* 게시글 삭제 서비스
* 1. Post 조회
* 2. 작성자 검증
* 3. Post 삭제
*/
public void deletePost(Long postId, Long userId) {
// Post 조회
Post post = postRepository.findById(postId)
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND));

// 작성자 검증
if (!post.getUser().getId().equals(userId)) {
throw new CustomException(ErrorCode.POST_NO_PERMISSION);
}

// Post 삭제
postRepository.delete(post);
}
}
Loading