Skip to content

Commit

Permalink
Merge pull request #23 from Alpha-mon/14-feat-community-board-post-co…
Browse files Browse the repository at this point in the history
…mment-api

#14 - feat: Comment API
  • Loading branch information
megymj authored Oct 14, 2023
2 parents effd619 + 2c15100 commit 83cc437
Show file tree
Hide file tree
Showing 15 changed files with 369 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

@Slf4j
@RequiredArgsConstructor
@Tag(name = "community] board", description = "게시글 전체 불러오기 API")
@Tag(name = "community] board API", description = "게시글 목록 불러오기 API")
@RestController
@RequestMapping("/api/community/board")
public class BoardController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.ai.roboadvisor.domain.community.dto.request.CommentDeleteRequest;
import org.ai.roboadvisor.domain.community.dto.request.CommentRequest;
import org.ai.roboadvisor.domain.community.dto.request.CommentUpdateRequest;
import org.ai.roboadvisor.domain.community.dto.response.CommentDeleteResponse;
import org.ai.roboadvisor.domain.community.dto.response.CommentResponse;
import org.ai.roboadvisor.domain.community.service.CommentService;
import org.ai.roboadvisor.domain.community.swagger_annotation.comment.delete.*;
Expand All @@ -21,7 +22,7 @@

@Slf4j
@RequiredArgsConstructor
@Tag(name = "community] comment", description = "댓글 작성, 수정, 삭제 API")
@Tag(name = "community] comment API", description = "댓글 작성, 수정, 삭제 API")
@RestController
@RequestMapping("/api/community/comment")
public class CommentController {
Expand All @@ -46,7 +47,7 @@ public ResponseEntity<SuccessApiResponse<CommentResponse>> save(@PathVariable("p
@update_BAD_REQUEST
@update_UNAUTHORIZED
@ApiResponse_Internal_Server_Error
@PutMapping("/{postId}")
@PatchMapping("/{postId}")
public ResponseEntity<SuccessApiResponse<CommentResponse>> update(@PathVariable("postId") Long postId,
@RequestBody CommentUpdateRequest commentUpdateRequest) {
return ResponseEntity.status(HttpStatus.OK)
Expand All @@ -60,8 +61,8 @@ public ResponseEntity<SuccessApiResponse<CommentResponse>> update(@PathVariable(
@delete_UNAUTHORIZED
@ApiResponse_Internal_Server_Error
@DeleteMapping("/{postId}")
public ResponseEntity<SuccessApiResponse<CommentResponse>> delete(@PathVariable("postId") Long postId,
@RequestBody CommentDeleteRequest commentDeleteRequest) {
public ResponseEntity<SuccessApiResponse<CommentDeleteResponse>> delete(@PathVariable("postId") Long postId,
@RequestBody CommentDeleteRequest commentDeleteRequest) {
return ResponseEntity.status(HttpStatus.OK)
.body(SuccessApiResponse.success(SuccessCode.COMMENT_DELETE_SUCCESS,
commentService.delete(postId, commentDeleteRequest)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

@Slf4j
@RequiredArgsConstructor
@Tag(name = "community] post", description = "게시글 CRUD API")
@Tag(name = "community] post API", description = "게시글 CRUD API")
@RestController
@RequestMapping("/api/community/post")
public class PostController {
Expand Down Expand Up @@ -60,7 +60,7 @@ public ResponseEntity<SuccessApiResponse<PostResponse>> save(@RequestBody PostRe
@update_BAD_REQUEST
@update_UNAUTHORIZED
@ApiResponse_Internal_Server_Error
@PutMapping("/{postId}")
@PatchMapping("/{postId}")
public ResponseEntity<SuccessApiResponse<PostResponse>> update(@PathVariable("postId") Long postId, @RequestBody PostRequest postRequest) {
return ResponseEntity.status(HttpStatus.OK)
.body(SuccessApiResponse.success(SuccessCode.POST_UPDATE_SUCCESS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class CommentRequest {
@NotBlank
private String nickname;

@Schema(description = "댓글 내용", example = "안녕하세요. 댓글 작성 1입니다. ")
@Schema(description = "댓글 내용", example = "안녕하세요. 댓글 작성 1입니다.")
@NotBlank
private String content;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@AllArgsConstructor
public class CommentUpdateRequest {

@Schema(description = "댓글 번호", example = "1")
@Schema(description = "댓글 고유 번호", example = "1")
private Long commentId;

@Schema(description = "사용자의 닉네임", example = "testUser")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.ai.roboadvisor.domain.community.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.ai.roboadvisor.domain.community.entity.Comment;

@Getter
@AllArgsConstructor
public class CommentDeleteResponse {
private Long id;

public static CommentDeleteResponse fromCommentEntity(Comment comment) {
return new CommentDeleteResponse(comment.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.ai.roboadvisor.domain.community.dto.request.CommentDeleteRequest;
import org.ai.roboadvisor.domain.community.dto.request.CommentRequest;
import org.ai.roboadvisor.domain.community.dto.request.CommentUpdateRequest;
import org.ai.roboadvisor.domain.community.dto.response.CommentDeleteResponse;
import org.ai.roboadvisor.domain.community.dto.response.CommentResponse;
import org.ai.roboadvisor.domain.community.entity.Comment;
import org.ai.roboadvisor.domain.community.entity.DeleteStatus;
Expand All @@ -30,6 +31,7 @@ public CommentResponse save(Long postId, CommentRequest commentRequest) {
Tendency commentTendency = commentRequest.getTendency();
checkTendencyIsValid(commentTendency);

// 게시글의 tendency와 댓글의 tendency가 일치하는지 검증
Post existPost = postRepository.findPostById(postId)
.orElseThrow(() -> new CustomException(ErrorCode.POST_ID_NOT_EXISTS));
checkCommentIsValid(commentTendency, existPost);
Expand All @@ -55,7 +57,7 @@ public CommentResponse update(Long postId, CommentUpdateRequest commentUpdateReq
}

@Transactional
public CommentResponse delete(Long postId, CommentDeleteRequest commentDeleteRequest) {
public CommentDeleteResponse delete(Long postId, CommentDeleteRequest commentDeleteRequest) {
Comment existingComment = findExistingCommentById(postId, commentDeleteRequest.getCommentId());

// validate if user has authority
Expand All @@ -64,7 +66,7 @@ public CommentResponse delete(Long postId, CommentDeleteRequest commentDeleteReq
// manually delete a comment
deleteComment(existingComment);

return CommentResponse.fromCommentEntity(existingComment);
return CommentDeleteResponse.fromCommentEntity(existingComment);
}

private void checkTendencyIsValid(Tendency tendency) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@ApiResponse(responseCode = "200", description = """
댓글이 정상적으로 삭제된 경우: data 내에 응답 객체는 '댓글 작성', '댓글 수정' 과 동일하다.
댓글이 정상적으로 삭제된 경우:
"data" 값으로 삭제 요청된 댓글의 id값을 리턴한다.
""",
content = @Content(schema = @Schema(implementation = SuccessApiResponse.class),
examples = @ExampleObject(name = "example",
description = "정상 응답 예시",
value = """
{
"code": 200,
"message": "댓글 삭제가 정상적으로 처리되었습니다",
"data": {
"id": 1,
"postId": 1,
"nickname": "testUser",
"content": "안녕하세요 댓글 1",
"createdDateTime": "2023-09-25 04:28:41"
}
}
{
"code": 200,
"message": "댓글 삭제가 정상적으로 처리되었습니다",
"data": {
"id": 3
}
}
"""
)))
public @interface delete_OK {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@ApiResponse(responseCode = "401", description = "게시글과 댓글의 투자 성향이 다른 경우, 즉 댓글 작성 권한이 없는 경우",
@ApiResponse(responseCode = "401", description = "댓글 삭제 권한이 없는 경우",
content = @Content(schema = @Schema(implementation = SuccessApiResponse.class),
examples = @ExampleObject(name = "example",
description = "게시글과 댓글의 투자 성향이 다른 경우 예시",
description = "댓글 삭제 권한이 없는 경우 예시",
value = """
{
"code": 401,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@ApiResponse(responseCode = "400", description = "게시글 번호가 잘못 입력된 경우, 혹은 존재하지 않는 경우",
@ApiResponse(responseCode = "400", description = """
투자 성향이 LION(공격투자형), SNAKE(적극투자형), MONKEY(위험중립형), SHEEP(안정추구형) 외에 다른 값이 들어온 경우,
즉, 존재하지 않는 투자 성향이 입력된 경우
""",
content = @Content(schema = @Schema(implementation = SuccessApiResponse.class),
examples =
@ExampleObject(name = "example",
description = "게시글 번호가 잘못 입력된 경우, 혹은 존재하지 않는 경우 예시",
examples = @ExampleObject(name = "example",
description = "투자 성향이 잘못 입력된 경우 예시",
value = """
{
"code": 400,
"message": "요청하신 게시글 id가 존재하지 않습니다.",
"data": null
"code": 400,
"message": "잘못된 투자 성향 형식이 입력되었습니다",
"data": null
}
"""
)

))
)))
public @interface save_BAD_REQUEST {
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
value = """
{
"code": 401,
"message": "게시글 수정 혹은 삭제 권한이 존재하지 않습니다",
"message": "게시글과 댓글의 투자 성향이 달라서 댓글을 작성할 수 없습니다",
"data": null
}
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@ApiResponse(responseCode = "401", description = "게시글과 댓글의 투자 성향이 다른 경우, 즉 댓글 작성 권한이 없는 경우",
@ApiResponse(responseCode = "401", description = "댓글 수정 권한이 없는 경우, 즉 작성자와 수정을 요청한 사용자의 닉네임이 다른 경우",
content = @Content(schema = @Schema(implementation = SuccessApiResponse.class),
examples = @ExampleObject(name = "example",
description = "게시글과 댓글의 투자 성향이 다른 경우 예시",
description = "댓글 수정 권한이 없는 경우",
value = """
{
"code": 401,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public enum ErrorCode {
POST_ID_NOT_EXISTS(HttpStatus.BAD_REQUEST, "C001", "요청하신 게시글 id가 존재하지 않습니다."),
USER_HAS_NOT_AUTHORIZED(HttpStatus.UNAUTHORIZED, "CO02", "사용자에게 권한이 존재하지 않습니다"),
TENDENCY_NOT_MATCH_BETWEEN_POST_AND_COMMENT(HttpStatus.UNAUTHORIZED,
"CO03", "게시글 수정 혹은 삭제 권한이 존재하지 않습니다"),
"CO03", "게시글과 댓글의 투자 성향이 달라서 댓글을 작성할 수 없습니다"),

// tendency
TENDENCY_INPUT_INVALID(HttpStatus.BAD_REQUEST, "TE01", "잘못된 투자 성향 형식이 입력되었습니다");
Expand Down
Loading

0 comments on commit 83cc437

Please sign in to comment.