Skip to content

Commit

Permalink
✨ Feature/#169 - 6.5 댓글 작성 API 구현 (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
dongkyeomjang authored Nov 26, 2024
1 parent 327c4a2 commit ab98218
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 1 deletion.
12 changes: 11 additions & 1 deletion http/suggestion/SuggestionControllerHttpRequest.http
Original file line number Diff line number Diff line change
Expand Up @@ -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
< /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}}"
}
Original file line number Diff line number Diff line change
@@ -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<Void> 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<Void> createComment(
@AccountID UUID accountId,
@PathVariable Long id,
@RequestBody @Valid CreateCommentRequestDto requestDto
) {
createCommentUseCase.execute(accountId, id, requestDto);
return ResponseDto.created(null);
}

}
Original file line number Diff line number Diff line change
@@ -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
) {
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit ab98218

Please sign in to comment.