diff --git a/server/src/main/java/com/yogit/server/board/controller/CommenrController.java b/server/src/main/java/com/yogit/server/board/controller/CommenrController.java new file mode 100644 index 0000000..0ebe4c8 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/controller/CommenrController.java @@ -0,0 +1,40 @@ +package com.yogit.server.board.controller; + +import com.yogit.server.board.dto.request.comment.CreateCommentReq; +import com.yogit.server.board.dto.response.comment.CommentRes; +import com.yogit.server.board.service.comment.CommentService; +import com.yogit.server.global.dto.ApplicationResponse; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Slf4j +@RestController +@RequiredArgsConstructor // private final DI의존주입 +@RequestMapping("/comments") +public class CommenrController { + + private final CommentService commentService; + /** + * 코멘트 등록 + * @author 토마스 + */ + @ApiOperation(value = "코멘트 등록", notes = "코멘트 내용을 입력해 등록 요청.") + @ApiResponses({ + @ApiResponse(code= 201, message = "요청에 성공하였습니다."), + @ApiResponse(code= 404, message = "존재하지 않는 유저입니다."), + @ApiResponse(code= 404, message = "존재하지 않는 클립보드입니다."), + @ApiResponse(code = 4000 , message = "서버 오류입니다.") + }) + @PostMapping("") + public ApplicationResponse createComment(@RequestBody @Validated CreateCommentReq createCommentReq){ + return commentService.createComment(createCommentReq); + } +} diff --git a/server/src/main/java/com/yogit/server/board/dto/request/comment/CreateCommentReq.java b/server/src/main/java/com/yogit/server/board/dto/request/comment/CreateCommentReq.java new file mode 100644 index 0000000..955ccab --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/dto/request/comment/CreateCommentReq.java @@ -0,0 +1,26 @@ +package com.yogit.server.board.dto.request.comment; + +import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiParam; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotBlank; + +@Data +@NoArgsConstructor +public class CreateCommentReq { + + @ApiModelProperty(example = "1") + @ApiParam(value = "유저 ID", required = true) + private Long userId; + + @ApiModelProperty(example = "1") + @ApiParam(value = "ClipBoard ID", required = true) + private Long clipBoardId; + + @ApiModelProperty(example = "경복궁역 몇 번 출구인가요?") + @ApiParam(value = "클립보드 상세 내용", required = true) + @NotBlank + private String content; +} diff --git a/server/src/main/java/com/yogit/server/board/entity/Comment.java b/server/src/main/java/com/yogit/server/board/entity/Comment.java index 8697e55..5930884 100644 --- a/server/src/main/java/com/yogit/server/board/entity/Comment.java +++ b/server/src/main/java/com/yogit/server/board/entity/Comment.java @@ -1,5 +1,6 @@ package com.yogit.server.board.entity; +import com.yogit.server.board.dto.request.comment.CreateCommentReq; import com.yogit.server.config.domain.BaseEntity; import com.yogit.server.user.entity.User; import lombok.AccessLevel; @@ -26,4 +27,14 @@ public class Comment extends BaseEntity { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "clip_board_id") private ClipBoard clipBoard; + + /* + 연관관계 편의 메소드 + */ + + public Comment(CreateCommentReq dto, User user, ClipBoard clipBoard) { + this.content = dto.getContent(); + this.user = user; + this.clipBoard = clipBoard; + } } diff --git a/server/src/main/java/com/yogit/server/board/service/comment/CommentService.java b/server/src/main/java/com/yogit/server/board/service/comment/CommentService.java new file mode 100644 index 0000000..d4b4bf3 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/service/comment/CommentService.java @@ -0,0 +1,10 @@ +package com.yogit.server.board.service.comment; + +import com.yogit.server.board.dto.request.comment.CreateCommentReq; +import com.yogit.server.board.dto.response.comment.CommentRes; +import com.yogit.server.global.dto.ApplicationResponse; + +public interface CommentService { + + ApplicationResponse createComment(CreateCommentReq createCommentReq); +} diff --git a/server/src/main/java/com/yogit/server/board/service/comment/CommentServiceImpl.java b/server/src/main/java/com/yogit/server/board/service/comment/CommentServiceImpl.java new file mode 100644 index 0000000..f48e083 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/service/comment/CommentServiceImpl.java @@ -0,0 +1,46 @@ +package com.yogit.server.board.service.comment; + +import com.yogit.server.board.dto.request.comment.CreateCommentReq; +import com.yogit.server.board.dto.response.comment.CommentRes; +import com.yogit.server.board.entity.ClipBoard; +import com.yogit.server.board.entity.Comment; +import com.yogit.server.board.exception.clipboard.NotFoundClipBoardException; +import com.yogit.server.board.repository.ClipBoardRepository; +import com.yogit.server.board.repository.CommentRepository; +import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.user.entity.User; +import com.yogit.server.user.exception.NotFoundUserException; +import com.yogit.server.user.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class CommentServiceImpl implements CommentService{ + + private final CommentRepository commentRepository; + private final UserRepository userRepository; + private final ClipBoardRepository clipBoardRepository; + + + + @Transactional(readOnly = false) + @Override + public ApplicationResponse createComment(CreateCommentReq dto){ + + User user = userRepository.findById(dto.getUserId()) + .orElseThrow(() -> new NotFoundUserException()); + + ClipBoard clipBoard = clipBoardRepository.findClipBoardById(dto.getClipBoardId()) + .orElseThrow(() -> new NotFoundClipBoardException()); + + Comment comment = new Comment(dto, user, clipBoard); + Comment savedComment = commentRepository.save(comment); + CommentRes commentRes = CommentRes.toDto(savedComment); + return ApplicationResponse.create("코멘트 생성을 성공했습니다.", commentRes); + } + + +}