Skip to content

Commit

Permalink
feat: 복수선택형 filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
gimhanul committed Sep 14, 2023
1 parent 2630f40 commit 58bbf75
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ public ApplicationStatusResponse getMyApplication() {
return queryMyApplicationService.execute();
}

@GetMapping("/{id}/result")
// TODO :: HTTP method mismatched
@PostMapping("/{id}/result")
public ApplicationResultResponse getApplicationResult(
@PathVariable Long id,
@RequestBody(required = false) @Valid FilterListRequest filterList
@RequestBody(required = false) FilterListRequest filterList
) {
return queryApplicationResultService.execute(id, filterList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.soogung.simblue.domain.user.facade.UserFacade;
import com.soogung.simblue.global.error.exception.ErrorCode;
import com.soogung.simblue.global.error.exception.SimblueException;
import com.soogung.simblue.global.util.Operator;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -59,6 +60,7 @@ public ApplicationResultResponse execute(Long id, FilterListRequest filterList)
.findApplicationResult(id).stream()
.filter(r -> filtering(r, filterList))
.map(this::createReplyList)
.filter(r -> filteringContains(r, filterList))
.collect(Collectors.toList());

return new ApplicationResultResponse(
Expand All @@ -70,14 +72,13 @@ public ApplicationResultResponse execute(Long id, FilterListRequest filterList)
}

private boolean filtering(ReplyBlock block, FilterListRequest request) {
if (
request == null ||
request.getFilterList() == null ||
request.getFilterList().isEmpty()) {
return true;
}
if (validate(request)) return true;

for (FilterRequest filter : request.getFilterList()) {
if (filter.getOperator() == Operator.CONTAINS) {
continue;
}

Reply reply = getReplyByQuestionId(block.getReplies(), filter.getQuestionId());
if (!filter.getOperator().getComparator()
.execute(filter.getTarget(), reply.getAnswer())) {
Expand All @@ -88,13 +89,44 @@ private boolean filtering(ReplyBlock block, FilterListRequest request) {
return true;
}

private boolean filteringContains(ReplyBlockResponse block, FilterListRequest request) {
if (validate(request)) return true;

for (FilterRequest filter : request.getFilterList()) {
if (filter.getOperator() != Operator.CONTAINS) {
continue;
}

ReplyResponse reply = getReplyResponseByQuestionId(block.getReplyList(), filter.getQuestionId());
if (!filter.getOperator().getComparator()
.execute(filter.getTarget(), reply.getReply())) {
return false;
}
}

return true;
}

private boolean validate(FilterListRequest request) {
return request == null ||
request.getFilterList() == null ||
request.getFilterList().isEmpty();
}

private Reply getReplyByQuestionId(List<Reply> replyList, Long questionId) {
return replyList.stream()
.filter(r -> r.getQuestion().getId().equals(questionId))
.findFirst()
.orElseThrow(() -> new SimblueException(ErrorCode.BAD_REQUEST));
}

private ReplyResponse getReplyResponseByQuestionId(List<ReplyResponse> replyList, Long questionId) {
return replyList.stream()
.filter(r -> r.getQuestionId().equals(questionId))
.findFirst()
.orElseThrow(() -> new SimblueException(ErrorCode.BAD_REQUEST));
}

private ReplyBlockResponse createReplyList(ReplyBlock block) {
User student = block.getUser();

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/soogung/simblue/global/util/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public enum Operator {
BEFORE((target, other) -> (LocalDate.parse((String) target)).isBefore((LocalDate.parse((String) other)))),
AFTER((target, other) -> (LocalDate.parse((String) target)).isAfter((LocalDate.parse((String) other)))),
BEFORE_OR_EQUAL((target, other) -> !AFTER.comparator.execute(target, other)),
AFTER_OR_EQUAL((target, other) -> !BEFORE.comparator.execute(target, other));
AFTER_OR_EQUAL((target, other) -> !BEFORE.comparator.execute(target, other)),
CONTAINS((target, other) -> ((String) other).contains((String) target));

private final Comparator<Object> comparator;
}

0 comments on commit 58bbf75

Please sign in to comment.