diff --git a/src/main/java/com/gongjakso/server/domain/team/controller/TeamController.java b/src/main/java/com/gongjakso/server/domain/team/controller/TeamController.java index 8cd4248..78a9bd8 100644 --- a/src/main/java/com/gongjakso/server/domain/team/controller/TeamController.java +++ b/src/main/java/com/gongjakso/server/domain/team/controller/TeamController.java @@ -9,6 +9,7 @@ import com.gongjakso.server.global.common.ApplicationResponse; import com.gongjakso.server.global.security.PrincipalDetails; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; @@ -89,7 +90,11 @@ public ApplicationResponse getTeam(@AuthenticationPrincipal PrincipalDe return ApplicationResponse.ok(teamService.getTeam(principalDetails == null ? null : principalDetails.getMember(), contestId, teamId)); } - @Operation(summary = "팀 리스트 조회 API", description = "특정 공모전에 해당하는 팀 리스트를 조회하는 API (오프셋 기반 페이지네이션)") + @Operation(summary = "팀 리스트 조회 API", + description = "특정 공모전에 해당하는 팀 리스트를 조회하는 API (오프셋 기반 페이지네이션)", + parameters = { + @Parameter(name = "sort", description = "정렬 기준 (예시- 생성 시각 내림차순 createdAt,desc / 인기순 오름차순 scrap,asc", required = false), + }) @GetMapping("/contest/{contest_id}/team/list") public ApplicationResponse> getTeamList(@PathVariable(value = "contest_id") Long contestId, @RequestParam(value = "province", required = false) String province, @@ -98,7 +103,12 @@ public ApplicationResponse> getTeamList(@PathVariable(value return ApplicationResponse.ok(teamService.getTeamListWithContest(contestId, province, district, pageable)); } - @Operation(summary = "팀 리스트 조회 API", description = "공모전에 상관없이 팀 리스트를 조회하는 API (검색 기능 존재 / 오프셋 기반 페이지네이션)") + @Operation(summary = "팀 리스트 조회 API", + method = "GET", + description = "공모전에 상관없이 팀 리스트를 조회하는 API (검색 기능 존재 / 오프셋 기반 페이지네이션)", + parameters = { + @Parameter(name = "sort", description = "정렬 기준 (예시- 생성 시각 내림차순 createdAt,desc / 인기순 오름차순 scrap,asc", required = false), + }) @GetMapping("/team/list") public ApplicationResponse> getTeamList(@RequestParam(value = "province", required = false) String province, @RequestParam(value = "district", required = false) String district, @@ -110,7 +120,7 @@ public ApplicationResponse> getTeamList(@RequestParam(value @Operation(summary = "내가 모집 중인 팀 리스트 조회 API", description = "공모전에 상관없이 내가 모집 중인 팀 리스트를 조회하는 API (오프셋 기반 페이지네이션)") @GetMapping("/team/my-recruit") public ApplicationResponse> getMyRecruitTeamList(@AuthenticationPrincipal PrincipalDetails principalDetails, - @PageableDefault(size = 8) Pageable pageable) { + @PageableDefault(size = 8) Pageable pageable) { return ApplicationResponse.ok(teamService.getMyRecruitTeamList(principalDetails.getMember(), pageable)); } diff --git a/src/main/java/com/gongjakso/server/domain/team/repository/TeamRepositoryImpl.java b/src/main/java/com/gongjakso/server/domain/team/repository/TeamRepositoryImpl.java index 419f733..bb67f36 100644 --- a/src/main/java/com/gongjakso/server/domain/team/repository/TeamRepositoryImpl.java +++ b/src/main/java/com/gongjakso/server/domain/team/repository/TeamRepositoryImpl.java @@ -3,13 +3,18 @@ import com.gongjakso.server.domain.team.dto.response.SimpleTeamRes; import com.gongjakso.server.domain.team.entity.Team; import com.gongjakso.server.domain.team.enumerate.TeamStatus; +import com.gongjakso.server.global.exception.ApplicationException; +import com.gongjakso.server.global.exception.ErrorCode; import com.querydsl.core.BooleanBuilder; +import com.querydsl.core.types.OrderSpecifier; import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -50,7 +55,7 @@ public Page findPaginationWithContest(Long contestId, String prov team.deletedAt.isNull(), builder ) - .orderBy(team.createdAt.desc()) + .orderBy(getSort(pageable).toArray(OrderSpecifier[]::new)) .offset(pageable.getOffset()) .limit(pageable.getPageSize()) .fetch(); @@ -89,7 +94,7 @@ public Page findPaginationWithoutContest(String province, String .from(team) .where(team.deletedAt.isNull() .and(builder)) - .orderBy(team.createdAt.desc()) + .orderBy(getSort(pageable).toArray(OrderSpecifier[]::new)) .offset(pageable.getOffset()) .limit(pageable.getPageSize()) .fetch(); @@ -110,11 +115,14 @@ public Page findPaginationWithoutContest(String province, String } public Page findRecruitPagination(Long memberId, Pageable pageable) { + List teamStatusList = Arrays.asList(TeamStatus.RECRUITING, TeamStatus.EXTENSION); + List teamList = queryFactory .select(team) .from(team) .where( team.member.id.eq(memberId), + team.status.in(teamStatusList), team.deletedAt.isNull() ) .orderBy(team.createdAt.desc()) @@ -232,4 +240,29 @@ public Page findScrapPagination(Long memberId, Pageable pageable) return new PageImpl<>(content, pageable, (total != null) ? total : 0); } + + private List> getSort(Pageable pageable) { + List> orderSpecifierList = new ArrayList<>(); + + if(pageable.getSort().isEmpty()) { + orderSpecifierList.add(team.createdAt.desc()); + return orderSpecifierList; + } + + for(Sort.Order order : pageable.getSort()) { + System.out.println(order.getProperty()); + switch (order.getProperty()) { + case "createdAt": + orderSpecifierList.add((order.isAscending()) ? team.createdAt.asc() : team.createdAt.desc()); + break; + case "scrap": + orderSpecifierList.add((order.isAscending()) ? team.scrapCount.asc() : team.scrapCount.desc()); + break; + default: + throw new ApplicationException(ErrorCode.INVALID_SORT_EXCEPTION); + } + } + + return orderSpecifierList; + } } diff --git a/src/main/java/com/gongjakso/server/global/exception/ErrorCode.java b/src/main/java/com/gongjakso/server/global/exception/ErrorCode.java index c0f4b1c..7a530c3 100644 --- a/src/main/java/com/gongjakso/server/global/exception/ErrorCode.java +++ b/src/main/java/com/gongjakso/server/global/exception/ErrorCode.java @@ -20,6 +20,7 @@ public enum ErrorCode { ALREADY_DELETE_EXCEPTION(HttpStatus.BAD_REQUEST, 2004, "이미 삭제된 리소스입니다."), FORBIDDEN_EXCEPTION(HttpStatus.FORBIDDEN, 2005, "인가되지 않는 요청입니다."), ALREADY_EXIST_EXCEPTION(HttpStatus.BAD_REQUEST, 2006, "이미 존재하는 리소스입니다."), + INVALID_SORT_EXCEPTION(HttpStatus.BAD_REQUEST, 2007, "올바르지 않은 정렬 값입니다."), // 3000: Auth Error KAKAO_TOKEN_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, 3000, "토큰 발급에서 오류가 발생했습니다."),