From d26d57ec758db92eb95b5a2bf6cf6c223165e931 Mon Sep 17 00:00:00 2001 From: minyeong <106754293+miinyeong@users.noreply.github.com> Date: Thu, 16 Nov 2023 23:24:55 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20answer=20create=20API=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/answer/AnswerController.java | 5 +- .../entity/answer/AnswerGroupQuestionDTO.java | 32 ++++++++++++ .../server/service/answer/AnswerService.java | 51 +++++++++++++------ 3 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/onestep/server/entity/answer/AnswerGroupQuestionDTO.java diff --git a/src/main/java/com/onestep/server/controller/answer/AnswerController.java b/src/main/java/com/onestep/server/controller/answer/AnswerController.java index 865fcc0..b81bbbd 100644 --- a/src/main/java/com/onestep/server/controller/answer/AnswerController.java +++ b/src/main/java/com/onestep/server/controller/answer/AnswerController.java @@ -29,7 +29,7 @@ public class AnswerController { //답변 작성 @PostMapping(value = "/answer/create/{question_id}/{user_id}", consumes = {MediaType.APPLICATION_JSON_VALUE, "multipart/form-data"}) public String writeAnswer(@PathVariable Long question_id,@PathVariable String user_id, @RequestPart(value = "answerTxt") String answerTxt, @RequestPart(value = "img") MultipartFile img){ - Answer answer = null; + String answer = null; try{ log.info("imgTest={}",img); if(img.isEmpty()) { @@ -41,7 +41,8 @@ public String writeAnswer(@PathVariable Long question_id,@PathVariable String us }catch (IOException e){ e.printStackTrace(); } - return answer.getQuestion().getQuestion_id()+"번 질문에 대한 답변 작성이 완료되었습니다."; + + return answer; } //답변 읽기 diff --git a/src/main/java/com/onestep/server/entity/answer/AnswerGroupQuestionDTO.java b/src/main/java/com/onestep/server/entity/answer/AnswerGroupQuestionDTO.java new file mode 100644 index 0000000..f61bc50 --- /dev/null +++ b/src/main/java/com/onestep/server/entity/answer/AnswerGroupQuestionDTO.java @@ -0,0 +1,32 @@ +package com.onestep.server.entity.answer; + +import com.onestep.server.entity.Answer; +import com.onestep.server.entity.GroupQuestion; +import com.onestep.server.entity.Question; +import com.onestep.server.entity.User; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.Date; + +@Getter +@Setter +@NoArgsConstructor +public class AnswerGroupQuestionDTO { + + private String answer_txt; + private User user; + private Date answer_date; + private GroupQuestion groupQuestion; + + public Answer toEntity(){ + return Answer.builder() + .answer_id(null) + .user(user) + .answer_txt(answer_txt) + .write_date(answer_date) + .groupQuestion(groupQuestion) + .build(); + } +} diff --git a/src/main/java/com/onestep/server/service/answer/AnswerService.java b/src/main/java/com/onestep/server/service/answer/AnswerService.java index 2d531d5..ad36251 100644 --- a/src/main/java/com/onestep/server/service/answer/AnswerService.java +++ b/src/main/java/com/onestep/server/service/answer/AnswerService.java @@ -2,6 +2,7 @@ import com.onestep.server.entity.*; import com.onestep.server.entity.answer.AnswerDTO; +import com.onestep.server.entity.answer.AnswerGroupQuestionDTO; import com.onestep.server.entity.answer.AnswerReturnDTO; import com.onestep.server.entity.like.LikeAnswerClass; import com.onestep.server.entity.like.LikeAnswerDTO; @@ -29,26 +30,46 @@ public class AnswerService { private final ILikeAnswerRepository iLikeAnswerRepository; // 답변 작성 - public Answer writeAnswer(Long questionId, String userId,String answerTxt, String answerImg){ + public String writeAnswer(Long questionId, String userId,String answerTxt, String answerImg){ Optional optionalUser= iUserRepository.findById(userId); User user=optionalUser.get(); - Optional optionalQuestion = iQuestionRepository.findById(questionId); - Question question=optionalQuestion.get(); - - Date date = new Date(); - AnswerDTO answerDTO = new AnswerDTO(); - answerDTO.setQuestion(question); - answerDTO.setUser(user); - answerDTO.setAnswer_txt(answerTxt); - answerDTO.setAnswer_date(date); - //답변 저장 - Answer addAnswer = iAnswerRepository.save(answerDTO.toEntity()); - if(answerImg != "") { - addAnswer.setAnswer_img(answerImg); + Answer addAnswer = null; + Optional optionalQuestion = iQuestionRepository.findById(questionId); + if(optionalQuestion.isPresent()) { + Question question = optionalQuestion.get(); + Date date = new Date(); + AnswerDTO answerDTO = new AnswerDTO(); + answerDTO.setQuestion(question); + answerDTO.setUser(user); + answerDTO.setAnswer_txt(answerTxt); + answerDTO.setAnswer_date(date); + addAnswer = iAnswerRepository.save(answerDTO.toEntity()); + //답변 저장 + if(answerImg != "") { + addAnswer.setAnswer_img(answerImg); + } + + return addAnswer.getQuestion().getQuestion_id()+"번 질문에 대한 답변 작성이 완료되었습니다."; + } + Optional optionalGroupQuestion = iGroupQuestionRepository.findById(questionId); + if(optionalGroupQuestion.isPresent()){ + GroupQuestion groupQuestion = optionalGroupQuestion.get(); + Date date = new Date(); + AnswerGroupQuestionDTO answerGroupQuestionDTO = new AnswerGroupQuestionDTO(); + answerGroupQuestionDTO.setGroupQuestion(groupQuestion); + answerGroupQuestionDTO.setUser(user); + answerGroupQuestionDTO.setAnswer_txt(answerTxt); + answerGroupQuestionDTO.setAnswer_date(date); + addAnswer = iAnswerRepository.save(answerGroupQuestionDTO.toEntity()); + //답변 저장 + if(answerImg != "") { + addAnswer.setAnswer_img(answerImg); + } + return addAnswer.getGroupQuestion().getQuestion_id()+"번 질문에 대한 답변 작성이 완료되었습니다."; } - return addAnswer; + return "답변 작성 실패"; } //답변 읽기 public List readAnswer(Long questionId,String familyId){