Skip to content

Commit

Permalink
Merge pull request #98 from dongkyun0713/dongkyun
Browse files Browse the repository at this point in the history
지식인 서비스 수정
  • Loading branch information
dongkyun0713 authored Feb 26, 2024
2 parents b61def3 + 7dfba8e commit 040a888
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Question extends BaseEntity {
@JoinColumn(name = "author")
private User author;

@Setter
@Column(name = "status")
private Status status;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public static class Response {
@Schema(description = "사용자 레벨")
private String profile;

@Schema(description = "줄 포인트")
private Integer sendExperience;
public Response(Question question) {
this.id = question.getId();
this.authorId = question.getAuthor().getId();
Expand All @@ -114,6 +116,7 @@ public Response(Question question) {
this.updateDate = question.getUpdateDate();
this.level = question.getAuthor().getLevel();
this.profile = question.getAuthor().getProfile();
this.sendExperience = question.getSendExperience();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.example.titto_backend.common.exception.ErrorCode;
import com.example.titto_backend.questionBoard.domain.Answer;
import com.example.titto_backend.questionBoard.domain.Question;
import com.example.titto_backend.questionBoard.domain.Status;
import com.example.titto_backend.questionBoard.dto.AnswerDTO;
import com.example.titto_backend.questionBoard.repository.AnswerRepository;
import com.example.titto_backend.questionBoard.repository.QuestionRepository;
Expand Down Expand Up @@ -51,7 +52,7 @@ public AnswerDTO.Response save(AnswerDTO.Request request, Long questionId, Strin
//Update
@Transactional
public AnswerDTO.Response update(Long id, AnswerDTO.Request request, Long userId) throws CustomException {
validateAuthorIsLoggedInUser(id, userId);
validateAnswerAuthorIsLoggedInUser(id, userId);
Answer answer = answerRepository.findById(id)
.orElseThrow(() -> new CustomException(ErrorCode.ANSWER_NOT_FOUND));
answer.setContent(request.getContent());
Expand All @@ -61,25 +62,21 @@ public AnswerDTO.Response update(Long id, AnswerDTO.Request request, Long userId
// Delete
@Transactional
public void delete(Long id, Long userId) throws CustomException {
validateAuthorIsLoggedInUser(id, userId);
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
Integer updateCountAnswer = user.getCountAnswer() - 1;
user.setCountAnswer(updateCountAnswer);
validateAnswerAuthorIsLoggedInUser(id, userId);
answerRepository.deleteById(id);
}

@Transactional
public void acceptAnswer(Long questionId, Long answerId, Long userId) {
validateAuthorIsLoggedInUser(questionId, userId);
validateQuestionAuthorIsLoggedInUser(questionId, userId);
Question question = questionRepository.findById(questionId)
.orElseThrow(() -> new CustomException(ErrorCode.QUESTION_NOT_FOUND));
verifyAcceptedAnswer(questionId);
Answer answer = answerRepository.findById(answerId)
.orElseThrow(() -> new CustomException(ErrorCode.ANSWER_NOT_FOUND));
answer.setAccepted(true);
question.setAcceptedAnswer(answer);

question.setStatus(Status.valueOf("SOLVED"));
question.setAnswerAccepted(true); // 일단 임시 추가

Integer updateCountAccept = answer.getAuthor().getCountAccept() + 1;
Expand All @@ -94,7 +91,7 @@ private void verifyAcceptedAnswer(Long questionId) {
}
}

private void validateAuthorIsLoggedInUser(Long questionId, Long userId) {
private void validateQuestionAuthorIsLoggedInUser(Long questionId, Long userId) {
Question question = questionRepository.findById(questionId)
.orElseThrow(() -> new CustomException(ErrorCode.QUESTION_NOT_FOUND));
User user = userRepository.findById(userId)
Expand All @@ -104,4 +101,15 @@ private void validateAuthorIsLoggedInUser(Long questionId, Long userId) {
}
}

private void validateAnswerAuthorIsLoggedInUser(Long answerId, Long userId) {
Answer answer = answerRepository.findById(answerId)
.orElseThrow(() -> new CustomException(ErrorCode.QUESTION_NOT_FOUND));
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
if (!answer.getAuthor().equals(user)) {
throw new CustomException(ErrorCode.MISMATCH_AUTHOR);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public String save(String email, QuestionDTO.Request request) throws CustomExcep
.author(user)
.content(request.getContent())
.department(Department.valueOf(request.getDepartment().toUpperCase()))
.status(Status.valueOf(request.getStatus().toUpperCase()))
.status(Status.valueOf("UNSOLVED"))
.sendExperience(request.getSendExperience())
.viewCount(0)
.isAnswerAccepted(false)
Expand Down

0 comments on commit 040a888

Please sign in to comment.