이제 배포환경 구축이 끝났으니! 내가 맡은 다음 작업을 진행해야한다.
내가 맡은 다음 작업은 댓글 CRUD기능 구현이었다.
jpa를 배우면서 연관관계매핑하는게 가장 어려웠는데, 이번에 프로젝트를 하며 좀 많이 이해되어서 뿌듯했다.
정말 코딩은 직접 해봐야한다...!
댓글 기능을 구현할 때 Question과 Member 기능이 거의 다 구현되어있어서 바로 작업을 시작했다.
이 글에서는 API 계층까지 구현한 내용을 정리하려한다.
@RestController
@RequestMapping("/questions/{question-id}/comments")
@Validated
public class CommentController {
private final static String REPLY_DEFAULT_URL = "/questions/{question-id}/comments";
private final CommentService commentService;
private final CommentMapper mapper;
public CommentController(CommentService commentService, CommentMapper mapper) {
this.commentService = commentService;
this.mapper = mapper;
}
//질문에 대한 답변을 등록
@PostMapping
public ResponseEntity postComment(@RequestBody @Valid CommentPostDto commentPostDto,
@PathVariable("question-id") @Positive long questionId){
commentPostDto.setQuestionId(questionId);
Comment postComment = mapper.commentPostDtoToComment(commentPostDto);
Comment response = commentService.createComment(postComment);
URI location = UriComponentsBuilder.newInstance()
.path(REPLY_DEFAULT_URL+"/{comment-id}")
.buildAndExpand(questionId,response.getCommentId())
.toUri();
return ResponseEntity.created(location).build();
}
//답변 수정
@PatchMapping("/{comment-id}")
public ResponseEntity patchComment (@PathVariable("question-id") @Positive long questionId,
@PathVariable("comment-id") @Positive long commentId,
@RequestBody @Valid CommentPatchDto commentPatchDto){
commentPatchDto.setCommentId(commentId);
Comment patchComment = mapper.commentPatchDtoToComment(commentPatchDto);
Comment response = commentService.updateComment(patchComment);
return new ResponseEntity<>(mapper.commentToCommentResponseToDto(response), HttpStatus.OK);
}
//답변 조회
@GetMapping
public ResponseEntity getComments (@PathVariable("question-id") @Positive long questionId){
List<Comment> comment = (List<Comment>) commentService.findCommentAll(questionId);
return new ResponseEntity<>(mapper.commentsToCommentsResponseDto(comment), HttpStatus.OK);
}
//답변 삭제
@DeleteMapping("/{comment-id}")
public ResponseEntity deleteComment (@PathVariable("question-id") @Positive long questionId,
@PathVariable("comment-id") @Positive long commentId){
commentService.deleteComment(questionId, commentId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
ackage com.stackoverflow.Server.comment.dto;
import com.stackoverflow.Server.member.entity.Member;
import com.stackoverflow.Server.question.entity.Question;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.validation.constraints.NotEmpty;
@Getter
@Setter
@NoArgsConstructor
public class CommentPatchDto {
private Long commentId;
@NotEmpty(message = "답변 내용을 작성해주세요.")
private String commentBody;
}
package com.stackoverflow.Server.comment.dto;
import com.stackoverflow.Server.comment.entity.Comment;
import com.stackoverflow.Server.member.entity.Member;
import com.stackoverflow.Server.question.entity.Question;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.validation.constraints.NotEmpty;
@Getter
@Setter
@NoArgsConstructor
public class CommentPostDto {
private Long questionId;
private Long memberId;
private String nickname;
@NotEmpty(message = "답변 내용을 작성해주세요.")
private String commentBody;
public Member getMember(){
Member member = new Member();
member.setMemberId(memberId);
member.setNickname(nickname);
return member;
}
public Question getQuestion (){
Question question = new Question();
question.setQuestionId(questionId);
return question;
}
}
아침에 출근해서 남 블로그 읽는게 개꿀...쿄쿄쿄