Skip to content

Commit

Permalink
[Refactor/#251] combine sorting & filtering api of siren (#252)
Browse files Browse the repository at this point in the history
* [Refactor/#251] make query of get filtering & sorting siren list

* [Refactor/#251] add siren api sort & filter

* [Refactor/#251] rm not necessary part

* [Refactor/#251] optimization code

* [Refactor/#251] refactor about upcasting

* [Refactor/#251] refactor name of sort param
  • Loading branch information
Han-Jeong authored May 11, 2024
1 parent 28b41a8 commit bdde3b6
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.waggle.domain.board.application.question;

import com.example.waggle.domain.board.persistence.entity.Question;
import com.example.waggle.domain.board.presentation.dto.question.QuestionFilterParam;
import com.example.waggle.domain.board.presentation.dto.question.QuestionSortParam;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

Expand All @@ -21,7 +21,7 @@ public interface QuestionQueryService {

Page<Question> getPagedQuestions(Pageable pageable);

Page<Question> getPagedQuestionsByFilter(QuestionFilterParam filterParam, Pageable pageable);
Page<Question> getPagedQuestionsByFilter(QuestionSortParam sortParam, Pageable pageable);

List<Question> getRepresentativeQuestionList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.example.waggle.domain.board.persistence.dao.question.jpa.QuestionRepository;
import com.example.waggle.domain.board.persistence.entity.Question;
import com.example.waggle.domain.board.presentation.dto.question.QuestionFilterParam;
import com.example.waggle.domain.board.presentation.dto.question.QuestionSortParam;
import com.example.waggle.domain.recommend.persistence.dao.RecommendRepository;
import com.example.waggle.exception.object.handler.QuestionHandler;
import com.example.waggle.exception.payload.code.ErrorStatus;
Expand Down Expand Up @@ -62,8 +62,8 @@ public Page<Question> getPagedQuestions(Pageable pageable) {
}

@Override
public Page<Question> getPagedQuestionsByFilter(QuestionFilterParam filterParam, Pageable pageable) {
return questionRepository.findQuestionsByFilter(filterParam, pageable);
public Page<Question> getPagedQuestionsByFilter(QuestionSortParam sortParam, Pageable pageable) {
return questionRepository.findQuestionsByFilter(sortParam, pageable);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.waggle.domain.board.application.siren;

import com.example.waggle.domain.board.persistence.entity.Siren;
import com.example.waggle.domain.board.persistence.entity.SirenCategory;
import com.example.waggle.domain.board.presentation.dto.siren.SirenFilterParam;
import com.example.waggle.domain.board.presentation.dto.siren.SirenSortParam;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

Expand All @@ -21,9 +21,7 @@ public interface SirenQueryService {

Page<Siren> getPagedSirenListByMemberId(Long memberId, Pageable pageable);

Page<Siren> getPagedSirenListByFilter(SirenFilterParam filterParam, Pageable pageable);

Page<Siren> getPagedSirenListByCategory(SirenCategory category, Pageable pageable);
Page<Siren> getPagedSirenListByFilterAndSort(SirenFilterParam filterParam, SirenSortParam sortParam, Pageable pageable);

Siren getSirenByBoardId(Long boardId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.example.waggle.domain.board.persistence.dao.siren.jpa.SirenRepository;
import com.example.waggle.domain.board.persistence.entity.ResolutionStatus;
import com.example.waggle.domain.board.persistence.entity.Siren;
import com.example.waggle.domain.board.persistence.entity.SirenCategory;
import com.example.waggle.domain.board.presentation.dto.siren.SirenFilterParam;
import com.example.waggle.domain.board.presentation.dto.siren.SirenSortParam;
import com.example.waggle.domain.recommend.persistence.dao.RecommendRepository;
import com.example.waggle.exception.object.handler.SirenHandler;
import com.example.waggle.exception.payload.code.ErrorStatus;
Expand Down Expand Up @@ -71,13 +71,8 @@ public Page<Siren> getPagedSirenListByMemberId(Long memberId, Pageable pageable)
}

@Override
public Page<Siren> getPagedSirenListByFilter(SirenFilterParam filterParam, Pageable pageable) {
return sirenRepository.findSirensByFilter(filterParam, pageable);
}

@Override
public Page<Siren> getPagedSirenListByCategory(SirenCategory category, Pageable pageable) {
return sirenRepository.findByCategory(category, pageable);
public Page<Siren> getPagedSirenListByFilterAndSort(SirenFilterParam filterParam, SirenSortParam sortParam, Pageable pageable) {
return sirenRepository.findSirensByFilterAndSort(filterParam, sortParam, pageable);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.example.waggle.domain.board.persistence.dao.question.querydsl;

import com.example.waggle.domain.board.persistence.entity.Question;
import com.example.waggle.domain.board.presentation.dto.question.QuestionFilterParam;
import com.example.waggle.domain.board.presentation.dto.question.QuestionSortParam;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface QuestionQueryRepository {
Page<Question> findQuestionsByFilter(QuestionFilterParam filterParam, Pageable pageable);
Page<Question> findQuestionsByFilter(QuestionSortParam sortParam, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.waggle.domain.board.persistence.dao.question.querydsl;

import com.example.waggle.domain.board.persistence.entity.Question;
import com.example.waggle.domain.board.presentation.dto.question.QuestionFilterParam;
import com.example.waggle.domain.board.presentation.dto.question.QuestionSortParam;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -23,14 +23,14 @@ public class QuestionQueryRepositoryImpl implements QuestionQueryRepository {
private final JPAQueryFactory query;

@Override
public Page<Question> findQuestionsByFilter(QuestionFilterParam filterParam, Pageable pageable) {
public Page<Question> findQuestionsByFilter(QuestionSortParam sortParam, Pageable pageable) {
JPAQuery<Question> baseQuery = query.selectFrom(question);
if (filterParam == QuestionFilterParam.recommend) {
if (sortParam == QuestionSortParam.RECOMMEND) {
baseQuery.leftJoin(recommend).on(recommend.board.eq(question._super));
baseQuery.groupBy(question);
}
List<Question> questionList = baseQuery
.orderBy(createOrderFilter(filterParam))
.orderBy(createSortingOrder(sortParam))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
Expand All @@ -43,24 +43,24 @@ public Page<Question> findQuestionsByFilter(QuestionFilterParam filterParam, Pag
return new PageImpl<>(questionList, pageable, count);
}

private OrderSpecifier[] createOrderFilter(QuestionFilterParam filterParam) {
switch (filterParam) {
case latest -> {
private OrderSpecifier[] createSortingOrder(QuestionSortParam sortParam) {
switch (sortParam) {
case LATEST -> {
return new OrderSpecifier[]{question.createdDate.desc()};
}
case recommend -> {
case RECOMMEND -> {
return new OrderSpecifier[]{
recommend.count().desc(),
question.createdDate.desc()
};
}
case resolved -> {
case RESOLVED -> {
return new OrderSpecifier[]{
question.status.asc(),
question.createdDate.desc()
};
}
case unresolved -> {
case UNRESOLVED -> {
return new OrderSpecifier[]{
question.status.desc(),
question.createdDate.desc()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.example.waggle.domain.board.persistence.entity.Siren;
import com.example.waggle.domain.board.presentation.dto.siren.SirenFilterParam;
import java.util.List;
import com.example.waggle.domain.board.presentation.dto.siren.SirenSortParam;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface SirenQueryRepository {

Page<Siren> findSirensByFilter(SirenFilterParam filterParam, Pageable pageable);
Page<Siren> findSirensByFilterAndSort(SirenFilterParam filterParam, SirenSortParam sortParam, Pageable pageable);

List<Siren> findRandomUnresolvedSirens();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
package com.example.waggle.domain.board.persistence.dao.siren.querydsl;

import static com.example.waggle.domain.board.persistence.entity.QSiren.siren;
import static com.example.waggle.domain.recommend.persistence.entity.QRecommend.recommend;

import com.example.waggle.domain.board.persistence.entity.ResolutionStatus;
import com.example.waggle.domain.board.persistence.entity.Siren;
import com.example.waggle.domain.board.presentation.dto.siren.SirenFilterParam;
import com.example.waggle.domain.board.presentation.dto.siren.SirenSortParam;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.example.waggle.domain.board.persistence.entity.QSiren.siren;
import static com.example.waggle.domain.recommend.persistence.entity.QRecommend.recommend;

@Repository
@RequiredArgsConstructor
public class SirenQueryRepositoryImpl implements SirenQueryRepository {
private final JPAQueryFactory query;

@Override
public Page<Siren> findSirensByFilter(SirenFilterParam filterParam, Pageable pageable) {
public Page<Siren> findSirensByFilterAndSort(SirenFilterParam filterParam, SirenSortParam sortParam, Pageable pageable) {
JPAQuery<Siren> baseQuery = query.selectFrom(siren);
if (filterParam.equals(SirenFilterParam.recommend)) {
if (sortParam.equals(SirenSortParam.RECOMMEND)) {
baseQuery.leftJoin(recommend).on(siren._super.eq(recommend.board));
baseQuery.groupBy(siren);
}
List<Siren> sirenList = baseQuery
.orderBy(createOrderFilter(filterParam))
.orderBy(createSortingOrder(sortParam))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.where(selectCategory(filterParam))
.fetch();
Long count = query.select(siren.count())
.from(siren)
Expand All @@ -49,21 +53,21 @@ public List<Siren> findRandomUnresolvedSirens() {
.fetch();
}

private OrderSpecifier[] createOrderFilter(SirenFilterParam filterParam) {
switch (filterParam) {
case recommend -> {
private OrderSpecifier[] createSortingOrder(SirenSortParam sortParam) {
switch (sortParam) {
case RECOMMEND -> {
return new OrderSpecifier[]{
recommend.count().desc(),
siren.createdDate.desc()
};
}
case resolved -> {
case RESOLVED -> {
return new OrderSpecifier[]{
siren.status.asc(),
siren.createdDate.desc()
};
}
case unresolved -> {
case UNRESOLVED -> {
return new OrderSpecifier[]{
siren.status.desc(),
siren.createdDate.desc()
Expand All @@ -74,4 +78,8 @@ private OrderSpecifier[] createOrderFilter(SirenFilterParam filterParam) {
}
}
}

private BooleanExpression selectCategory(SirenFilterParam filterParam) {
return filterParam == SirenFilterParam.ALL ? null : siren.category.eq(filterParam.getCategory());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
import com.example.waggle.domain.board.application.question.QuestionQueryService;
import com.example.waggle.domain.board.persistence.entity.Question;
import com.example.waggle.domain.board.presentation.converter.QuestionConverter;
import com.example.waggle.domain.board.presentation.dto.question.QuestionFilterParam;
import com.example.waggle.domain.board.presentation.dto.question.QuestionRequest;
import com.example.waggle.domain.board.presentation.dto.question.QuestionResponse.QuestionSummaryDto;
import com.example.waggle.domain.board.presentation.dto.question.QuestionResponse.QuestionSummaryListDto;
import com.example.waggle.domain.board.presentation.dto.question.QuestionResponse.RepresentativeQuestionDto;
import com.example.waggle.domain.board.presentation.dto.question.QuestionSortParam;
import com.example.waggle.domain.member.persistence.entity.Member;
import com.example.waggle.domain.recommend.application.query.RecommendQueryService;
import com.example.waggle.exception.payload.code.ErrorStatus;
import com.example.waggle.exception.payload.dto.ApiResponseDto;
import com.example.waggle.global.annotation.api.ApiErrorCodeExample;
import com.example.waggle.global.annotation.auth.AuthUser;
import com.example.waggle.exception.payload.dto.ApiResponseDto;
import com.example.waggle.exception.payload.code.ErrorStatus;
import com.example.waggle.global.util.MediaUtil;
import com.example.waggle.global.util.PageUtil;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -107,16 +107,16 @@ public ApiResponseDto<QuestionSummaryListDto> getAllQuestions(
return ApiResponseDto.onSuccess(listDto);
}

@Operation(summary = "질문 필터 조회", description = "필터 옵션에 맞추어 결과를 조회합니다.")
@Operation(summary = "질문 정렬 조회", description = "필터 옵션에 맞추어 결과를 조회합니다.")
@ApiErrorCodeExample({
ErrorStatus._INTERNAL_SERVER_ERROR
})
@GetMapping("/filter")
public ApiResponseDto<QuestionSummaryListDto> getQuestionsByFilterParam(
@RequestParam(name = "filterParam") QuestionFilterParam filterParam,
@GetMapping("/sort")
public ApiResponseDto<QuestionSummaryListDto> getQuestionsBySortParam(
@RequestParam(name = "filterParam") QuestionSortParam sortParam,
@RequestParam(name = "currentPage", defaultValue = "0") int currentPage) {
Pageable pageable = PageRequest.of(currentPage, PageUtil.QUESTION_SIZE);
Page<Question> questions = questionQueryService.getPagedQuestionsByFilter(filterParam, pageable);
Page<Question> questions = questionQueryService.getPagedQuestionsByFilter(sortParam, pageable);
QuestionSummaryListDto listDto = QuestionConverter.toListDto(questions);
setRecommendCntInList(listDto.getQuestionList());
return ApiResponseDto.onSuccess(listDto);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.example.waggle.domain.board.presentation.controller;

import static com.example.waggle.global.util.PageUtil.SIREN_SIZE;

import com.example.waggle.domain.board.application.siren.SirenCacheService;
import com.example.waggle.domain.board.application.siren.SirenCommandService;
import com.example.waggle.domain.board.application.siren.SirenQueryService;
import com.example.waggle.domain.board.persistence.entity.Siren;
import com.example.waggle.domain.board.persistence.entity.SirenCategory;
import com.example.waggle.domain.board.presentation.converter.SirenConverter;
import com.example.waggle.domain.board.presentation.dto.siren.SirenFilterParam;
import com.example.waggle.domain.board.presentation.dto.siren.SirenRequest;
import com.example.waggle.domain.board.presentation.dto.siren.SirenResponse.SirenSummaryListDto;
import com.example.waggle.domain.board.presentation.dto.siren.SirenResponse.SirenDetailDto;
import com.example.waggle.domain.board.presentation.dto.siren.SirenResponse.SirenPagedSummaryListDto;
import com.example.waggle.domain.board.presentation.dto.siren.SirenResponse.SirenSummaryDto;
import com.example.waggle.domain.board.presentation.dto.siren.SirenResponse.SirenSummaryListDto;
import com.example.waggle.domain.board.presentation.dto.siren.SirenSortParam;
import com.example.waggle.domain.member.persistence.entity.Member;
import com.example.waggle.domain.recommend.application.query.RecommendQueryService;
import com.example.waggle.exception.payload.code.ErrorStatus;
Expand All @@ -24,8 +22,6 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand All @@ -34,15 +30,12 @@
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.example.waggle.global.util.PageUtil.SIREN_SIZE;

@Slf4j
@RequiredArgsConstructor
Expand Down Expand Up @@ -125,31 +118,17 @@ public ApiResponseDto<SirenPagedSummaryListDto> getAllSiren(
return ApiResponseDto.onSuccess(listDto);
}

@Operation(summary = "사이렌 필터 조회", description = "필터 옵션에 맞추어 결과를 조회합니다.")
@Operation(summary = "사이렌 정렬 및 필터 조회", description = "카테고리 옵션에 따라 필터링해주고 정렬 항목에 맞추어 결과를 조회합니다.")
@ApiErrorCodeExample({
ErrorStatus._INTERNAL_SERVER_ERROR
})
@GetMapping("/filter")
public ApiResponseDto<SirenPagedSummaryListDto> getSirensByFilter(
@GetMapping("/browse")
public ApiResponseDto<SirenPagedSummaryListDto> getSirensBySortAndFilter(
@RequestParam(name = "filterParam") SirenFilterParam filterParam,
@RequestParam(name = "currentPage", defaultValue = "0") int currentPage) {
Pageable pageable = PageRequest.of(currentPage, SIREN_SIZE);
Page<Siren> pagedSirenList = sirenQueryService.getPagedSirenListByFilter(filterParam, pageable);
SirenPagedSummaryListDto listDto = SirenConverter.toSirenPageDto(pagedSirenList);
setRecommendCntInList(listDto.getSirenList());
return ApiResponseDto.onSuccess(listDto);
}

@Operation(summary = "사이렌 카테고리 조회", description = "카테고리 옵션에 맞추어 결과를 조회합니다.")
@ApiErrorCodeExample({
ErrorStatus._INTERNAL_SERVER_ERROR
})
@GetMapping("/category")
public ApiResponseDto<SirenPagedSummaryListDto> getSirensByCategory(
@RequestParam(name = "category") SirenCategory category,
@RequestParam(name = "sortParam") SirenSortParam sortParam,
@RequestParam(name = "currentPage", defaultValue = "0") int currentPage) {
Pageable pageable = PageRequest.of(currentPage, SIREN_SIZE, latestSorting);
Page<Siren> pagedSirenList = sirenQueryService.getPagedSirenListByCategory(category, pageable);
Page<Siren> pagedSirenList = sirenQueryService.getPagedSirenListByFilterAndSort(filterParam, sortParam, pageable);
SirenPagedSummaryListDto listDto = SirenConverter.toSirenPageDto(pagedSirenList);
setRecommendCntInList(listDto.getSirenList());
return ApiResponseDto.onSuccess(listDto);
Expand Down

This file was deleted.

Loading

0 comments on commit bdde3b6

Please sign in to comment.