Skip to content

Commit

Permalink
Merge pull request #42 from ssu-student-union/refactor/38-post
Browse files Browse the repository at this point in the history
[refactor] #38 searchBoardPost api 검색 로직 수정 및 postcommentreaction toggle 형식으로 리팩토링
  • Loading branch information
chahyunsoo authored Aug 1, 2024
2 parents 9ffc3c6 + e3bcc78 commit 7110d96
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ussum.homepage.application.comment.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ussum.homepage.application.comment.service.CommentService;
import ussum.homepage.application.comment.service.dto.PostCommentListResponse;
Expand All @@ -16,34 +17,34 @@
public class CommentController {
private final CommentService commentService;
@GetMapping("/boards/{boardCode}/posts/{postId}/comments")
public ApiResponse<PostCommentListResponse> getPostCommentList(@PathVariable(name = "boardCode") String boardCode,
@PathVariable(name = "postId") Long postId,
@RequestParam(name = "page") int page,
@RequestParam(name = "take") int take,
@RequestParam(required = false, name = "type") String type) {
public ResponseEntity<ApiResponse<?>> getPostCommentList(@PathVariable(name = "boardCode") String boardCode,
@PathVariable(name = "postId") Long postId,
@RequestParam(name = "page") int page,
@RequestParam(name = "take") int take,
@RequestParam(required = false, name = "type") String type) {
PostCommentListResponse comments = commentService.getCommentList(boardCode, postId, page, take, type);
return ApiResponse.onSuccess(comments);
return ApiResponse.success(comments);
}
@PostMapping("/boards/{boardCode}/posts/{postId}/comments")
public ApiResponse<PostCommentResponse> createPostComment(@PathVariable(name = "boardCode") String boardCode,
@PathVariable(name = "postId") Long postId,
@RequestBody PostCommentCreateRequest postCommentCreateRequest) {
public ResponseEntity<ApiResponse<?>> createPostComment(@PathVariable(name = "boardCode") String boardCode,
@PathVariable(name = "postId") Long postId,
@RequestBody PostCommentCreateRequest postCommentCreateRequest) {
PostCommentResponse comment = commentService.createComment(null, boardCode, postId, postCommentCreateRequest);
return ApiResponse.onSuccess(comment);
return ApiResponse.success(comment);
}
@PatchMapping("/boards/{boardCode}/posts/{postId}/comments/{commentId}")
public ApiResponse<PostCommentResponse> editPostComment(@PathVariable(name = "boardCode") String boardCode,
@PathVariable(name = "postId") Long postId,
@PathVariable(name = "commentId") Long commentId,
@RequestBody PostCommentUpdateRequest postCommentUpdateRequest) {
public ResponseEntity<ApiResponse<?>> editPostComment(@PathVariable(name = "boardCode") String boardCode,
@PathVariable(name = "postId") Long postId,
@PathVariable(name = "commentId") Long commentId,
@RequestBody PostCommentUpdateRequest postCommentUpdateRequest) {
PostCommentResponse comment = commentService.editComment(null, postId, commentId, postCommentUpdateRequest);
return ApiResponse.onSuccess(comment);
return ApiResponse.success(comment);
}
@DeleteMapping("/boards/{boardCode}/posts/{postId}/comments/{commentId}")
public ApiResponse<PostCommentResponse> deletePostComment(@PathVariable(name = "boardCode") String boardCode,
public ResponseEntity<ApiResponse<?>> deletePostComment(@PathVariable(name = "boardCode") String boardCode,
@PathVariable(name = "postId") Long postId,
@PathVariable(name = "commentId") Long commentId) {
commentService.deleteComment(commentId);
return ApiResponse.onSuccess(null);
return ApiResponse.success(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public class PostController {
private final PostService postService;

@GetMapping("/{boardCode}/posts")
public ApiResponse<PostListResponse> getBoardPostsList(@RequestParam(value = "page", defaultValue = "0") int page, @RequestParam(value = "take") int take,
public ResponseEntity<ApiResponse<?>> getBoardPostsList(@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "take") int take,
@PathVariable(name = "boardCode") String boardCode) {

PostListResponse postList = postService.getPostList(PageRequest.of(page, take, Sort.by("id").descending()), boardCode);
return ApiResponse.onSuccess(postList);
return ApiResponse.success(postList);
}

@GetMapping("/{boardCode}/posts/top-liked")
Expand All @@ -37,39 +38,45 @@ public ResponseEntity<ApiResponse<?>> getTopLikedBoardPostList(@RequestParam(val
}

@GetMapping("/{boardCode}/posts/{postId}")
public ApiResponse<PostResponse> getBoardPost(@PathVariable(name = "boardCode") String boardCode,
@PathVariable(name = "postId") Long postId) {
public ResponseEntity<ApiResponse<?>> getBoardPost(@PathVariable(name = "boardCode") String boardCode,
@PathVariable(name = "postId") Long postId) {

return ApiResponse.onSuccess(postService.getPost(boardCode, postId));
PostResponse post = postService.getPost(boardCode, postId);
return ApiResponse.success(post);
}

@PostMapping("/{boardCode}/posts")
public ApiResponse<?> createBoardPost(@UserId Long userId, @PathVariable(name = "boardCode") String boardCode,
@RequestBody PostCreateRequest postCreateRequest) {
public ResponseEntity<ApiResponse<?>> createBoardPost(@UserId Long userId,
@PathVariable(name = "boardCode") String boardCode,
@RequestBody PostCreateRequest postCreateRequest) {
postService.createPost(userId, boardCode,postCreateRequest);
return ApiResponse.onSuccess(null);
return ApiResponse.success(null);
}

@PatchMapping("/{boardCode}/posts/{postId}")
public ApiResponse<PostResponse> editBoardPost(@PathVariable(name = "boardCode") String boardCode, @PathVariable(name = "postId") Long postId,
@RequestBody PostUpdateRequest postUpdateRequest) {
public ResponseEntity<ApiResponse<?>> editBoardPost(@PathVariable(name = "boardCode") String boardCode,
@PathVariable(name = "postId") Long postId,
@RequestBody PostUpdateRequest postUpdateRequest) {
PostResponse post = postService.editPost(boardCode,postId, postUpdateRequest);
return ApiResponse.onSuccess(post);
return ApiResponse.success(post);
}

@DeleteMapping("/{boardCode}/posts/{postId}")
public ApiResponse<?> deleteBoardPost(@PathVariable(name = "boardCode") String boardCode, @PathVariable(name = "postId") Long postId) {
public ResponseEntity<ApiResponse<?>> deleteBoardPost(@PathVariable(name = "boardCode") String boardCode,
@PathVariable(name = "postId") Long postId) {
postService.deletePost(boardCode, postId);
return ApiResponse.onSuccess(null);
return ApiResponse.success(null);
}

@GetMapping("/{boardCode}/posts/search")
public ApiResponse<PostListResponse> searchBoardPost(@RequestParam(value = "q") String q, @RequestParam(value = "categorycode") String categoryCode,
@RequestParam(value = "page", defaultValue = "0") int page, @RequestParam(value = "take") int take,
@PathVariable String boardCode) {
return ApiResponse.onSuccess(postService.searchPost(
PageRequest.of(page, take, Sort.by("id").descending()), boardCode, q, categoryCode)
);
public ResponseEntity<ApiResponse<?>> searchBoardPost(@RequestParam(value = "q") String q,
@RequestParam(value = "categoryCode") String categoryCode,
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "take") int take,
@PathVariable String boardCode) {

PostListResponse postList = postService.searchPost(PageRequest.of(page, take, Sort.by("id").descending()), boardCode, q, categoryCode);
return ApiResponse.success(postList);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class PostService {
private final BoardReader boardReader;
private final CategoryReader categoryReader;
private final UserReader userReader;

private final PostReader postReader;
private final PostFormatter postFormatter;
private final PostAppender postAppender;
Expand All @@ -40,7 +39,7 @@ public class PostService {
public PostListResponse getPostList(Pageable pageable, String boardCode) {
// Board board = boardReader.getBoardWithBoardCode(boardCode);
Page<Post> postList = postReader.getPostList(pageable, boardCode);
return PostListResponse.of(postList.getContent(), (int) postList.getTotalElements(), postFormatter::format);
return PostListResponse.of(postList.getContent(), (int) postList.getTotalElements(), postList.getNumberOfElements(), postFormatter::format);
}

public TopLikedPostListResponse getTopLikedPostList(int page, int take, String boardCode){
Expand Down Expand Up @@ -78,8 +77,7 @@ public void deletePost(String boardCode,Long postId){

public PostListResponse searchPost(Pageable pageable, String boardCode, String q, String categoryCode) {
Page<Post> searchPost = postReader.getPostListBySearch(pageable, boardCode, q, categoryCode);
return PostListResponse.of(searchPost.getContent(), (int) searchPost.getTotalElements(),
postFormatter::format);
return PostListResponse.of(searchPost.getContent(), (int) searchPost.getTotalElements(), searchPost.getNumberOfElements(), postFormatter::format);
}

private List<SimplePostResponse> createSimplePostResponse(List<SimplePostResponse> simplePostDtoList){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

public record PostListResponse(
List<PostResponse> posts,
Integer total
Integer totalElements,
Integer numberOfElements
) {
public static PostListResponse of(List<Post> posts, Integer total, Function<Long, PostResponse> formatter) {
public static PostListResponse of(List<Post> posts, Integer totalElements, Integer numberOfElements, Function<Long, PostResponse> formatter) {
return new PostListResponse(
posts.stream()
.map(post -> formatter.apply(post.getId()))
.toList(), total);
.toList(), totalElements, numberOfElements);
}

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,64 @@
package ussum.homepage.application.reaction.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ussum.homepage.application.reaction.service.PostCommentReactionService;
import ussum.homepage.application.reaction.service.dto.request.CreatePostCommentReactionReq;
import ussum.homepage.application.reaction.service.dto.request.PostCommentReactionCreateRequest;
import ussum.homepage.application.reaction.service.dto.response.PostCommentReactionResponse;
import ussum.homepage.global.ApiResponse;
import ussum.homepage.global.config.auth.UserId;

@RequiredArgsConstructor
@RequestMapping
@RestController
@Tag(name = "post_comment_reaction", description = "게시물 댓글 반응 api")
public class PostCommentReactionController {
private final PostCommentReactionService postCommentReactionService;

@Operation(summary = "게시물 댓글 반응 통합 api", description = """
게시물 댓글 반응을 위한 통합 api입니다.
요청 json으로 like 또는 unlike를 받습니다.
ex. like를 Request에 넣어서 요청을 하면 댓글 좋아요가 생성되고, 동일한 요청을 한번더 요청하면 이전에 눌렀던 댓긇 좋아요가 취소됩니다.
""")
@PostMapping("/toggle/posts/{commentId}")
public ResponseEntity<ApiResponse<?>> togglePostCommentReaction(@UserId Long userId,
@PathVariable(name = "commentId") Long commentId,
@RequestBody CreatePostCommentReactionReq createPostCommentReactionReq) {
postCommentReactionService.postCommentReactionToggle(userId, commentId, createPostCommentReactionReq);
return ApiResponse.success("배현서 박정우 11번가 입사를 축하드립니다.");
}

@Operation(summary = "게시물 댓글 반응 생성 api", description = """
게시물 댓글 반응을 등록하기 위한 api입니다.
요청 json으로 like 또는 unlike를
받습니다.
""")
@PostMapping("/boards/posts/comments/{commentId}/reactions")
public ApiResponse<Void> createPostCommentReaction(@PathVariable(name = "commentId") Long commentId,
@RequestBody PostCommentReactionCreateRequest postCommentReactionCreateRequest) {
PostCommentReactionResponse commentReaction = postCommentReactionService.createPostCommentReaction(commentId, postCommentReactionCreateRequest);
// return ApiResponse.onSuccess(commentReaction);
return ApiResponse.onSuccess(null);
public ResponseEntity<ApiResponse<?>> createPostCommentReaction(@PathVariable(name = "commentId") Long commentId,
@RequestBody PostCommentReactionCreateRequest postCommentReactionCreateRequest) {
postCommentReactionService.createPostCommentReaction(commentId, postCommentReactionCreateRequest);
return ApiResponse.success(null);
}

@Operation(summary = "게시물 댓글 반응 삭제 api", description = """
게시물 댓글 반응을 삭제하기 위한 api입니다.
요청 json으로 like 또는 unlike를 받습니다.
""")
@DeleteMapping("/boards/posts/comments/{commentId}/reactions")
public ApiResponse<Void> deletePostCommentReaction(@PathVariable(name = "commentId") Long commentId,
@RequestBody PostCommentReactionCreateRequest postCommentReactionCreateRequest) {
public ResponseEntity<ApiResponse<?>> deletePostCommentReaction(@PathVariable(name = "commentId") Long commentId,
@RequestBody PostCommentReactionCreateRequest postCommentReactionCreateRequest) {
postCommentReactionService.deletePostCommentReaction(commentId, postCommentReactionCreateRequest);
return ApiResponse.onSuccess(null);
return ApiResponse.success(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,31 @@
public class PostReactionController {
private final PostReactionService postReactionService;

@Operation(summary = "게시물 반응 통합 api", description = """
게시물 반응을 위한 통합 api입니다.
요청 json으로 like 또는 unlike를 받습니다.
ex. like를 Request에 넣어서 요청을 하면 좋아요가 생성되고, 동일한 요청을 한번더 요청하면 이전에 눌렀던 좋아요가 취소됩니다.
""")
@PostMapping("/toggle/{postId}")
public ResponseEntity<ApiResponse<?>> togglePostReaction(@UserId Long userId, @PathVariable(name = "postId")Long postId, @RequestBody CreatePostReactionReq createPostReactionReq) {
postReactionService.postReactionToggle(userId, postId, createPostReactionReq);
return ApiResponse.success(null);
}

@Operation(summary = "게시물 반응 생성 api", description = """
게시물 반응을 등록하기 위한 api입니다.
요청 json으로 like 또는 unlike를 받습니다.
""")
@PostMapping("/boards/posts/{postId}/reactions")
public ApiResponse<Void> createPostReaction(@PathVariable(name = "postId") Long postId,
public ResponseEntity<ApiResponse<?>> createPostReaction(@PathVariable(name = "postId") Long postId,
@RequestBody PostReactionCreateRequest postReactionCreateRequest) {
PostReactionResponse postReaction = postReactionService.createPostReaction(postId, postReactionCreateRequest);
return ApiResponse.onSuccess(null);
postReactionService.createPostReaction(postId, postReactionCreateRequest);
return ApiResponse.success(null);
}

@Operation(summary = "게시물 반응 삭제 api", description = """
Expand All @@ -46,10 +55,10 @@ public ApiResponse<Void> createPostReaction(@PathVariable(name = "postId") Long
""")
@DeleteMapping("/boards/posts/{postId}/reactions")
public ApiResponse<Void> deletePostReaction(@UserId Long userId, @PathVariable(name = "postId") Long postId,
public ResponseEntity<ApiResponse<?>> deletePostReaction(@UserId Long userId, @PathVariable(name = "postId") Long postId,
@RequestBody PostReactionCreateRequest postReactionCreateRequest) {
postReactionService.deletePostReaction(userId, postId, postReactionCreateRequest);
return ApiResponse.onSuccess(null);
return ApiResponse.success(null);

}
}
Loading

0 comments on commit 7110d96

Please sign in to comment.