Skip to content

Commit

Permalink
[feat/fix] 문의 답변 수정&삭제 + 문의 삭제시 답변도 함께 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
minji1289 committed Jan 10, 2024
1 parent 174f50c commit e7e6613
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,18 @@ public AnswerResponseDto createAnswer(@PathVariable Long project_id, @PathVariab
return questionService.createAnswer(project_id, question_id, requestDto);
}

// 문의 답변 수정 (판매자만 가능)
@PatchMapping("/{project_id}/question/{question_id}/answer")
public ResponseEntity updateAnswer(@PathVariable Long project_id, @PathVariable Long question_id, @RequestBody AnswerRequestDto requestDto)
{
return questionService.updateAnswer(project_id, question_id, requestDto);
}

//문의 답변 삭제 (판매자만 가능)
@DeleteMapping("/{project_id}/question/{question_id}/answer")
public ResponseEntity deleteAnswer(@PathVariable Long project_id, @PathVariable Long question_id)
{
return questionService.deleteAnswer(project_id, question_id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,14 @@ public ResponseEntity deleteQuestion(Long question_id) {
User user = userRepository.findByEmail(SecurityUtil.getLoginUsername())
.orElseThrow(()->new ResponseStatusException(HttpStatus.BAD_REQUEST));
Question question = questionRepository.findByQuestion_Id(question_id);
//판매자만 공지 삭제 가능
//판매자만 문의 삭제 가능
if (user.getEmail() == question.getUser().getEmail()){
//문의 삭제시 문의 답변글도 함께 삭제
if (answerRepository.existsByQuestionId(question_id)) //문의 답변이 존재하는 경우
{
Answer answer = answerRepository.findByQuestionId(question_id);
answerRepository.deleteById(answer.getId());
}
questionRepository.deleteById(question_id);
return new ResponseEntity(HttpStatus.OK);
}
Expand Down Expand Up @@ -157,8 +163,35 @@ public AnswerResponseDto createAnswer(Long project_id, Long question_id, AnswerR

return answerResponseDto;
}

return null;
}

//문의 답변 수정 (판매자만 가능)
public ResponseEntity updateAnswer(Long project_id, Long question_id, AnswerRequestDto requestDto) {
User user = userRepository.findByEmail(SecurityUtil.getLoginUsername())
.orElseThrow(()->new ResponseStatusException(HttpStatus.BAD_REQUEST));
Project project = projectRepository.findByProject_Id(project_id);
//판매자만 문의 답변 수정 가능
if (user.getEmail() == project.getUser().getEmail()){
Answer answer = answerRepository.findByQuestionId(question_id);
answer.update(requestDto.getContent());
answerRepository.save(answer);
return new ResponseEntity(HttpStatus.OK);
}
else return new ResponseEntity(HttpStatus.BAD_REQUEST); //판매자 아닐 경우
}

//문의 답변 삭제 (판매자만 가능)
public ResponseEntity deleteAnswer(Long project_id, Long question_id) {
User user = userRepository.findByEmail(SecurityUtil.getLoginUsername())
.orElseThrow(()->new ResponseStatusException(HttpStatus.BAD_REQUEST));
Project project = projectRepository.findByProject_Id(project_id);
//판매자만 문의 답변 삭제 가능
if (user.getEmail() == project.getUser().getEmail()){
Answer answer = answerRepository.findByQuestionId(question_id);
answerRepository.deleteById(answer.getId());
return new ResponseEntity(HttpStatus.OK);
}
else return new ResponseEntity(HttpStatus.BAD_REQUEST); //판매자 아닐 경우
}
}
4 changes: 4 additions & 0 deletions src/main/java/wowmarket/wow_server/domain/Answer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ public Answer(Project project, Question question, User user, AnswerRequestDto re
this.content = requestDto.getContent();
}

public void update(String content) {
this.content = content;
}

}

0 comments on commit e7e6613

Please sign in to comment.