Skip to content

Commit e94a7aa

Browse files
authored
Merge pull request #73 from Hsu-Connect/feat/#72
[#72] 게시글 수정 API
2 parents 496dd64 + 856c502 commit e94a7aa

File tree

8 files changed

+89
-2
lines changed

8 files changed

+89
-2
lines changed

src/main/java/hansung/hansung_connect/common/exception/code/status/ErrorStatus.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public enum ErrorStatus implements BaseErrorCode {
2626
// 게시글 관련 응답
2727
POST_NOT_FOUND(HttpStatus.BAD_REQUEST, "POST4001", "게시글을 찾을 수 없습니다."),
2828
POST_QUERY_TYPE_EXCEPTION(HttpStatus.BAD_REQUEST, "POST4002", "게시글 조회 타입이 잘못되었습니다."),
29-
POST_LIST_NOT_FOUND(HttpStatus.INTERNAL_SERVER_ERROR, "POST5001", "게시글 리스트가 비어있습니다."),
30-
INVALID_PAGE_FOR_POPULAR(HttpStatus.BAD_REQUEST, "POST4003", "인기 게시글은 0번 페이지만 조회 가능합니다."),
29+
POST_LIST_NOT_FOUND(HttpStatus.INTERNAL_SERVER_ERROR, "POST4003", "게시글 리스트가 비어있습니다."),
30+
INVALID_PAGE_FOR_POPULAR(HttpStatus.BAD_REQUEST, "POST4004", "인기 게시글은 0번 페이지만 조회 가능합니다."),
31+
POST_FORBIDDEN(HttpStatus.INTERNAL_SERVER_ERROR, "POST4005", "해당 게시글의 수정이 허용된 사용자가 아닙니다."),
3132

3233
// 커리어 관련 응답
3334
CAREER_COMPANY_REQUIRED(HttpStatus.BAD_REQUEST, "CAREER4001", "회사명은 필수입니다."),

src/main/java/hansung/hansung_connect/domain/post/controller/PostController.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.swagger.v3.oas.annotations.tags.Tag;
1212
import lombok.RequiredArgsConstructor;
1313
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.PatchMapping;
1415
import org.springframework.web.bind.annotation.PathVariable;
1516
import org.springframework.web.bind.annotation.PostMapping;
1617
import org.springframework.web.bind.annotation.RequestBody;
@@ -113,4 +114,23 @@ public ApiResponse<PostResponseDto.PostTitleListResponse> getPopularPosts() {
113114
public ApiResponse<PostResponseDto.PostSummaryListResponse> getLatestPromotionPosts() {
114115
return ApiResponse.onSuccess(postQueryService.getLatestPromotionPosts());
115116
}
117+
118+
@PatchMapping("/{postId}")
119+
@Operation(
120+
summary = "게시글 수정",
121+
description = """
122+
게시글을 수정하는 API입니다. 제목, 내용 중 수정된 부분만 전달
123+
-title: 게시글 제목, null 허용
124+
-body: 게시글 내용 , null 허용
125+
"""
126+
)
127+
public ApiResponse<PostResponseDto.PostUpdateResponse> updatePost(
128+
@PathVariable("postId") Long postId,
129+
@RequestBody PostRequestDto.PostUpdateRequest request
130+
) {
131+
132+
Long userId = 1L;
133+
134+
return ApiResponse.onSuccess(postCommandService.updatePost(userId, postId, request));
135+
}
116136
}

src/main/java/hansung/hansung_connect/domain/post/converter/PostConverter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public PostResponseDto.PostCreateResponse toPostCreateResponse(Post post) {
3131
.build();
3232
}
3333

34+
public PostResponseDto.PostUpdateResponse toPostUpdateResponse(Post post) {
35+
return PostResponseDto.PostUpdateResponse.builder()
36+
.postId(post.getId())
37+
.build();
38+
}
39+
3440
public PostResponseDto.PostResponse toPostResponse(Post post, Long currentUserId, List<PostCommentResponse> commentResponses) {
3541
return PostResponseDto.PostResponse.builder()
3642
.id(post.getId())

src/main/java/hansung/hansung_connect/domain/post/dto/PostRequestDto.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@ public static class PostCreateRequest {
2121
@Schema(description = "게시글 본문", example = "한성대에서 채용 공고가 올라왔습니다.")
2222
private String body;
2323
}
24+
25+
@Getter
26+
@NoArgsConstructor
27+
@Schema(name = "PostUpdateRequest", title = "게시글 수정 요청")
28+
public static class PostUpdateRequest {
29+
30+
@Schema(description = "게시글 제목", example = "한성대 채용 공고")
31+
private String title;
32+
33+
@Schema(description = "게시글 본문", example = "한성대에서 채용 공고가 올라왔습니다.")
34+
private String body;
35+
}
2436
}

src/main/java/hansung/hansung_connect/domain/post/dto/PostResponseDto.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ public static class PostCreateResponse {
2121

2222
}
2323

24+
@Getter
25+
@Builder
26+
@AllArgsConstructor
27+
@NoArgsConstructor
28+
@Schema(name = "PostUpdateResponse", title = "게시글 수정 응답")
29+
public static class PostUpdateResponse {
30+
31+
@Schema(description = "게시글 ID", example = "123")
32+
private Long postId;
33+
34+
}
35+
2436
@Getter
2537
@Builder
2638
@NoArgsConstructor

src/main/java/hansung/hansung_connect/domain/post/entity/Post.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,12 @@ public class Post extends BaseEntity {
4848
public void increaseViews() {
4949
views += 1;
5050
}
51+
52+
public void updateTitle(String title) {
53+
this.title = title;
54+
}
55+
56+
public void updateBody(String body) {
57+
this.body = body;
58+
}
5159
}

src/main/java/hansung/hansung_connect/domain/post/service/PostCommandService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55

66
public interface PostCommandService {
77
PostResponseDto.PostCreateResponse createPost(Long userId, PostRequestDto.PostCreateRequest request);
8+
PostResponseDto.PostUpdateResponse updatePost(Long userId, Long postId, PostRequestDto.PostUpdateRequest request);
89
}

src/main/java/hansung/hansung_connect/domain/post/service/PostCommandServiceImpl.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import hansung.hansung_connect.common.exception.code.status.ErrorStatus;
55
import hansung.hansung_connect.domain.post.converter.PostConverter;
66
import hansung.hansung_connect.domain.post.dto.PostRequestDto.PostCreateRequest;
7+
import hansung.hansung_connect.domain.post.dto.PostRequestDto.PostUpdateRequest;
78
import hansung.hansung_connect.domain.post.dto.PostResponseDto.PostCreateResponse;
9+
import hansung.hansung_connect.domain.post.dto.PostResponseDto.PostUpdateResponse;
810
import hansung.hansung_connect.domain.post.entity.Post;
911
import hansung.hansung_connect.domain.post.repository.PostRepository;
1012
import hansung.hansung_connect.domain.user.entity.User;
@@ -33,4 +35,29 @@ public PostCreateResponse createPost(Long userId, PostCreateRequest request) {
3335
return postConverter.toPostCreateResponse(post);
3436
}
3537

38+
@Override
39+
public PostUpdateResponse updatePost(Long userId, Long postId, PostUpdateRequest request) {
40+
41+
Post post = postRepository.findById(postId)
42+
.orElseThrow(() -> new GeneralException(ErrorStatus.POST_NOT_FOUND));
43+
44+
if(!post.getUser().getId().equals(userId)) {
45+
throw new GeneralException(ErrorStatus.POST_FORBIDDEN);
46+
}
47+
48+
String title = request.getTitle();
49+
if(title != null) {
50+
post.updateTitle(title);
51+
}
52+
53+
String body = request.getTitle();
54+
if(body != null) {
55+
post.updateBody(body);
56+
}
57+
58+
postRepository.save(post);
59+
60+
return postConverter.toPostUpdateResponse(post);
61+
}
62+
3663
}

0 commit comments

Comments
 (0)