From 6b0898e6c36c1adc6be0b7f923cd0a49785f9e7f Mon Sep 17 00:00:00 2001 From: dongkyeomjang Date: Wed, 27 Nov 2024 05:18:47 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feature/#169=20-=206.5=20=EB=8C=93?= =?UTF-8?q?=EA=B8=80=20=EC=9E=91=EC=84=B1=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SuggestionControllerHttpRequest.http | 12 ++++- .../SuggestionCommandV1Controller.java | 44 ++++++++++++++++ .../dto/request/CreateCommentRequestDto.java | 9 ++++ .../service/CreateCommentService.java | 50 +++++++++++++++++++ .../usecase/CreateCommentUseCase.java | 11 ++++ .../domain/service/CommentService.java | 15 ++++++ 6 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/daon/onjung/suggestion/application/controller/command/SuggestionCommandV1Controller.java create mode 100644 src/main/java/com/daon/onjung/suggestion/application/dto/request/CreateCommentRequestDto.java create mode 100644 src/main/java/com/daon/onjung/suggestion/application/service/CreateCommentService.java create mode 100644 src/main/java/com/daon/onjung/suggestion/application/usecase/CreateCommentUseCase.java diff --git a/http/suggestion/SuggestionControllerHttpRequest.http b/http/suggestion/SuggestionControllerHttpRequest.http index 3146eb0..2bf8d09 100644 --- a/http/suggestion/SuggestionControllerHttpRequest.http +++ b/http/suggestion/SuggestionControllerHttpRequest.http @@ -36,4 +36,14 @@ Content-Type: application/json Content-Disposition: form-data; name="file"; filename="image.png" Content-Type: image/png -< /Users/kyeom/Desktop/1_banner.png \ No newline at end of file +< /Users/kyeom/Desktop/1_banner.png + +### 6.5 댓글 등록하기 +// @no-log +POST {{host_url}}/api/v1/boards/{{suggestion.API_6_5.id}}/comments +Content-Type: application/json +Authorization: Bearer {{access_token}} + +{ + "content": "{{suggestion.API_6_5.content}}" +} \ No newline at end of file diff --git a/src/main/java/com/daon/onjung/suggestion/application/controller/command/SuggestionCommandV1Controller.java b/src/main/java/com/daon/onjung/suggestion/application/controller/command/SuggestionCommandV1Controller.java new file mode 100644 index 0000000..68226d8 --- /dev/null +++ b/src/main/java/com/daon/onjung/suggestion/application/controller/command/SuggestionCommandV1Controller.java @@ -0,0 +1,44 @@ +package com.daon.onjung.suggestion.application.controller.command; + +import com.daon.onjung.core.annotation.security.AccountID; +import com.daon.onjung.core.dto.ResponseDto; +import com.daon.onjung.suggestion.application.dto.request.CreateBoardRequestDto; +import com.daon.onjung.suggestion.application.dto.request.CreateCommentRequestDto; +import com.daon.onjung.suggestion.application.usecase.CreateBoardUseCase; +import com.daon.onjung.suggestion.application.usecase.CreateCommentUseCase; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.util.UUID; + +@RestController +@RequiredArgsConstructor +public class SuggestionCommandV1Controller { + + private final CreateBoardUseCase createBoardUseCase; + private final CreateCommentUseCase createCommentUseCase; + + @PostMapping(value = "/api/v1/boards", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE}) + public ResponseDto createBoard( + @AccountID UUID accountId, + @RequestPart(value = "file", required = false) MultipartFile file, + @RequestPart("body") @Valid CreateBoardRequestDto requestDto + ) { + createBoardUseCase.execute(accountId, file, requestDto); + return ResponseDto.created(null); + } + + @PostMapping("/api/v1/boards/{id}/comments") + public ResponseDto createComment( + @AccountID UUID accountId, + @PathVariable Long id, + @RequestBody @Valid CreateCommentRequestDto requestDto + ) { + createCommentUseCase.execute(accountId, id, requestDto); + return ResponseDto.created(null); + } + +} diff --git a/src/main/java/com/daon/onjung/suggestion/application/dto/request/CreateCommentRequestDto.java b/src/main/java/com/daon/onjung/suggestion/application/dto/request/CreateCommentRequestDto.java new file mode 100644 index 0000000..c19b214 --- /dev/null +++ b/src/main/java/com/daon/onjung/suggestion/application/dto/request/CreateCommentRequestDto.java @@ -0,0 +1,9 @@ +package com.daon.onjung.suggestion.application.dto.request; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public record CreateCommentRequestDto( + @JsonProperty("content") + String content +) { +} diff --git a/src/main/java/com/daon/onjung/suggestion/application/service/CreateCommentService.java b/src/main/java/com/daon/onjung/suggestion/application/service/CreateCommentService.java new file mode 100644 index 0000000..719322c --- /dev/null +++ b/src/main/java/com/daon/onjung/suggestion/application/service/CreateCommentService.java @@ -0,0 +1,50 @@ +package com.daon.onjung.suggestion.application.service; + +import com.daon.onjung.account.domain.User; +import com.daon.onjung.account.repository.mysql.UserRepository; +import com.daon.onjung.core.exception.error.ErrorCode; +import com.daon.onjung.core.exception.type.CommonException; +import com.daon.onjung.suggestion.application.dto.request.CreateCommentRequestDto; +import com.daon.onjung.suggestion.application.usecase.CreateCommentUseCase; +import com.daon.onjung.suggestion.domain.Board; +import com.daon.onjung.suggestion.domain.Comment; +import com.daon.onjung.suggestion.domain.service.CommentService; +import com.daon.onjung.suggestion.repository.mysql.BoardRepository; +import com.daon.onjung.suggestion.repository.mysql.CommentRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.UUID; + +@Service +@RequiredArgsConstructor +public class CreateCommentService implements CreateCommentUseCase { + + private final UserRepository userRepository; + private final BoardRepository boardRepository; + private final CommentRepository commentRepository; + + private final CommentService commentService; + + @Override + @Transactional + public void execute(UUID accountId, Long boardId, CreateCommentRequestDto requestDto) { + + // 유저 조회 + User user = userRepository.findById(accountId) + .orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); + + // 게시글 조회 + Board board = boardRepository.findById(boardId) + .orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); + + // 댓글 생성 + Comment comment = commentService.createComment( + requestDto.content(), + user, + board + ); + commentRepository.save(comment); + } +} diff --git a/src/main/java/com/daon/onjung/suggestion/application/usecase/CreateCommentUseCase.java b/src/main/java/com/daon/onjung/suggestion/application/usecase/CreateCommentUseCase.java new file mode 100644 index 0000000..dc132e1 --- /dev/null +++ b/src/main/java/com/daon/onjung/suggestion/application/usecase/CreateCommentUseCase.java @@ -0,0 +1,11 @@ +package com.daon.onjung.suggestion.application.usecase; + +import com.daon.onjung.core.annotation.bean.UseCase; +import com.daon.onjung.suggestion.application.dto.request.CreateCommentRequestDto; + +import java.util.UUID; + +@UseCase +public interface CreateCommentUseCase { + void execute(UUID accountId, Long boardId, CreateCommentRequestDto requestDto); +} diff --git a/src/main/java/com/daon/onjung/suggestion/domain/service/CommentService.java b/src/main/java/com/daon/onjung/suggestion/domain/service/CommentService.java index 10b2ebe..4fb6ca4 100644 --- a/src/main/java/com/daon/onjung/suggestion/domain/service/CommentService.java +++ b/src/main/java/com/daon/onjung/suggestion/domain/service/CommentService.java @@ -1,7 +1,22 @@ package com.daon.onjung.suggestion.domain.service; +import com.daon.onjung.account.domain.User; +import com.daon.onjung.suggestion.domain.Board; +import com.daon.onjung.suggestion.domain.Comment; import org.springframework.stereotype.Service; @Service public class CommentService { + + public Comment createComment( + String content, + User user, + Board board + ) { + return Comment.builder() + .content(content) + .user(user) + .board(board) + .build(); + } }