Skip to content

Commit

Permalink
๐Ÿ› ๊ฐ€๊ฒฉ์ˆœ ์ •๋ ฌ ์•ˆ๋จ ์˜ค๋ฅ˜ (#116)
Browse files Browse the repository at this point in the history
* ๐Ÿ› ๊ฐ€๊ฒฉ ์ •๋ ฌ ๊ธฐ๋Šฅ ์˜ค๋ฅ˜ ์ˆ˜์ •

* ๐Ÿšง ๋‹ค์‹œ ์ž‘์—… ์•ˆ๋จ ์ƒํƒœ

* โœ๏ธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ช… ๋ณ€๊ฒฝ

* ๐Ÿ› 1์ฐจ ์ˆ˜์ •

* ๐Ÿ› ์ตœ์ข… ์˜ค๋ฅ˜ ํ•ด๊ฒฐ
  • Loading branch information
MinseoKangQ authored and Jindongleee committed Nov 16, 2024
1 parent 8c91420 commit 749d9f8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public SuccessResponse<PageResponse<CreatorInfo>> 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<CreatorInfo> res = creatorService.filteringCreator(pageable, price, category, recently);
PageResponse<CreatorInfo> res = creatorService.filteringCreator(pageable, price, category, align);
return SuccessResponse.of(res);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -26,27 +24,43 @@ public record CreatorInfo(
String thumbnailImgUrl
) {

public static CreatorInfo from(User user, List<String> availableWork, List<Portfolio> portfolioList) {
public static CreatorInfo from(User user, List<String> availableWork, List<Portfolio> 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<Work> workStream = user.getWorkList().stream();

// availableWork๋ฅผ ํ•œ๊ธ€๋ช…์œผ๋กœ ๋ณ€ํ™˜
List<String> 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<String> 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
);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -105,20 +101,19 @@ public GetCreatorRes getCreatorInfo(Authentication authentication){

@Override
public PageResponse<CreatorInfo> 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<User> creators = userRepository.customFiltering(price, workCategory, sortedPageable);
Page<User> creators = userRepository.customFiltering(price, workCategory, align, pageable);

// CreatorInfo๋กœ ๋ณ€ํ™˜
List<CreatorInfo> creatorInfoList = creators.getContent().stream()
Expand All @@ -127,13 +122,14 @@ public PageResponse<CreatorInfo> 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()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,39 @@ 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 " + // ํฌํŠธํด๋ฆฌ์˜ค ๋“ฑ๋ก
// "AND p.thumbnailImgUrl IS NOT NULL " + // ํฌํŠธํด๋ฆฌ์˜ค ์ธ๋„ค์ผ ์กด์žฌ
// "AND p.fileUrl IS NOT NULL") // ํฌํŠธํด๋ฆฌ์˜ค ํŒŒ์ผ ์กด์žฌ
List<User> 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<User> 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<User> 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 " +
Expand Down

0 comments on commit 749d9f8

Please sign in to comment.