Skip to content

Commit

Permalink
feat: 리스트 검색, 리스트 상세 조회 API 요청 및 응답값 수정
Browse files Browse the repository at this point in the history
- 리스트 검색: Category의 영어 이름으로 검색 -> CategoryCode를 이용해 검색, 검색 응답 시에 카테고리 정보 포함
- 리스트 상세 조회: 응답에 카테고리 코드 포함 (#336)
  • Loading branch information
kdkdhoho committed Jan 9, 2025
1 parent f806937 commit fd681bb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

@Builder
public record ListDetailResponse(
String categoryCode,
String categoryEngName,
String categoryKorName,
List<LabelResponse> labels,
Expand Down Expand Up @@ -50,6 +51,7 @@ public static ListDetailResponse of(
List<ReactionResponse> reactions
) {
return ListDetailResponse.builder()
.categoryCode(list.getCategory().getCode())
.categoryEngName(list.getCategory().name().toLowerCase())
.categoryKorName(list.getCategory().getViewName())
.labels(LabelResponse.toList(list.getLabels().getValues()))
Expand All @@ -74,7 +76,7 @@ public static ListDetailResponse of(
.reactions(reactions)
.build();
}

public record LabelResponse(
String name
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@Builder
public record ListSearchResponse(
List<ListInfo> resultLists,
List<ListDto> resultLists,
Long totalCount,
Long cursorId,
boolean hasNext
Expand All @@ -21,15 +21,15 @@ public static ListSearchResponse of(
boolean hasNext
) {
return ListSearchResponse.builder()
.resultLists(ListInfo.toList(lists))
.resultLists(ListDto.toList(lists))
.totalCount(totalCount)
.cursorId(cursorId)
.hasNext(hasNext)
.build();
}

@Builder
public record ListInfo(
public record ListDto(
Long id,
String title,
List<ItemInfo> items,
Expand All @@ -39,17 +39,20 @@ public record ListInfo(
Long ownerId,
String ownerNickname,
String ownerProfileImageUrl,
String representImageUrl
String representImageUrl,
String categoryCode,
String categoryKorName,
String categoryEngName
) {

public static List<ListInfo> toList(List<ListEntity> lists) {
public static List<ListDto> toList(List<ListEntity> lists) {
return lists.stream()
.map(ListInfo::of)
.map(ListDto::of)
.toList();
}

private static ListInfo of(ListEntity list) {
return ListInfo.builder()
private static ListDto of(ListEntity list) {
return ListDto.builder()
.id(list.getId())
.title(list.getTitle().getValue())
.items(ItemInfo.toList(list.getTop3Items().getValues()))
Expand All @@ -60,6 +63,9 @@ private static ListInfo of(ListEntity list) {
.ownerNickname(list.getUser().getNickname())
.ownerProfileImageUrl(list.getUser().getProfileImageUrl())
.representImageUrl(list.getRepresentImageUrl())
.categoryCode(list.getCategory().getCode())
.categoryKorName(list.getCategory().getViewName())
.categoryEngName(list.getCategory().name())
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,27 +212,27 @@ private ListRecentResponse toListRecentResponse(Slice<ListEntity> result) {
}

@Transactional(readOnly = true)
public ListSearchResponse search(String keyword, SortType sortType, CategoryType category, int size, Long cursorId) {
public ListSearchResponse search(String keyword, SortType sortType, String categoryCode, int size, Long cursorId) {
List<ListEntity> lists = listRepository.findAll().stream()
.filter(list -> !list.isDeletedUser() && list.isPublic())
.toList();
ListEntities allList = new ListEntities(lists);
ListEntities filtered = allList.filterBy(category)
ListEntities listEntities = new ListEntities(lists);

ListEntities filteredAndSortedLists = listEntities.filterBy(CategoryType.codeOf(categoryCode))
.filterBy(keyword)
.sortBy(sortType, keyword);

long totalCount = filtered.size();
long totalCount = filteredAndSortedLists.size();

ListEntity cursorList = (cursorId == 0L) ? null : listRepository.getById(cursorId);
List<ListEntity> paged = filtered.paging(cursorList, size + 1).listEntities();
List<ListEntity> pagedLists = filteredAndSortedLists.paging(cursorList, size + 1).listEntities();

if (paged.size() > size) {
return ListSearchResponse.of(paged.subList(0, size), totalCount, paged.get(size - 1).getId(), true);
if (pagedLists.size() > size) {
return ListSearchResponse.of(pagedLists.subList(0, size), totalCount, pagedLists.get(size - 1).getId(), true);
}
if (paged.isEmpty()) {
return ListSearchResponse.of(paged, totalCount, null, false);
if (pagedLists.isEmpty()) {
return ListSearchResponse.of(pagedLists, totalCount, null, false);
}
return ListSearchResponse.of(paged, totalCount, paged.get(paged.size() - 1).getId(), false);
return ListSearchResponse.of(pagedLists, totalCount, pagedLists.get(pagedLists.size() - 1).getId(), false);
}

public void update(Long listId, Long loginUserId, ListUpdateRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ ResponseEntity<ListRecentResponse> getRecentListsByFollowing(
ResponseEntity<ListSearchResponse> search(
@RequestParam(value = "keyword", defaultValue = "") String keyword,
@RequestParam(value = "sort", defaultValue = "new") SortType sort,
@RequestParam(value = "category", defaultValue = "entire") CategoryType category,
@RequestParam(value = "categoryCode", defaultValue = "0") String categoryCode,
@RequestParam(value = "size", defaultValue = "5") int size,
@RequestParam(value = "cursorId", defaultValue = "0") Long cursorId
) {
ListSearchResponse response = listService.search(keyword, sort, category, size, cursorId);
ListSearchResponse response = listService.search(keyword, sort, categoryCode, size, cursorId);
return ResponseEntity.ok(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.좋아하는_라면_TOP3_생성_요청_데이터;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.최신_리스트_10개_조회_카테고리_필터링_API_호출;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.추천_리스트_조회_API_호출;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.카테고리로_검색_API_호출;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.카테고리_코드로_검색_API_호출;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.카테고리와_키워드로_검색_API_호출;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.키워드로_검색_API_호출;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.키워드와_정렬기준을_포함한_검색_API_호출;
Expand Down Expand Up @@ -755,21 +755,31 @@ class 리스트_검색 {
}

@Test
void 카테고리로_필터링_할_수_있다() {
void 카테고리_코드로_필터링_할_수_있다() {
// given
var 동호 = 회원을_저장한다(동호());
var 동호_액세스_토큰 = 액세스_토큰을_발급한다(동호);
리스트_저장_API_호출(가장_좋아하는_견종_TOP3_생성_요청_데이터(List.of()), 동호_액세스_토큰);
var 좋아하는_라면_TOP3_생성_결과 = 리스트_저장_API_호출(좋아하는_라면_TOP3_생성_요청_데이터(List.of()), 동호_액세스_토큰).as(ListCreateResponse.class);

assertThat(가장_좋아하는_견종_TOP3_생성_요청_데이터(List.of()).category()).isNotEqualTo(좋아하는_라면_TOP3_생성_요청_데이터(List.of()).category());
CategoryType 검색하려는_카테고리 = 좋아하는_라면_TOP3_생성_요청_데이터(List.of()).category();

// when
var result = 카테고리로_검색_API_호출("etc").as(ListSearchResponse.class);
var result = 카테고리_코드로_검색_API_호출(검색하려는_카테고리.getCode()).as(ListSearchResponse.class);

// then
assertAll(
() -> assertThat(result.totalCount()).isOne(),
() -> assertThat(result.resultLists()).hasSize(1),
() -> assertThat(result.resultLists().get(0).id()).isEqualTo(좋아하는_라면_TOP3_생성_결과.listId())
() -> {
var listResponses = result.resultLists();

assertThat(listResponses).hasSize(1);
assertThat(listResponses.get(0).id()).isEqualTo(좋아하는_라면_TOP3_생성_결과.listId());
assertThat(listResponses.stream()
.allMatch(listResponse -> listResponse.categoryCode().equals(검색하려는_카테고리.getCode()))
).isTrue();
}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public abstract class ListAcceptanceTestHelper {

public static ListCreateRequest 좋아하는_라면_TOP3_생성_요청_데이터(List<Long> collaboratorIds) {
return new ListCreateRequest(
CategoryType.ETC,
CategoryType.FOOD_RECIPES,
List.of("라", "면", "좋"),
collaboratorIds,
"좋아하는 라면 TOP 3",
Expand Down Expand Up @@ -210,9 +210,9 @@ public abstract class ListAcceptanceTestHelper {
.extract();
}

public static ExtractableResponse<Response> 카테고리로_검색_API_호출(String category) {
public static ExtractableResponse<Response> 카테고리_코드로_검색_API_호출(String categoryCode) {
return given()
.when().get("/lists/search?category={category}", category)
.when().get("/lists/search?categoryCode={categoryCode}", categoryCode)
.then().log().all()
.extract();
}
Expand Down

0 comments on commit fd681bb

Please sign in to comment.