Skip to content

Commit

Permalink
fix: answers requiring authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtftr committed Nov 24, 2024
1 parent b70f716 commit 8f28da4
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ public CreateAnswerResponseDto updateAnswer(Long answerId, CreateAnswerRequestDt
.build();
}

public GetAnswersResponseDto getAnswersForQuestion(Long questionId) throws UnauthorizedAccessException {
public GetAnswersResponseDto getAnswersForQuestion(Long questionId) {
Question question = questionService.findById(questionId).orElseThrow();
User currentUser = userContextService.getCurrentUser();
final User currentUser = getCurrentUserOrNull();

return GetAnswersResponseDto.builder()
.items(question.getAnswers().stream().map(answer -> GetAnswersResponseDto.AnswerResponseDto.builder()
.id(answer.getId())
Expand All @@ -90,9 +91,17 @@ public GetAnswersResponseDto getAnswersForQuestion(Long questionId) throws Unaut
.updatedAt(answer.getUpdatedAt())
.upvoteCount(answer.getUpvoteCount())
.downvoteCount(answer.getDownvoteCount())
.selfAnswer(currentUser!=null && currentUser.getId().equals(answer.getAnsweredBy().getId()))
.selfAnswer(currentUser != null && currentUser.getId().equals(answer.getAnsweredBy().getId()))
.build()).toList())
.totalItems(question.getAnswers().size())
.build();
}

private User getCurrentUserOrNull() {
try {
return userContextService.getCurrentUser();
} catch (UnauthorizedAccessException e) {
return null;
}
}
}

0 comments on commit 8f28da4

Please sign in to comment.