-
Notifications
You must be signed in to change notification settings - Fork 20
[3주차] 시재욱/[feat] 게시글 도메인 API 구현 #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seejaewook456-maker
wants to merge
4
commits into
Leets-Official:시재욱/main
Choose a base branch
from
seejaewook456-maker:시재욱/3주차
base: 시재욱/main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "\uC2DC\uC7AC\uC6B1/3\uC8FC\uCC28"
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/example/demo/comment/dto/CommentCreateRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,18 @@ | ||
| package com.example.demo.comment.dto; | ||
|
|
||
| import lombok.Getter; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| @Getter | ||
| public class CommentCreateRequest { | ||
|
|
||
| @NotNull(message = "유저 ID는 필수입니다.") | ||
| private Long userId; | ||
|
|
||
| @NotNull(message = "게시글 ID는 필수입니다.") | ||
| private Long postId; | ||
|
|
||
| @NotBlank(message = "댓글 내용은 비어 있을 수 없습니다.") | ||
| private String content; | ||
| } |
3 changes: 3 additions & 0 deletions
3
src/main/java/com/example/demo/comment/dto/CommentUpdateRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| package com.example.demo.comment.dto; | ||
|
|
||
| import lombok.Getter; | ||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
| @Getter | ||
| public class CommentUpdateRequest { | ||
|
|
||
| @NotBlank(message = "댓글 내용은 비어 있을 수 없습니다.") | ||
| private String content; | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/demo/global/exception/ApiResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.example.demo.global.exception; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public class ApiResponse<T> { | ||
| private boolean isSuccess; | ||
| private String code; | ||
| private String message; | ||
| private T result; | ||
| } |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/example/demo/global/exception/BaseCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.example.demo.global.exception; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum BaseCode { | ||
|
|
||
| // 공통 | ||
| SUCCESS("2000", "성공"), | ||
| INVALID_REQUEST("4002", "잘못된 요청입니다."), | ||
|
|
||
| // user | ||
| USER_CREATE_SUCCESS("2001", "유저 생성 성공"), | ||
|
|
||
| // post | ||
| POST_CREATE_SUCCESS("2010", "게시글 생성 성공"), | ||
| POST_UPDATE_SUCCESS("2002", "게시글 수정 성공"), | ||
| POST_DELETE_SUCCESS("2003", "게시글 삭제 성공"), | ||
| POST_NOT_FOUND("4040", "해당 게시글을 찾을 수 없습니다."), | ||
|
|
||
| // comment | ||
| COMMENT_CREATE_SUCCESS("2011", "댓글 생성 성공"), | ||
| COMMENT_UPDATE_SUCCESS("2004", "댓글 수정 성공"), | ||
| COMMENT_DELETE_SUCCESS("2005", "댓글 삭제 성공"); | ||
|
|
||
| private final String code; | ||
| private final String message; | ||
| } | ||
52 changes: 52 additions & 0 deletions
52
src/main/java/com/example/demo/global/exception/GlobalExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package com.example.demo.global.exception; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.MethodArgumentNotValidException; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| @RestControllerAdvice | ||
| public class GlobalExceptionHandler { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // 검증 만족 못한 내용들 | ||
| @ExceptionHandler(MethodArgumentNotValidException.class) | ||
| public ResponseEntity<ApiResponse<Map<String, String>>> handleValidation( | ||
| MethodArgumentNotValidException e) { | ||
|
|
||
| Map<String, String> errors = new HashMap<>(); | ||
|
|
||
| e.getBindingResult().getFieldErrors() | ||
| .forEach(error -> | ||
| errors.put(error.getField(), error.getDefaultMessage()) | ||
| ); | ||
|
|
||
| return ResponseEntity.badRequest() | ||
| .body(ResponseUtil.fail(BaseCode.INVALID_REQUEST, errors)); | ||
| } | ||
|
|
||
| // 게시글 없음 | ||
| @ExceptionHandler(IllegalArgumentException.class) | ||
| public ResponseEntity<ApiResponse<Map<String, Object>>> handleIllegal( | ||
| IllegalArgumentException e) { | ||
|
|
||
| if (e.getMessage().contains("게시글 없음")) { | ||
| return ResponseEntity.status(404) | ||
| .body(ResponseUtil.fail( | ||
| BaseCode.POST_NOT_FOUND, | ||
| Map.of("postId", -1) | ||
| )); | ||
| } | ||
|
|
||
| return ResponseEntity.badRequest() | ||
| .body(ResponseUtil.fail(BaseCode.INVALID_REQUEST, null)); | ||
| } | ||
|
|
||
| // 기타 예외상황 | ||
| @ExceptionHandler(Exception.class) | ||
| public ResponseEntity<ApiResponse<Void>> handleException() { | ||
| return ResponseEntity.internalServerError() | ||
| .body(ResponseUtil.fail(BaseCode.INVALID_REQUEST, null)); | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
src/main/java/com/example/demo/global/exception/ResponseUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.example.demo.global.exception; | ||
|
|
||
| public class ResponseUtil { | ||
|
|
||
| public static <T> ApiResponse<T> success(BaseCode code, T result) { | ||
| return ApiResponse.<T>builder() | ||
| .isSuccess(true) | ||
| .code(code.getCode()) | ||
| .message(code.getMessage()) | ||
| .result(result) | ||
| .build(); | ||
| } | ||
|
|
||
| public static <T> ApiResponse<T> fail(BaseCode code, T result) { | ||
| return ApiResponse.<T>builder() | ||
| .isSuccess(false) | ||
| .code(code.getCode()) | ||
| .message(code.getMessage()) | ||
| .result(result) | ||
| .build(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 48 additions & 3 deletions
51
src/main/java/com/example/demo/post/controller/PostController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,66 @@ | ||
| package com.example.demo.post.controller; | ||
|
|
||
| import com.example.demo.global.exception.ApiResponse; | ||
| import com.example.demo.global.exception.BaseCode; | ||
| import com.example.demo.global.exception.ResponseUtil; | ||
| import com.example.demo.post.dto.PostCreateRequest; | ||
| import com.example.demo.post.dto.PostDetailResponse; | ||
| import com.example.demo.post.dto.PostResponse; | ||
| import com.example.demo.post.service.PostService; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/posts") | ||
| public class PostController { | ||
|
|
||
| private final PostService postService; | ||
|
|
||
| //게시글 생성 | ||
| @PostMapping | ||
| public Long create(@RequestBody PostCreateRequest request) { | ||
| return postService.createPost(request); | ||
| public ApiResponse<PostResponse> create( | ||
| @RequestBody @Valid PostCreateRequest request) { | ||
|
|
||
| return ResponseUtil.success( | ||
| BaseCode.POST_CREATE_SUCCESS, | ||
| postService.createPost(request) | ||
| ); | ||
| } | ||
|
|
||
| //게시글 수정 | ||
| @PutMapping("/{postId}") | ||
| public ApiResponse<PostResponse> update( | ||
| @PathVariable Long postId, | ||
| @RequestBody @Valid PostCreateRequest request) { | ||
|
|
||
| return ResponseUtil.success( | ||
| BaseCode.POST_UPDATE_SUCCESS, | ||
| postService.updatePost(postId, request) | ||
| ); | ||
| } | ||
|
|
||
| //게시글 삭제 | ||
| @DeleteMapping("/{postId}") | ||
| public ApiResponse<Map<String, Long>> delete( | ||
| @PathVariable Long postId) { | ||
|
|
||
| } | ||
| return ResponseUtil.success( | ||
| BaseCode.POST_DELETE_SUCCESS, | ||
| Map.of("postId", postService.deletePost(postId)) | ||
| ); | ||
| } | ||
|
|
||
| //게시글 조회 | ||
| @GetMapping("/{postId}") | ||
| public ApiResponse<PostDetailResponse> getPost(@PathVariable Long postId) { | ||
|
|
||
| return ResponseUtil.success( | ||
| BaseCode.SUCCESS, | ||
| postService.getPostDetail(postId) | ||
| ); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/demo/post/dto/PostBlockResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.example.demo.post.dto; | ||
|
|
||
| import com.example.demo.post.entity.BlockType; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public class PostBlockResponse { | ||
|
|
||
| private BlockType blockType; | ||
| private String textContent; | ||
| private String imageUrl; | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/example/demo/post/dto/PostCreateRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,24 @@ | ||
| package com.example.demo.post.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotEmpty; | ||
| import lombok.Getter; | ||
| import java.util.List; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
|
|
||
| @Getter | ||
| public class PostCreateRequest { | ||
|
|
||
| @NotNull(message = "유저 ID는 필수입니다.") | ||
| private Long userId; | ||
|
|
||
| @NotBlank(message = "제목은 비어 있을 수 없습니다.") | ||
| private String title; | ||
|
|
||
| @NotBlank(message = "설명은 비어 있을 수 없습니다.") | ||
| private String description; | ||
|
|
||
| @NotEmpty(message = "블록은 최소 1개 이상이어야 합니다.") | ||
| private List<PostBlockDto> blocks; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
모든 API 응답을 ApiResponse로 통일하고 BaseCode를 enum으로 코드와 메시지를 관리한 부분 잘 설계하신 것 같습니다!👍