Skip to content

Commit

Permalink
Fix: answer create API 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
miinyeong committed Nov 16, 2023
1 parent f026615 commit d26d57e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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;
}

//답변 읽기
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
51 changes: 36 additions & 15 deletions src/main/java/com/onestep/server/service/answer/AnswerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<User> optionalUser= iUserRepository.findById(userId);
User user=optionalUser.get();
Optional<Question> 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<Question> 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<GroupQuestion> 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<AnswerReturnDTO> readAnswer(Long questionId,String familyId){
Expand Down

0 comments on commit d26d57e

Please sign in to comment.