diff --git a/src/main/java/org/example/gather_back_end/creator/controller/CreatorController.java b/src/main/java/org/example/gather_back_end/creator/controller/CreatorController.java index 0f0e617..24bc7f0 100644 --- a/src/main/java/org/example/gather_back_end/creator/controller/CreatorController.java +++ b/src/main/java/org/example/gather_back_end/creator/controller/CreatorController.java @@ -99,9 +99,9 @@ public SuccessResponse> filteringCreator( @PageableDefault(size = 12, page = 0) Pageable pageable, @RequestParam(value = "price", required = false) Integer price, @RequestParam(value = "category", required = false) String category, - @RequestParam(value = "align", defaultValue = "recently", required = false) String recently + @RequestParam(value = "align", defaultValue = "recently", required = false) String align ) { - PageResponse res = creatorService.filteringCreator(pageable, price, category, recently); + PageResponse res = creatorService.filteringCreator(pageable, price, category, align); return SuccessResponse.of(res); } diff --git a/src/main/java/org/example/gather_back_end/creator/dto/filtering/CreatorInfo.java b/src/main/java/org/example/gather_back_end/creator/dto/filtering/CreatorInfo.java index 3f419b9..2745cf5 100644 --- a/src/main/java/org/example/gather_back_end/creator/dto/filtering/CreatorInfo.java +++ b/src/main/java/org/example/gather_back_end/creator/dto/filtering/CreatorInfo.java @@ -1,13 +1,11 @@ package org.example.gather_back_end.creator.dto.filtering; import io.swagger.v3.oas.annotations.media.Schema; -import java.util.Comparator; import java.util.List; -import java.util.stream.Collectors; +import java.util.stream.Stream; import org.example.gather_back_end.domain.Portfolio; import org.example.gather_back_end.domain.User; import org.example.gather_back_end.domain.Work; -import org.example.gather_back_end.util.format.WorkTypeConverter; public record CreatorInfo( @Schema(description = "크리에이터명", example = "hello") @@ -26,27 +24,43 @@ public record CreatorInfo( String thumbnailImgUrl ) { - public static CreatorInfo from(User user, List availableWork, List portfolioList) { + public static CreatorInfo from(User user, List availableWork, List portfolioList, String align, String category) { - // workList에서 startPrice 중 가장 작은 값 찾기 - String minStartPrice = user.getWorkList().stream() - .map(Work::getStartPrice) - .min(Comparator.naturalOrder()) - .map(String::valueOf) // int를 String으로 변환 - .orElse("N/A"); // workList가 비어 있을 경우 기본값 + // workList 필터링 + Stream workStream = user.getWorkList().stream(); - // availableWork를 한글명으로 변환 - List translatedAvailableWork = user.getWorkList().stream() - .map(work -> WorkTypeConverter.toKorean(work.getCategory())) - .distinct() - .collect(Collectors.toList()); + if (category != null) { + workStream = workStream.filter(work -> work.getCategory().name().equalsIgnoreCase(category)); + } + + // align 파라미터에 따라 startPrice 설정 + String startPrice; + if ("highPrice".equalsIgnoreCase(align)) { + startPrice = workStream + .map(Work::getStartPrice) + .max(Integer::compareTo) + .map(String::valueOf) + .orElse("N/A"); + } else { // 기본값은 minPrice + startPrice = workStream + .map(Work::getStartPrice) + .min(Integer::compareTo) + .map(String::valueOf) + .orElse("N/A"); + } + + // availableWork는 이미 변환된 상태로 전달됨 + List translatedAvailableWork = availableWork; + + // 포트폴리오 썸네일 설정 + String thumbnailImgUrl = portfolioList.isEmpty() ? null : portfolioList.get(0).getThumbnailImgUrl(); return new CreatorInfo( user.getNickname(), translatedAvailableWork, user.getIntroductionTitle(), - minStartPrice, - portfolioList.getFirst().getThumbnailImgUrl() + startPrice, + thumbnailImgUrl ); } } diff --git a/src/main/java/org/example/gather_back_end/creator/service/CreatorServiceImpl.java b/src/main/java/org/example/gather_back_end/creator/service/CreatorServiceImpl.java index d32fe42..1974a33 100644 --- a/src/main/java/org/example/gather_back_end/creator/service/CreatorServiceImpl.java +++ b/src/main/java/org/example/gather_back_end/creator/service/CreatorServiceImpl.java @@ -1,8 +1,6 @@ package org.example.gather_back_end.creator.service; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Set; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -20,9 +18,7 @@ import org.example.gather_back_end.work.dto.GetWorkRes; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; import org.springframework.security.core.Authentication; import org.springframework.stereotype.Service; @@ -105,20 +101,19 @@ public GetCreatorRes getCreatorInfo(Authentication authentication){ @Override public PageResponse filteringCreator(Pageable pageable, Integer price, String category, String align) { - // 정렬 조건 설정 - Sort sort = switch (align) { - case "lowPrice" -> Sort.by(Sort.Direction.ASC, "workList.startPrice"); - case "highPrice" -> Sort.by(Sort.Direction.DESC, "workList.startPrice"); - default -> Sort.by(Sort.Direction.DESC, "createAt"); - }; - - Pageable sortedPageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort); // category를 WorkType으로 변환 - WorkType workCategory = (category != null) ? WorkType.valueOf(category) : null; + WorkType workCategory = null; + if (category != null) { + try { + workCategory = WorkType.valueOf(category); + } catch (IllegalArgumentException e) { + log.error("유효하지 않은 카테고리: {}", category); + } + } // 데이터베이스에서 페이징 처리된 결과 가져오기 - Page creators = userRepository.customFiltering(price, workCategory, sortedPageable); + Page creators = userRepository.customFiltering(price, workCategory, align, pageable); // CreatorInfo로 변환 List creatorInfoList = creators.getContent().stream() @@ -127,13 +122,14 @@ public PageResponse filteringCreator(Pageable pageable, Integer pri user.getWorkList().stream() .map(work -> WorkTypeConverter.toKorean(work.getCategory())) .distinct() - .toList(), - user.getPortfolioList() + .collect(Collectors.toList()), + user.getPortfolioList(), + align, // align 파라미터 전달 + category // category 파라미터 전달 )) - .toList(); + .collect(Collectors.toList()); // PageResponse로 변환 - return PageResponse.of(new PageImpl<>(creatorInfoList, sortedPageable, creators.getTotalElements())); + return PageResponse.of(new PageImpl<>(creatorInfoList, pageable, creators.getTotalElements())); } - } diff --git a/src/main/java/org/example/gather_back_end/repository/UserRepository.java b/src/main/java/org/example/gather_back_end/repository/UserRepository.java index c2d25fe..54d9e86 100644 --- a/src/main/java/org/example/gather_back_end/repository/UserRepository.java +++ b/src/main/java/org/example/gather_back_end/repository/UserRepository.java @@ -39,11 +39,11 @@ default User getByNickname(String nickname) { // 크리에이터 찾기 @Query("SELECT u FROM User u " + - "JOIN u.workList w " + - "JOIN u.portfolioList p " + - "WHERE u.introductionTitle IS NOT NULL " + // 소개글 제목 존재 - "AND SIZE(u.workList) > 0 " + // 작업 가능 항목 등록 - "AND SIZE(u.portfolioList) > 0 ") + "JOIN u.workList w " + + "JOIN u.portfolioList p " + + "WHERE u.introductionTitle IS NOT NULL " + // 소개글 제목 존재 + "AND SIZE(u.workList) > 0 " + // 작업 가능 항목 등록 + "AND SIZE(u.portfolioList) > 0 ") // 포트폴리오 등록 // TODO: 포트폴리오 더미데이터 모두 넣은 후 주석 해제 // "AND SIZE(u.portfolioList) > 0 " + // 포트폴리오 등록 @@ -51,17 +51,27 @@ default User getByNickname(String nickname) { // "AND p.fileUrl IS NOT NULL") // 포트폴리오 파일 존재 List findAllCreators(); - @Query("SELECT DISTINCT u FROM User u " + - "JOIN u.workList w " + - "WHERE u.introductionTitle IS NOT NULL " + - "AND (:price IS NULL OR " + - " (:price = 10000 AND w.startPrice < 10000) OR " + - " (:price = 50000 AND w.startPrice < 50000) OR " + - " (:price = 100000 AND w.startPrice < 100000) OR " + - " (:price = 200000 AND w.startPrice < 200000) OR " + - " (:price = 200001 AND w.startPrice >= 200000)) " + - "AND (:category IS NULL OR w.category = :category)") - Page customFiltering(@Param("price") Integer price, @Param("category") WorkType category, Pageable pageable); + @Query("SELECT u FROM User u " + + "JOIN u.workList w " + + "WHERE u.introductionTitle IS NOT NULL " + + "AND (:price IS NULL OR " + + " (:price = 10000 AND w.startPrice < 10000) OR " + + " (:price = 50000 AND w.startPrice < 50000) OR " + + " (:price = 100000 AND w.startPrice < 100000) OR " + + " (:price = 200000 AND w.startPrice < 200000) OR " + + " (:price = 200001 AND w.startPrice >= 200000)) " + + "AND (:category IS NULL OR w.category = :category) " + + "GROUP BY u.id " + + "ORDER BY " + + " CASE WHEN :align = 'lowPrice' THEN MIN(w.startPrice) END ASC, " + + " CASE WHEN :align = 'highPrice' THEN MAX(w.startPrice) END DESC, " + + " MAX(u.createAt) DESC") + Page customFiltering( + @Param("price") Integer price, + @Param("category") WorkType category, + @Param("align") String align, + Pageable pageable + ); @Query("SELECT u FROM User u " + "JOIN u.workList w " +