-
Notifications
You must be signed in to change notification settings - Fork 20
[3주차] 변승현/[feat] 게시글 도메인 API 구현 #92
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
base: 변승현/main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\uBCC0\uC2B9\uD604/3\uC8FC\uCC28"
Changes from all commits
b976a1c
e325e87
2fdb211
7ada4a3
0bcb710
244395b
d47bde3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.example.demo.controller; | ||
|
|
||
| import com.example.demo.domain.post.dto.*; | ||
| import com.example.demo.domain.post.service.PostService; | ||
| import com.example.demo.global.response.ApiResponse; | ||
| import jakarta.validation.Valid; | ||
| import jakarta.validation.constraints.Min; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.validation.annotation.Validated; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/posts") | ||
| @RequiredArgsConstructor | ||
| @Validated | ||
| public class PostController { | ||
|
|
||
| private final PostService postService; | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<ApiResponse<PostListResponse>> getPosts( | ||
| @RequestParam(defaultValue = "0") @Min(value = 0, message = "페이지 번호는 0 이상이어야 합니다.") int page, | ||
| @RequestParam(defaultValue = "10") @Min(value = 1, message = "페이지 크기는 1 이상이어야 합니다.") int size | ||
| ) { | ||
| return ResponseEntity.ok( | ||
| ApiResponse.success("POST_LIST_SUCCESS", "게시글 목록 조회 성공", postService.getPosts(page, size)) | ||
| ); | ||
| } | ||
|
|
||
| @GetMapping("/{postId}") | ||
| public ResponseEntity<ApiResponse<PostDetailResponse>> getPost(@PathVariable Long postId) { | ||
| return ResponseEntity.ok( | ||
| ApiResponse.success("POST_DETAIL_SUCCESS", "게시글 상세 조회 성공", postService.getPost(postId)) | ||
| ); | ||
| } | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<ApiResponse<PostCreateResponse>> createPost( | ||
| @Valid @RequestBody PostCreateRequest request | ||
| ) { | ||
| return ResponseEntity.status(HttpStatus.CREATED) | ||
| .body(ApiResponse.success( | ||
| "POST_CREATE_SUCCESS", | ||
| "게시글 생성 성공", | ||
| postService.createPost(request) | ||
| )); | ||
| } | ||
|
|
||
| @PatchMapping("/{postId}") | ||
| public ResponseEntity<ApiResponse<Void>> updatePost( | ||
| @PathVariable Long postId, | ||
| @Valid @RequestBody PostUpdateRequest request | ||
| ) { | ||
| postService.updatePost(postId, request); | ||
| return ResponseEntity.ok( | ||
| ApiResponse.success("POST_UPDATE_SUCCESS", "게시글이 수정되었습니다.", null) | ||
| ); | ||
| } | ||
|
|
||
| @DeleteMapping("/{postId}") | ||
| public ResponseEntity<ApiResponse<Void>> deletePost( | ||
| @PathVariable Long postId, | ||
| @Valid @RequestBody PostDeleteRequest request | ||
| ) { | ||
| postService.deletePost(postId, request); | ||
| return ResponseEntity.ok( | ||
| ApiResponse.success("POST_DELETE_SUCCESS", "게시글이 삭제되었습니다.", null) | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.example.demo.domain.post.dto; | ||
|
|
||
| public record PageInfoResponse( | ||
| int page, | ||
| int size, | ||
| long totalElements, | ||
| int totalPages, | ||
| boolean hasNext | ||
| ) { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.example.demo.domain.post.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record PostCreateRequest( | ||
| @NotNull(message = "userId는 필수입니다.") | ||
| Long userId, | ||
|
|
||
| @NotBlank(message = "title은 필수입니다.") | ||
| String title, | ||
|
|
||
| @NotBlank(message = "content는 필수입니다.") | ||
| String content, | ||
|
|
||
| String imageUrl | ||
| ) { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.example.demo.domain.post.dto; | ||
|
|
||
| public record PostCreateResponse( | ||
| Long postId, | ||
| String message | ||
| ) { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.example.demo.domain.post.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record PostDeleteRequest( | ||
| @NotNull(message = "userId는 필수입니다.") | ||
| Long userId | ||
| ) { | ||
|
Comment on lines
+5
to
+8
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. 게시글 삭제하는데 유저아이디로만 삭제하고 있습니다! PostId를 추가로 받아서 선택삭제하는 방법을 의도하신거라면 postId가 필요해보입니다! |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.example.demo.domain.post.dto; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public record PostDetailResponse( | ||
| Long postId, | ||
| String title, | ||
| String content, | ||
| String imageUrl, | ||
|
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. 확장성을 고려하여 List 을 이용하여 2개 이상 이미지를 응답으로 보내는것도 고려해보시면 좋을 것 같습니다! |
||
| Long authorId, | ||
| String author, | ||
| LocalDateTime createdAt, | ||
| LocalDateTime updatedAt | ||
| ) { | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.example.demo.domain.post.dto; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public record PostListItemResponse( | ||
| Long postId, | ||
| String title, | ||
| String content, | ||
| String thumbnailImageUrl, | ||
| String author, | ||
| LocalDateTime createdAt | ||
| ) { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.example.demo.domain.post.dto; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record PostListResponse( | ||
| List<PostListItemResponse> posts, | ||
| PageInfoResponse pageInfo | ||
| ) { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.example.demo.domain.post.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record PostUpdateRequest( | ||
| @NotNull(message = "userId는 필수입니다.") | ||
| Long userId, | ||
|
|
||
| String title, | ||
| String content, | ||
| String imageUrl | ||
| ) { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.example.demo.domain.post.repository; | ||
|
|
||
| import com.example.demo.domain.post.entity.Post; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface PostRepository extends JpaRepository<Post, Long> { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package com.example.demo.domain.post.service; | ||
|
|
||
| import com.example.demo.domain.post.dto.*; | ||
| import com.example.demo.domain.post.entity.Post; | ||
| import com.example.demo.domain.post.repository.PostRepository; | ||
| import com.example.demo.domain.user.entity.User; | ||
| import com.example.demo.domain.user.repository.UserRepository; | ||
| import com.example.demo.global.exception.CustomException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional(readOnly = true) | ||
| public class PostService { | ||
|
|
||
| private final PostRepository postRepository; | ||
| private final UserRepository userRepository; | ||
|
|
||
| public PostListResponse getPosts(int page, int size) { | ||
| Page<Post> postPage = postRepository.findAll(PageRequest.of(page, size)); | ||
|
|
||
| return new PostListResponse( | ||
| postPage.getContent().stream() | ||
| .map(post -> new PostListItemResponse( | ||
| post.getId(), | ||
| post.getTitle(), | ||
| post.getContent(), | ||
| post.getImageUrl(), | ||
| post.getUser().getName(), | ||
| post.getCreatedAt() | ||
| )) | ||
| .toList(), | ||
| new PageInfoResponse( | ||
| postPage.getNumber(), | ||
| postPage.getSize(), | ||
| postPage.getTotalElements(), | ||
| postPage.getTotalPages(), | ||
| postPage.hasNext() | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| public PostDetailResponse getPost(Long postId) { | ||
| Post post = findPost(postId); | ||
|
|
||
| return new PostDetailResponse( | ||
| post.getId(), | ||
| post.getTitle(), | ||
| post.getContent(), | ||
| post.getImageUrl(), | ||
| post.getUser().getId(), | ||
| post.getUser().getName(), | ||
| post.getCreatedAt(), | ||
| post.getUpdatedAt() | ||
| ); | ||
| } | ||
|
|
||
| @Transactional | ||
| public PostCreateResponse createPost(PostCreateRequest request) { | ||
| User user = userRepository.findById(request.userId()) | ||
| .orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "USER_NOT_FOUND", "해당 사용자를 찾을 수 없습니다.")); | ||
|
|
||
| Post post = Post.of( | ||
| user, | ||
| request.title(), | ||
| request.content(), | ||
| request.imageUrl() | ||
| ); | ||
|
|
||
| Post savedPost = postRepository.save(post); | ||
| return new PostCreateResponse(savedPost.getId(), "게시글이 생성되었습니다."); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void updatePost(Long postId, PostUpdateRequest request) { | ||
| Post post = findPost(postId); | ||
|
|
||
| validateOwner(post, request.userId()); | ||
|
|
||
| if ((request.title() == null || request.title().isBlank()) | ||
| && (request.content() == null || request.content().isBlank()) | ||
| && request.imageUrl() == null) { | ||
| throw new CustomException(HttpStatus.BAD_REQUEST, "INVALID_PARAMETER", "수정할 값이 없습니다."); | ||
| } | ||
|
|
||
| post.update(request.title(), request.content(), request.imageUrl()); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void deletePost(Long postId, PostDeleteRequest request) { | ||
| Post post = findPost(postId); | ||
|
|
||
| validateOwner(post, request.userId()); | ||
|
|
||
| postRepository.delete(post); | ||
| } | ||
|
|
||
| private Post findPost(Long postId) { | ||
| return postRepository.findById(postId) | ||
| .orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "POST_NOT_FOUND", "해당 게시글을 찾을 수 없습니다.")); | ||
| } | ||
|
|
||
| private void validateOwner(Post post, Long userId) { | ||
| if (!post.getUser().getId().equals(userId)) { | ||
| throw new CustomException(HttpStatus.FORBIDDEN, "FORBIDDEN", "권한이 없습니다."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.example.demo.domain.user.repository; | ||
|
|
||
| import com.example.demo.domain.user.entity.User; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface UserRepository extends JpaRepository<User, Long> { | ||
| } |
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.
이미 알고 계시겠지만 이 부분은 DTO에서 검증할 수 있습니다~! 컨트롤러단에서는 요청을 받고 보내는 역할로 두는것을 추천드립니다~!