우선 댓글의 객체를 어떻게 만들지 생각해야한다.
Coment 객체에 들어가야 할 것이 뭐가 있을까?
Coment 객체
@Entity
@NoArgsConstructor
@Getter
public class Coment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(columnDefinition = "TEXT")
private String content;
private LocalDateTime createDate;
private LocalDateTime modifyDate;
@ManyToOne(fetch = FetchType.LAZY)
private Question question;
@ManyToOne(fetch = FetchType.LAZY)
private Answer answer;
@ManyToOne(fetch = FetchType.LAZY)
private SiteUser author;
public Coment(String content, LocalDateTime createDate,SiteUser author) {
this.content = content;
this.createDate = createDate;
this.author = author;
}
public void AddQuestion(Question question){
this.question = question;
}
public void AddAnswer(Answer answer){
this.answer =answer;
}
public void updateComent(String content){
this.content = content;
}
}
answer 객체에 추가된 내용
@JsonIgnore
@OneToMany(mappedBy = "answer" ,cascade = CascadeType.REMOVE)
private List<Coment> comentList = new ArrayList<>();
Question객체에도 같은 방식으로 추가
@JsonIgnore
@OneToMany(mappedBy = "question",cascade = CascadeType.REMOVE)
private List<Coment> comentList = new ArrayList<>();
ComentController
@Controller
@RequiredArgsConstructor
@RequestMapping("/coment")
public class ComentController {
private final QuestionService questionService;
private final AnswerService answerService;
private final ComentService comentService;
private final UserService userService;
@PreAuthorize("isAuthenticated()")
@GetMapping("/create/question/{id}")
public String createQuestionComent(ComentForm comentForm) {
return "comment_form";
}
//질문 에 댓글 달때 사용
//
@PreAuthorize("isAuthenticated()")
@PostMapping("/create/question/{id}")
public String createQuestionComent(Model model, @PathVariable("id") Integer id,
@Valid ComentForm comentForm, BindingResult bindingResult,
Principal principal) {
Question question = questionService.getQuestion(id);
SiteUser user = userService.getUser(principal.getName());
if (bindingResult.hasErrors()) {
model.addAttribute("question", question);
return "question_detail";
}
comentService.createQuestionComent(question, comentForm.getContent(), user);
return String.format("redirect:/question/detail/%s", id);
}
@PreAuthorize("isAuthenticated()")
@GetMapping("/create/answer/{id}")
public String createAnswerComent(ComentForm comentForm) {
return "comment_form";
}
@PreAuthorize("isAuthenticated()")
@PostMapping("/create/answer/{id}")
public String createAnswerComent(Model model, @PathVariable("id") Integer id,
@Valid ComentForm comentForm, BindingResult bindingResult,
Principal principal) {
Answer answer = answerService.getAnswer(id);
SiteUser user = userService.getUser(principal.getName());
if (bindingResult.hasErrors()) {
model.addAttribute("question", answer.getQuestion());
return "question_detail";
}
comentService.createAnswerComent(answer, comentForm.getContent(), user);
return String.format("redirect:/question/detail/%s", answer.getQuestion().getId());
}
}
질문과 답변에 댓글을 추가하는 방식까지 먼저 진행 했고 여기서 추가적으로 해야할 것은 수정과 삭제를 추가로 진행할 예정이다.