-
Notifications
You must be signed in to change notification settings - Fork 20
[3주차] 이건희/[feat] 게시글 도메인 API 구현 #115
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
KunHeeLee7
wants to merge
9
commits into
Leets-Official:이건희/main
Choose a base branch
from
KunHeeLee7:이건희/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: "\uC774\uAC74\uD76C/3\uC8FC\uCC28"
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4e099ed
[Chore] 프로젝트 구조 재설계 (계층 구조 -> 도메인 구조)
KunHeeLee7 31752fe
Feat: 게시글 생성 API 구현
KunHeeLee7 6f02c39
Feat: Global Exception handling 구현
KunHeeLee7 0ac55d1
feat: 게시글 상세 조회 기능 구현 및 예외 처리 연결
KunHeeLee7 ede2448
feat: 게시글 목록 조회 기능 구현
KunHeeLee7 450eeda
feat: 소프트 딜리트 기능 구현 및 응답 형식 명세서에 맞게 수정.
KunHeeLee7 f239d02
Feat: 게시글 수정 기능 추가 및 예외 처리 수정
KunHeeLee7 a70c29c
Refactor: 코드 정리 및 닉네임 노출 처리 수정
KunHeeLee7 db81a0f
refactor: UserRepository 연동 및 게시글 작성자 확인 로직 구현
KunHeeLee7 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
8 changes: 4 additions & 4 deletions
8
...ts/assignment/entity/comment/Comment.java → ...gnment/domain/comment/entity/Comment.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
68 changes: 68 additions & 0 deletions
68
LeeKunHee/src/main/java/com/leets/assignment/domain/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 |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package com.leets.assignment.domain.post.controller; | ||
|
|
||
| import com.leets.assignment.domain.post.dto.req.PostRequestDTO; | ||
| import com.leets.assignment.domain.post.dto.res.PostResponseDTO; | ||
| import com.leets.assignment.domain.post.service.PostService; | ||
| import com.leets.assignment.global.common.ApiResponse; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/posts") | ||
| @RequiredArgsConstructor | ||
| public class PostController { | ||
|
|
||
| private final PostService postService; | ||
|
|
||
| // 1. 게시글 작성 | ||
| @PostMapping | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public ApiResponse<PostResponseDTO.PostDetailResDTO> createPost( | ||
| @Valid @RequestBody PostRequestDTO.CreatePostDTO request | ||
| ) { | ||
| PostResponseDTO.PostDetailResDTO result = postService.createPost(request); | ||
| return ApiResponse.onSuccess("POST201_1", "게시글 작성에 성공했습니다.", result); | ||
| } | ||
|
|
||
| // 2. 게시글 전체 목록 조회 | ||
| @GetMapping | ||
| public ApiResponse<List<PostResponseDTO.PostListResDTO>> getPostList() { | ||
| List<PostResponseDTO.PostListResDTO> result = postService.getPostList(); | ||
| return ApiResponse.onSuccess("POST200_1", "게시글 목록 조회에 성공했습니다.", result); | ||
| } | ||
|
|
||
| // 3. 게시글 상세 조회 | ||
| @GetMapping("/{postId}") | ||
| public ApiResponse<PostResponseDTO.PostDetailResDTO> getPost(@PathVariable Long postId) { | ||
| PostResponseDTO.PostDetailResDTO result = postService.getPost(postId); | ||
| return ApiResponse.onSuccess("POST200_2", "게시글 상세 조회에 성공했습니다.", result); | ||
| } | ||
|
|
||
| // 4. 게시글 수정 | ||
| @PatchMapping("/{postId}") | ||
| public ApiResponse<PostResponseDTO.PostDetailResDTO> updatePost( | ||
| @PathVariable Long postId, | ||
| @Valid @RequestBody PostRequestDTO.UpdatePostDTO request | ||
| ) { | ||
| PostResponseDTO.PostDetailResDTO result = postService.updatePost(postId, request); | ||
| return ApiResponse.onSuccess("POST200_3", "게시글이 수정되었습니다.", result); | ||
| } | ||
|
|
||
| // 5. 게시글 삭제 | ||
| @DeleteMapping("/{postId}") | ||
| public ApiResponse<Void> deletePost( | ||
| @PathVariable Long postId, | ||
| @RequestParam Long userId // 쿼리 파라미터(?userId=1)로 작성자 ID를 받음 | ||
| ) { | ||
| postService.deletePost(postId, userId); | ||
| // 삭제는 반환할 데이터가 없으므로 result에 null을 넣습니다. | ||
| return ApiResponse.onSuccess("POST200_4", "게시글 삭제에 성공했습니다.", null); | ||
| } | ||
|
|
||
| } |
61 changes: 61 additions & 0 deletions
61
LeeKunHee/src/main/java/com/leets/assignment/domain/post/dto/req/PostRequestDTO.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,61 @@ | ||
| package com.leets.assignment.domain.post.dto.req; | ||
|
|
||
| import com.leets.assignment.domain.post.entity.BlockType; | ||
| import jakarta.validation.Valid; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotEmpty; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.Size; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import java.util.List; | ||
|
|
||
| public class PostRequestDTO { | ||
|
|
||
| // 게시글 생성용 DTO | ||
| @Getter | ||
| @NoArgsConstructor | ||
| public static class CreatePostDTO { | ||
| @NotBlank(message = "제목을 입력해주세요.") | ||
| @Size(max = 255, message = "제목은 최대 255자까지 가능합니다.") | ||
| private String title; | ||
|
|
||
| @NotNull(message = "작성자 ID는 필수입니다.") | ||
| private Long userId; | ||
|
|
||
| @NotEmpty(message = "내용을 입력해주세요.") | ||
| @Valid // 내부 블록들의 검증을 수행하기 위해 필수! | ||
| private List<BlockDTO> blocks; | ||
| } | ||
|
|
||
| // 공통 블록 DTO (생성/수정 모두 사용) | ||
| @Getter | ||
| @NoArgsConstructor | ||
| public static class BlockDTO { | ||
| @NotNull(message = "순서는 필수입니다.") | ||
| private Integer sequence; | ||
|
|
||
| @NotNull(message = "블록 타입은 필수입니다.") | ||
| private BlockType blockType; | ||
|
|
||
| @NotBlank(message = "내용을 입력해주세요.") // 각 블록의 내용이 비었을 때 | ||
| private String content; | ||
| } | ||
|
|
||
| // 게시글 수정용 DTO | ||
| @Getter | ||
| @NoArgsConstructor | ||
| public static class UpdatePostDTO { | ||
| @NotNull(message = "수정자 ID는 필수입니다.") // 권한 확인을 위해 | ||
| private Long userId; | ||
|
|
||
| @NotBlank(message = "제목을 입력해주세요.") | ||
| @Size(max = 255, message = "제목은 최대 255자까지 가능합니다.") | ||
| private String title; | ||
|
|
||
| @NotEmpty(message = "내용을 입력해주세요.") // 리스트가 비어있으면 거부 | ||
| @Valid | ||
| private List<BlockDTO> blocks; | ||
| } | ||
|
|
||
| } |
72 changes: 72 additions & 0 deletions
72
LeeKunHee/src/main/java/com/leets/assignment/domain/post/dto/res/PostResponseDTO.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,72 @@ | ||
| package com.leets.assignment.domain.post.dto.res; | ||
|
|
||
| import com.leets.assignment.domain.post.entity.BlockType; | ||
| import com.leets.assignment.domain.post.entity.Post; | ||
| import com.leets.assignment.domain.post.entity.PostBlock; | ||
| import lombok.Builder; | ||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
|
|
||
| public class PostResponseDTO { | ||
|
|
||
| // 1. 게시글 목록 조회용 (나중에 목록 기능 만들 때 사용) | ||
| @Builder | ||
| public record PostListResDTO ( | ||
| Long postId, | ||
| String title, | ||
| String nickname, | ||
| LocalDateTime createdAt | ||
| ){ | ||
| public static PostListResDTO from(Post post) { | ||
| return PostListResDTO.builder() | ||
| .postId(post.getPostId()) | ||
| .title(post.getTitle()) | ||
| .nickname(post.getUser() != null ? post.getUser().getNickname() : "알 수 없음") | ||
| .createdAt(post.getCreatedAt()) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
| // 2. 게시글 상세 조회용 (현재 구현 중인 기능) | ||
| @Builder | ||
| public record PostDetailResDTO ( | ||
| Long postId, | ||
| String title, | ||
| String nickname, | ||
| List<BlockResDTO> blocks, | ||
| LocalDateTime createdAt, | ||
| LocalDateTime updatedAt | ||
| ){ | ||
| // 이 메서드가 있어야 Service에서 .from(post)를 호출할 수 있습니다! | ||
| public static PostDetailResDTO from(Post post) { | ||
| return PostDetailResDTO.builder() | ||
| .postId(post.getPostId()) | ||
| .title(post.getTitle()) | ||
| .nickname(post.getUser() != null ? post.getUser().getNickname() : "알 수 없음") | ||
| .createdAt(post.getCreatedAt()) | ||
| .updatedAt(post.getUpdatedAt()) | ||
| .blocks(post.getBlocks().stream() | ||
| .map(BlockResDTO::from) // 아래 BlockResDTO.from 호출 | ||
| .toList()) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
| // 3. 블록 상세 정보 | ||
| @Builder | ||
| public record BlockResDTO ( | ||
| Long blockId, | ||
| Integer sequence, | ||
| BlockType blockType, | ||
| String content | ||
| ){ | ||
| public static BlockResDTO from(PostBlock block) { | ||
| return BlockResDTO.builder() | ||
| .blockId(block.getBlockId()) | ||
| .sequence(block.getSequence()) | ||
| .blockType(block.getBlockType()) | ||
| .content(block.getContent()) | ||
| .build(); | ||
| } | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
LeeKunHee/src/main/java/com/leets/assignment/domain/post/entity/BlockType.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,5 @@ | ||
| package com.leets.assignment.domain.post.entity; | ||
|
|
||
| public enum BlockType { | ||
| TEXT, IMAGE | ||
| } |
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
2 changes: 1 addition & 1 deletion
2
...ets/assignment/entity/post/PostBlock.java → ...ignment/domain/post/entity/PostBlock.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
7 changes: 7 additions & 0 deletions
7
...nHee/src/main/java/com/leets/assignment/domain/post/exception/PostForbiddenException.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,7 @@ | ||
| package com.leets.assignment.domain.post.exception; | ||
|
|
||
| public class PostForbiddenException extends RuntimeException { | ||
| public PostForbiddenException() { | ||
| super("수정 권한이 없습니다."); | ||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
...unHee/src/main/java/com/leets/assignment/domain/post/exception/PostNotFoundException.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,8 @@ | ||
| package com.leets.assignment.domain.post.exception; | ||
|
|
||
| // RuntimeException을 상속받아야 서비스에서 쉽게 던질 수 있습니다. | ||
| public class PostNotFoundException extends RuntimeException { | ||
| public PostNotFoundException() { | ||
| super("해당 게시글을 찾을 수 없습니다."); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
LeeKunHee/src/main/java/com/leets/assignment/domain/post/repository/PostRepository.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,10 @@ | ||
| package com.leets.assignment.domain.post.repository; | ||
|
|
||
| import com.leets.assignment.domain.post.entity.Post; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface PostRepository extends JpaRepository<Post, Long> { | ||
| // 기본적으로 save(), findById(), delete() 등을 제공. | ||
| } |
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.
에러마다 예외처리를 하기 보다, Post도메인의 공통 예외처리부분을 만들어두고 에러코드를 ENUM으로 관리하면 더 효율적이고 깔끔하게 관리할 수 있을 것 같습니다!