Skip to content

Commit

Permalink
Merge pull request #185 from dnd-side-project/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
eun-seong authored Aug 19, 2024
2 parents b56b133 + 196eebe commit d0ac00c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@Getter
@Builder
@Document("dashboards")
@CompoundIndex(def = "{ 'user': 1, 'period': 1, 'relation': 1 }", unique = true)
@CompoundIndex(def = "{ 'user': 1, 'period': 1, 'relation': 1, 'wikiType': 1 }", unique = true)
public class Dashboard extends BaseTimeEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ public ResponseEntity<?> getAnswersByQuestion(
@RequestParam(name = "period", required = false, defaultValue = "TOTAL") Period period,
@RequestParam(name = "relation", required = false, defaultValue = "TOTAL") Relation relation,
@RequestParam(name = "pageNo", required = false, defaultValue = "0") int pageNo,
@RequestParam(name = "pageSize", required = false, defaultValue = "20") int pageSize) {

var answersByQuestion = surveyService.getAnswersByQuestion(tokenUserInfoDto.getWikiId(), questionId, period, relation, pageNo, pageSize);
@RequestParam(name = "pageSize", required = false, defaultValue = "20") int pageSize
) {
var answersByQuestion = surveyService.getAnswersByQuestion(
tokenUserInfoDto.getWikiId(), questionId,
period, relation,
pageNo, pageSize
);
return ResponseDto.ok(answersByQuestion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public interface SurveyRepository extends MongoRepository<Survey, String> {
Page<Survey> findBySenderAndPeriod(User sender, Period period, Pageable pageable);

Page<Survey> findBySenderAndRelation(User sender, Relation relation, Pageable pageable);

Long countByOwnerAndWikiType(User owner, WikiType wikiType);
}
35 changes: 18 additions & 17 deletions src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.dnd.namuiwiki.common.dto.PageableDto;
import com.dnd.namuiwiki.common.exception.ApplicationErrorException;
import com.dnd.namuiwiki.common.exception.ApplicationErrorType;
import com.dnd.namuiwiki.domain.jwt.JwtProvider;
import com.dnd.namuiwiki.domain.jwt.JwtService;
import com.dnd.namuiwiki.domain.jwt.dto.TokenUserInfoDto;
import com.dnd.namuiwiki.domain.option.OptionRepository;
Expand Down Expand Up @@ -164,7 +163,11 @@ private void validateSurveyOwner(Survey survey, User user) {
}
}

public GetAnswersByQuestionResponse getAnswersByQuestion(String wikiId, String questionId, Period period, Relation relation, int pageNo, int pageSize) {
public GetAnswersByQuestionResponse getAnswersByQuestion(
String wikiId, String questionId,
Period period, Relation relation,
int pageNo, int pageSize
) {
validateFilter(period, relation);

Question question = getQuestionById(questionId);
Expand All @@ -173,21 +176,19 @@ public GetAnswersByQuestionResponse getAnswersByQuestion(String wikiId, String q
Sort sort = Sort.by(Sort.Direction.DESC, "createdAt");
Pageable pageable = PageRequest.of(pageNo, pageSize, sort);
Page<Survey> surveys = getReceivedSurveysByFilter(period, relation, owner, pageable);
var answers = surveys.map(survey -> {
var answerOfQuestion = survey.getAnswers().stream()
.filter(answer -> answer.getQuestion().getId().equals(questionId))
.findAny()
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_QUESTION_ID));
return SingleAnswerWithSurveyDetailDto.builder()
.senderName(survey.getSenderName())
.period(survey.getPeriod())
.relation(survey.getRelation())
.createdAt(survey.getWrittenAt())
.answer(convertAnswer(question, answerOfQuestion))
.reason(answerOfQuestion.getReason())
.optionName(question, answerOfQuestion)
.build();
});
var answers = surveys.map(survey -> survey.getAnswers().stream()
.filter(answer -> answer.getQuestion().getId().equals(questionId))
.findAny()
.map(answer -> SingleAnswerWithSurveyDetailDto.builder()
.senderName(survey.getSenderName())
.period(survey.getPeriod())
.relation(survey.getRelation())
.createdAt(survey.getWrittenAt())
.answer(convertAnswer(question, answer))
.reason(answer.getReason())
.optionName(question, answer)
.wikiType(survey.getWikiType())
.build()).orElse(null));

return new GetAnswersByQuestionResponse(question.getTitle(), question.getName(), PageableDto.create(answers));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.dnd.namuiwiki.domain.survey.model.entity.Answer;
import com.dnd.namuiwiki.domain.survey.type.Period;
import com.dnd.namuiwiki.domain.survey.type.Relation;
import com.dnd.namuiwiki.domain.wiki.WikiType;
import lombok.Builder;
import lombok.Getter;

Expand All @@ -21,6 +22,7 @@ public class SingleAnswerWithSurveyDetailDto {
private Object answer;
private String reason;
private String optionName;
private WikiType wikiType;

public static class SingleAnswerWithSurveyDetailDtoBuilder {
public SingleAnswerWithSurveyDetailDtoBuilder optionName(Question question, Answer surveyAnswer) {
Expand Down

0 comments on commit d0ac00c

Please sign in to comment.