Skip to content

Commit

Permalink
refactor: 전체 폴더에 대한 콜렉션 조회 추가 및 코드리뷰에 의한 네이밍 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
pparkjs committed Oct 27, 2024
1 parent 3de5741 commit 9b6bd6c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,44 @@
import java.util.List;
import lombok.Builder;

public record CollectionResponse(
public record CollectionFindResponse(
Long cursorId,
Boolean hasNext,
List<CollectionListsResponse> collectionLists,
List<CollectionDto> collectionLists,
String folderName
) {

public static CollectionResponse of(
public static CollectionFindResponse of(
Long cursorId,
Boolean hasNext,
List<Collect> collects,
String folderName
) {
return new CollectionResponse(cursorId, hasNext, toList(collects), folderName);
return new CollectionFindResponse(cursorId, hasNext, toList(collects), folderName);
}

public static List<CollectionListsResponse> toList(List<Collect> collects) {
public static List<CollectionDto> toList(List<Collect> collects) {
return collects.stream()
.map(CollectionListsResponse::of)
.map(CollectionDto::of)
.toList();
}

public record CollectionListsResponse(
public record CollectionDto(
Long id,
ListsResponse list
ListsDto list
) {

public static CollectionListsResponse of(Collect collect) {
return new CollectionListsResponse(collect.getId(), toResponse(collect.getList()));
public static CollectionDto of(Collect collect) {
return new CollectionDto(collect.getId(), toResponse(collect.getList()));
}

public static ListsResponse toResponse(ListEntity list) {
return ListsResponse.of(list);
public static ListsDto toResponse(ListEntity list) {
return ListsDto.of(list);
}
}

@Builder
public record ListsResponse(
public record ListsDto(
Long id,
String backgroundColor,
String title,
Expand All @@ -54,11 +54,11 @@ public record ListsResponse(
String representativeImageUrl,
String category,
LocalDateTime updatedDate,
List<ListItemsResponse> listItems
List<ListItemsDto> listItems
) {

public static ListsResponse of(ListEntity list) {
return ListsResponse.builder()
public static ListsDto of(ListEntity list) {
return ListsDto.builder()
.id(list.getId())
.backgroundColor(list.getBackgroundColor().name())
.title(list.getTitle().getValue())
Expand All @@ -72,23 +72,23 @@ public static ListsResponse of(ListEntity list) {
.build();
}

public static List<ListItemsResponse> toList(List<Item> items) {
public static List<ListItemsDto> toList(List<Item> items) {
return items.stream()
.map(ListItemsResponse::of)
.map(ListItemsDto::of)
.toList();
}
}

@Builder
public record ListItemsResponse(
public record ListItemsDto(
Long id,
int rank,
String title,
String imageUrl
) {

public static ListItemsResponse of(Item item) {
return ListItemsResponse.builder()
public static ListItemsDto of(Item item) {
return ListItemsDto.builder()
.id(item.getId())
.rank(item.getRanking())
.title(item.getTitle().getValue())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.listywave.alarm.application.domain.AlarmCreateEvent;
import com.listywave.collection.application.domain.Collect;
import com.listywave.collection.application.domain.Folder;
import com.listywave.collection.application.dto.CollectionResponse;
import com.listywave.collection.application.dto.CollectionFindResponse;
import com.listywave.collection.repository.CollectionRepository;
import com.listywave.collection.repository.FolderRepository;
import com.listywave.list.application.domain.category.CategoryType;
Expand Down Expand Up @@ -32,6 +32,8 @@ public class CollectionService {
private final CollectionRepository collectionRepository;
private final ApplicationEventPublisher applicationEventPublisher;

private final static String FOLDER_ENTIRE_NAME = "전체";

public void collectOrCancel(Long listId, Long folderId, Long userId) {
User user = userRepository.getById(userId);
ListEntity list = listRepository.getById(listId);
Expand Down Expand Up @@ -60,17 +62,21 @@ private void addCollect(ListEntity list, User user, Folder folder) {
applicationEventPublisher.publishEvent(AlarmCreateEvent.collect(user, list));
}

public CollectionResponse getCollection(Long loginUserId, Long cursorId, Pageable pageable, Long folderId) {
User user = userRepository.getById(loginUserId);
Folder folder = folderRepository.getById(folderId);
public CollectionFindResponse getCollection(Long userId, Long cursorId, Pageable pageable, Long folderId) {
User user = userRepository.getById(userId);
String folderName = FOLDER_ENTIRE_NAME;
if (folderId != 0L) {
Folder folder = folderRepository.getById(folderId);
folderName = folder.getFolderName();
}
Slice<Collect> result = collectionRepository.getAllCollectionList(cursorId, pageable, user.getId(), folderId);
List<Collect> collectionList = result.getContent();

cursorId = null;
if (!collectionList.isEmpty()) {
cursorId = collectionList.get(collectionList.size() - 1).getId();
}
return CollectionResponse.of(cursorId, result.hasNext(), collectionList, folder.getFolderName());
return CollectionFindResponse.of(cursorId, result.hasNext(), collectionList, folderName);
}

public List<CategoryTypeResponse> getCategoriesOfCollection(Long loginUserId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.listywave.collection.presentation.controller;

import com.listywave.collection.application.dto.CollectionResponse;
import com.listywave.collection.application.dto.CollectionFindResponse;
import com.listywave.collection.application.service.CollectionService;
import com.listywave.collection.presentation.dto.FolderSelectionRequest;
import com.listywave.common.auth.Auth;
import com.listywave.list.application.dto.response.CategoryTypeResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
Expand All @@ -30,13 +34,13 @@ ResponseEntity<Void> collectOrCancel(
}

@GetMapping("/folder/{folderId}/collections")
ResponseEntity<CollectionResponse> getCollection(
ResponseEntity<CollectionFindResponse> getCollection(
@Auth Long loginUserId,
@PathVariable("folderId") Long folderId,
@RequestParam(name = "cursorId", required = false) Long cursorId,
@PageableDefault(size = 10) Pageable pageable
) {
CollectionResponse collection = collectionService.getCollection(loginUserId, cursorId, pageable, folderId);
CollectionFindResponse collection = collectionService.getCollection(loginUserId, cursorId, pageable, folderId);
return ResponseEntity.ok(collection);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import com.listywave.user.application.domain.User;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import java.util.List;

@RequiredArgsConstructor
public class CustomCollectionRepositoryImpl implements CustomCollectionRepository {
Expand All @@ -42,7 +42,7 @@ public Slice<Collect> getAllCollectionList(Long cursorId, Pageable pageable, Lon
}

private BooleanExpression folderIdEq(Long folderId) {
return collect.folder.id.eq(folderId);
return folderId == 0L ? null : collect.folder.id.eq(folderId);
}

private BooleanExpression collectIdLt(Long cursorId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package com.listywave.acceptance.collection;

import com.listywave.acceptance.common.AcceptanceTest;
import com.listywave.collection.application.dto.CollectionResponse;
import com.listywave.collection.application.dto.CollectionResponse.CollectionListsResponse;
import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.list.application.dto.response.ListCreateResponse;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

import static com.listywave.acceptance.collection.CollectionAcceptanceTestHelper.나의_콜렉션_조회_API_호출;
import static com.listywave.acceptance.collection.CollectionAcceptanceTestHelper.콜렉트_또는_콜렉트취소_API_호출;
import static com.listywave.acceptance.common.CommonAcceptanceHelper.HTTP_상태_코드를_검증한다;
import static com.listywave.acceptance.folder.FolderAcceptanceTestHelper.*;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.*;
import static com.listywave.acceptance.folder.FolderAcceptanceTestHelper.폴더_생성_API_호출;
import static com.listywave.acceptance.folder.FolderAcceptanceTestHelper.폴더_생성_요청_데이터;
import static com.listywave.acceptance.folder.FolderAcceptanceTestHelper.폴더_선택_요청_데이터;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.가장_좋아하는_견종_TOP3_생성_요청_데이터;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.리스트_저장_API_호출;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.회원용_리스트_상세_조회_API_호출;
import static com.listywave.list.fixture.ListFixture.지정된_개수만큼_리스트를_생성한다;
import static com.listywave.user.fixture.UserFixture.동호;
import static com.listywave.user.fixture.UserFixture.정수;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.NO_CONTENT;

import com.listywave.acceptance.common.AcceptanceTest;
import com.listywave.collection.application.dto.CollectionFindResponse;
import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.list.application.dto.response.ListCreateResponse;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("콜렉션 관련 인수테스트")
public class CollectionAcceptanceTest extends AcceptanceTest {

Expand Down Expand Up @@ -112,9 +114,40 @@ public class CollectionAcceptanceTest extends AcceptanceTest {

// when
var 결과 = 나의_콜렉션_조회_API_호출(정수_엑세스_토큰, 1L)
.as(CollectionResponse.class)
.as(CollectionFindResponse.class)
.collectionLists().stream()
.map(CollectionFindResponse.CollectionDto::list)
.collect(Collectors.toList());

// then
assertThat(결과).usingRecursiveComparison()
.comparingOnlyFields("id")
.isEqualTo(생성한_리스트.stream().map(ListEntity::getId).toList());
}

@Test
void 나의_전체_콜렉션을_조회한다() {
// given
var 동호 = 회원을_저장한다(동호());
var 정수 = 회원을_저장한다(정수());
var 정수_엑세스_토큰 = 액세스_토큰을_발급한다(정수);
var 생성한_리스트 = 지정된_개수만큼_리스트를_생성한다(동호, 2);
리스트를_모두_저장한다(생성한_리스트);

var 폴더_생성_요청_데이터1 = 폴더_생성_요청_데이터("맛집");
var 폴더_생성_요청_데이터2 = 폴더_생성_요청_데이터("예카");
var 폴더_선택_데이터1 = 폴더_선택_요청_데이터(1L);
var 폴더_선택_데이터2 = 폴더_선택_요청_데이터(2L);
폴더_생성_API_호출(정수_엑세스_토큰, 폴더_생성_요청_데이터1);
폴더_생성_API_호출(정수_엑세스_토큰, 폴더_생성_요청_데이터2);
콜렉트_또는_콜렉트취소_API_호출(정수_엑세스_토큰, 1L, 폴더_선택_데이터1);
콜렉트_또는_콜렉트취소_API_호출(정수_엑세스_토큰, 2L, 폴더_선택_데이터2);

// when
var 결과 = 나의_콜렉션_조회_API_호출(정수_엑세스_토큰, 0L)
.as(CollectionFindResponse.class)
.collectionLists().stream()
.map(CollectionListsResponse::list)
.map(CollectionFindResponse.CollectionDto::list)
.collect(Collectors.toList());

// then
Expand Down
38 changes: 19 additions & 19 deletions src/test/java/com/listywave/acceptance/user/UserAcceptanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@

import static com.listywave.acceptance.common.CommonAcceptanceHelper.HTTP_상태_코드를_검증한다;
import static com.listywave.acceptance.follow.FollowAcceptanceTestHelper.팔로우_요청_API;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.*;
import static com.listywave.acceptance.user.UserAcceptanceTestHelper.*;
import static com.listywave.user.fixture.UserFixture.*;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.가장_좋아하는_견종_TOP3_생성_요청_데이터;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.리스트_수정_API_호출;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.리스트_저장_API_호출;
import static com.listywave.acceptance.list.ListAcceptanceTestHelper.아이템_순위와_라벨을_바꾼_좋아하는_견종_TOP3_요청_데이터;
import static com.listywave.acceptance.user.UserAcceptanceTestHelper.닉네임_중복_체크_요청;
import static com.listywave.acceptance.user.UserAcceptanceTestHelper.비회원_추천_API;
import static com.listywave.acceptance.user.UserAcceptanceTestHelper.비회원_회원_정보_조회_요청;
import static com.listywave.acceptance.user.UserAcceptanceTestHelper.비회원이_사용자_검색;
import static com.listywave.acceptance.user.UserAcceptanceTestHelper.프로필_수정_요청;
import static com.listywave.acceptance.user.UserAcceptanceTestHelper.프로필_수정_요청_데이터;
import static com.listywave.acceptance.user.UserAcceptanceTestHelper.회원_추천_API;
import static com.listywave.acceptance.user.UserAcceptanceTestHelper.회원이_사용자_검색;
import static com.listywave.acceptance.user.UserAcceptanceTestHelper.회원이_회원_정보_조회_요청;
import static com.listywave.user.fixture.UserFixture.동호;
import static com.listywave.user.fixture.UserFixture.유진;
import static com.listywave.user.fixture.UserFixture.정수;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.springframework.http.HttpStatus.*;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.NO_CONTENT;

import com.listywave.acceptance.common.AcceptanceTest;
import com.listywave.list.application.dto.response.ListCreateResponse;
Expand Down Expand Up @@ -159,21 +174,6 @@ class 추천_사용자 {
assertThat(결과.get(0).nickname()).isEqualTo(유진.getNickname());
}

@Test
void 비회원이_추천_사용자_조회한다() {
// given
회원을_저장한다(동호());
회원을_저장한다(정수());
회원을_저장한다(유진());

// when
List<UsersRecommendedResponse> 결과 = 비회원_추천_API().as(new TypeRef<>() {
});

// then
assertThat(결과).hasSize(3);
}

@Test
void 가장_최근에_리스트_생성_또는_수정한_사용자_순위대로_추천_사용자_조회한다() {
// given
Expand Down

0 comments on commit 9b6bd6c

Please sign in to comment.