Skip to content

Commit

Permalink
#27 feat: Comment 생성 API구현
Browse files Browse the repository at this point in the history
  • Loading branch information
xhaktmchl committed Nov 3, 2022
1 parent 4478ecb commit 7a37490
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<CommentRes> createComment(@RequestBody @Validated CreateCommentReq createCommentReq){
return commentService.createComment(createCommentReq);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions server/src/main/java/com/yogit/server/board/entity/Comment.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<CommentRes> createComment(CreateCommentReq createCommentReq);
}
Original file line number Diff line number Diff line change
@@ -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<CommentRes> 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);
}


}

0 comments on commit 7a37490

Please sign in to comment.