Skip to content

Commit

Permalink
Merge pull request #102 from likelion-backendschool/refactor/#100-doc…
Browse files Browse the repository at this point in the history
…torqna-refactoring

Refactor/#100 doctorqna refactoring
  • Loading branch information
Shin-sangwon authored Apr 11, 2023
2 parents fe6822b + b1e9011 commit 38960db
Show file tree
Hide file tree
Showing 33 changed files with 601 additions and 183 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
//Junit
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.9.0'
testImplementation 'org.mockito:mockito-inline:4.6.1'

}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import com.codelion.animalcare.domain.doctorqna.dto.request.AnswerSaveRequestDto;
import com.codelion.animalcare.domain.doctorqna.dto.request.AnswerUpdateRequestDto;
import com.codelion.animalcare.domain.doctorqna.service.AnswerQueryService;
import com.codelion.animalcare.domain.doctorqna.entity.Question;
import com.codelion.animalcare.domain.doctorqna.service.AnswerCommandService;
import com.codelion.animalcare.domain.doctorqna.service.AnswerQueryService;
import com.codelion.animalcare.domain.doctorqna.service.QuestionQueryService;
import com.codelion.animalcare.domain.doctorqna.service.QuestionCommandService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
Expand All @@ -30,25 +30,30 @@ public class AnswerController {

//답변 작성
@PostMapping("/usr/doctor-qna/{questionId}/answers/write")
public String save(Model model, @PathVariable Long questionId, @Valid AnswerSaveRequestDto answerSaveRequestDto, BindingResult bindingResult, Principal principal){
public String save(Model model, @PathVariable Long questionId,
@Valid AnswerSaveRequestDto answerForm, BindingResult bindingResult, Principal principal) {

if(bindingResult.hasErrors()) {
if (bindingResult.hasErrors()) {
model.addAttribute("question", questionQueryService.findById(questionId));
return "doctorqna/doctorQnaDetail";
}

// if(!answerService.isDoctor(principal)) {
// throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "의사만 답변을 작성할 수 있습니다.");
// }
Question question = questionQueryService.findQuestionByQuestionId(questionId);
AnswerSaveRequestDto answerSaveRequestDto = new AnswerSaveRequestDto(answerForm.content(),
question);

answerCommandService.save(questionId, answerSaveRequestDto, principal);
return "redirect:/usr/doctor-qna/%d".formatted(questionId);
}

@GetMapping("/usr/doctor-qna/{questionId}/answers/{answerId}/modify")
public String modify(Model model, @PathVariable Long questionId, @PathVariable Long answerId, AnswerUpdateRequestDto answerUpdateRequestDto, Principal principal){
public String modify(Model model, @PathVariable Long questionId, @PathVariable Long answerId,
AnswerUpdateRequestDto answerUpdateRequestDto, Principal principal) {

if(answerQueryService.answerAuthorized(answerId, principal)){
if (answerQueryService.answerAuthorized(answerId, principal)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "수정권한이 없습니다.");
}

Expand All @@ -58,13 +63,15 @@ public String modify(Model model, @PathVariable Long questionId, @PathVariable L
}

@PostMapping("/usr/doctor-qna/{questionId}/answers/{answerId}/modify")
public String modify(@PathVariable Long questionId, @PathVariable Long answerId, @Valid AnswerUpdateRequestDto answerUpdateRequestDto, BindingResult bindingResult, Principal principal){
public String modify(@PathVariable Long questionId, @PathVariable Long answerId,
@Valid AnswerUpdateRequestDto answerUpdateRequestDto, BindingResult bindingResult,
Principal principal) {

if(bindingResult.hasErrors()) {
if (bindingResult.hasErrors()) {
return "doctorqna/doctorQnaAnswerModifyForm";
}

if(answerQueryService.answerAuthorized(answerId, principal)){
if (answerQueryService.answerAuthorized(answerId, principal)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "수정권한이 없습니다.");
}

Expand All @@ -74,10 +81,11 @@ public String modify(@PathVariable Long questionId, @PathVariable Long answerId,

//답변 삭제
@GetMapping("/usr/doctor-qna/{questionId}/answers/{answerId}/delete")
public String delete(@PathVariable Long questionId, @PathVariable Long answerId, Principal principal) {
public String delete(@PathVariable Long questionId, @PathVariable Long answerId,
Principal principal) {

if(answerQueryService.answerAuthorized(answerId, principal)){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "수정권한이 없습니다.");
if (answerQueryService.answerAuthorized(answerId, principal)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "삭제권한이 없습니다.");
}

answerCommandService.delete(answerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.codelion.animalcare.domain.doctorqna.dto.request.QuestionSaveRequestDto;
import com.codelion.animalcare.domain.doctorqna.dto.request.QuestionUpdateRequestDto;
import com.codelion.animalcare.domain.doctorqna.entity.Question;
import com.codelion.animalcare.domain.doctorqna.service.QuestionQueryService;
import com.codelion.animalcare.domain.doctorqna.entity.QuestionHashtag;
import com.codelion.animalcare.domain.doctorqna.service.QuestionCommandService;
import com.codelion.animalcare.domain.doctorqna.service.QuestionHashtagService;
import com.codelion.animalcare.domain.doctorqna.service.QuestionQueryService;
import com.codelion.animalcare.domain.user.entity.UserInfo;
import com.codelion.animalcare.domain.user.service.UserService;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,51 +27,55 @@
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.security.Principal;
import java.util.List;


//TODO : 2022/04/06 -> 참조하지 않는 해시태그는? (배치?), 게시물 수정할때 해시태그도 수정 가능하게 하기, 해시태그 누르면 리스트 띄우기, ...
@RequiredArgsConstructor
@Controller
public class QuestionController {

private final QuestionCommandService questionCommandService;
private final QuestionQueryService questionQueryService;
private final UserService userService;
private final QuestionHashtagService questionHashtagService;

//게시글 등록 화면
@GetMapping("/usr/doctor-qna/write")
public String saveForm(QuestionSaveRequestDto questionSaveRequestDto){
public String saveForm(QuestionSaveRequestDto questionSaveRequestDto) {
return "doctorqna/doctorQnaQuestionForm";
}

//게시글 등록
@PostMapping("/usr/doctor-qna/write")
public String save(@Valid QuestionSaveRequestDto questionSaveRequestDto, BindingResult bindingResult, Principal principal) {
public String save(@Valid QuestionSaveRequestDto questionSaveRequestDto,
BindingResult bindingResult, Principal principal) {

if(bindingResult.hasErrors()) {
if (bindingResult.hasErrors()) {
return "doctorqna/doctorQnaQuestionForm";
}

questionCommandService.save(questionSaveRequestDto, principal);

return "redirect:/usr/doctor-qna";
}

//개별 조회
@GetMapping("/usr/doctor-qna/{id}")
public String findById(Model model, @PathVariable Long id, AnswerSaveRequestDto answerSaveRequestDto, HttpServletRequest request, HttpServletResponse response, Principal principal){
public String findById(Model model, @PathVariable Long id,
AnswerSaveRequestDto answerSaveRequestDto, HttpServletRequest request,
HttpServletResponse response, Principal principal) {
// 조회수 쿠키로 중복방지
Cookie oldCookie = null;
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(var cookie : cookies) {
if(cookie.getName().equals("DoctorQnaView")) {
if (cookies != null) {
for (var cookie : cookies) {
if (cookie.getName().equals("DoctorQnaView")) {
oldCookie = cookie;
}
}
}

if(oldCookie != null) {
if(!oldCookie.getValue().contains("[" + id.toString() + "]")) {
if (oldCookie != null) {
if (!oldCookie.getValue().contains("[" + id.toString() + "]")) {
questionCommandService.updateView(id);
oldCookie.setValue(oldCookie.getValue() + "_[" + id + "]");
oldCookie.setPath("/");
Expand All @@ -90,7 +96,7 @@ public String findById(Model model, @PathVariable Long id, AnswerSaveRequestDto

UserInfo user = userService.getUserInfo(principal.getName()).orElse(null);

if(user != null) { // 로그인 한 사용자라면
if (user != null) { // 로그인 한 사용자라면
model.addAttribute("login_id", user.getId());

like = questionQueryService.findLike(id, user); // 로그인 유저의 추천 여부 확인
Expand All @@ -99,40 +105,54 @@ public String findById(Model model, @PathVariable Long id, AnswerSaveRequestDto

model.addAttribute("like", like);

List<QuestionHashtag> hashtags = questionHashtagService.findHashtagListByQuestion(
questionQueryService.findQuestionByQuestionId(id));
model.addAttribute("hashtags", hashtags);
return "doctorqna/doctorQnaDetail";
}

//전체 조회
@GetMapping("/usr/doctor-qna")
public String findAll(Model model, @RequestParam(value="page", defaultValue="0") int page, String type, String kw) {
public String findAll(Model model, @RequestParam(value = "page", defaultValue = "0") int page,
String type, String kw, String hashtag) {

Page<Question> paging = questionQueryService.findAll(page, type, kw);
Page<Question> paging;

if(hashtag != null && !hashtag.isEmpty()) {
paging = questionHashtagService.findAllByHashtag(page, hashtag);
}else {
paging = questionQueryService.findAll(page, type, kw);
}

model.addAttribute("paging", paging);
model.addAttribute("kw", kw);
model.addAttribute("type", type);


return "doctorqna/doctorQnaList";
}

@GetMapping("/usr/doctor-qna/{id}/modify")
public String update(Model model, @PathVariable Long id, QuestionUpdateRequestDto questionUpdateRequestDto, Principal principal){
public String update(Model model, @PathVariable Long id,
QuestionUpdateRequestDto questionUpdateRequestDto, Principal principal) {

if(questionQueryService.questionAuthorized(id, principal)){
if (questionQueryService.questionAuthorized(id, principal)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "수정권한이 없습니다.");
}

model.addAttribute("question", questionQueryService.findById(id));
return "doctorqna/doctorQnaQuestionModifyForm";
}

@PostMapping("/usr/doctor-qna/{id}/modify")
public String update(@PathVariable Long id, @Valid QuestionUpdateRequestDto questionUpdateRequestDto, BindingResult bindingResult, Principal principal){
public String update(@PathVariable Long id,
@Valid QuestionUpdateRequestDto questionUpdateRequestDto, BindingResult bindingResult,
Principal principal) {

if(bindingResult.hasErrors()) {
if (bindingResult.hasErrors()) {
return "doctorqna/doctorQnaQuestionModifyForm";
}

if(questionQueryService.questionAuthorized(id, principal)){
if (questionQueryService.questionAuthorized(id, principal)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "수정권한이 없습니다.");
}

Expand All @@ -142,9 +162,9 @@ public String update(@PathVariable Long id, @Valid QuestionUpdateRequestDto ques
}

@GetMapping("/usr/doctor-qna/{id}/delete")
public String delete(@PathVariable Long id, Principal principal){
public String delete(@PathVariable Long id, Principal principal) {

if(questionQueryService.questionAuthorized(id, principal)){
if (questionQueryService.questionAuthorized(id, principal)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "삭제권한이 없습니다.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,27 @@
import com.codelion.animalcare.domain.doctorqna.entity.Question;
import com.codelion.animalcare.domain.user.entity.Doctor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotEmpty;

@Getter
@Setter
@NoArgsConstructor
public class AnswerSaveRequestDto {

public record AnswerSaveRequestDto(
@NotEmpty(message = "내용은 필수 입력 항목입니다.")
private String content;
@Setter
private Question question;
String content,

Question question
) {

@Builder
public AnswerSaveRequestDto(String content, Question question){
public AnswerSaveRequestDto(String content, Question question) {
this.content = content;
this.question = question;
}

public Answer toEntity(Doctor doctor) {
return Answer.builder()
.content(content)
.question(question)
.doctor(doctor)
.build();
.content(content)
.question(question)
.doctor(doctor)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package com.codelion.animalcare.domain.doctorqna.dto.request;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotBlank;

@Getter
@Setter
@NoArgsConstructor
public class AnswerUpdateRequestDto {

public record AnswerUpdateRequestDto(
@NotBlank(message = "내용은 필수 입력 항목입니다.")
private String content;
String content
) {

@Builder
public AnswerUpdateRequestDto(String content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.List;

public record QuestionSaveRequestDto(
@NotBlank(message = "제목은 필수 입력 사항입니다.")
@Size(max = 200, message = "제목이 너무 길어요.")
String title,
@NotBlank(message = "제목은 필수 입력 사항입니다.")
@Size(max = 200, message = "제목이 너무 길어요.")
String title,

@NotBlank(message = "내용은 필수 입력 사항입니다.") String content
@NotBlank(message = "내용은 필수 입력 사항입니다.")
String content,

List<String> hashtags
) {

public Question toEntity(Member member) {
return Question.builder()
.title(title())
.content(content())
.member(member)
.build();
.title(title())
.content(content())
.member(member)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package com.codelion.animalcare.domain.doctorqna.dto.request;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

@Getter
@Setter
@NoArgsConstructor
public class QuestionUpdateRequestDto {
public record QuestionUpdateRequestDto(
@NotBlank(message = "제목은 필수 항목 입니다.")
@Size(max = 200)
private String title;
String title,

@NotBlank(message = "내용은 필수 항목 입니다.")
private String content;
String content
) {

@Builder
public QuestionUpdateRequestDto(String title, String content) {
Expand Down
Loading

0 comments on commit 38960db

Please sign in to comment.