From 9b6bd6c0d99c77952e01ab6a21d62912d94a5b04 Mon Sep 17 00:00:00 2001 From: pparkjs Date: Sun, 27 Oct 2024 20:26:25 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=A0=84=EC=B2=B4=20=ED=8F=B4?= =?UTF-8?q?=EB=8D=94=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=BD=9C=EB=A0=89?= =?UTF-8?q?=EC=85=98=20=EC=A1=B0=ED=9A=8C=20=EC=B6=94=EA=B0=80=20=EB=B0=8F?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=EB=A6=AC=EB=B7=B0=EC=97=90=20=EC=9D=98?= =?UTF-8?q?=ED=95=9C=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ponse.java => CollectionFindResponse.java} | 42 ++++++------- .../service/CollectionService.java | 16 +++-- .../controller/CollectionController.java | 16 +++-- .../impl/CustomCollectionRepositoryImpl.java | 4 +- .../collection/CollectionAcceptanceTest.java | 63 ++++++++++++++----- .../acceptance/user/UserAcceptanceTest.java | 38 +++++------ 6 files changed, 111 insertions(+), 68 deletions(-) rename src/main/java/com/listywave/collection/application/dto/{CollectionResponse.java => CollectionFindResponse.java} (64%) diff --git a/src/main/java/com/listywave/collection/application/dto/CollectionResponse.java b/src/main/java/com/listywave/collection/application/dto/CollectionFindResponse.java similarity index 64% rename from src/main/java/com/listywave/collection/application/dto/CollectionResponse.java rename to src/main/java/com/listywave/collection/application/dto/CollectionFindResponse.java index 3e55e35d..8e0be44d 100644 --- a/src/main/java/com/listywave/collection/application/dto/CollectionResponse.java +++ b/src/main/java/com/listywave/collection/application/dto/CollectionFindResponse.java @@ -7,44 +7,44 @@ import java.util.List; import lombok.Builder; -public record CollectionResponse( +public record CollectionFindResponse( Long cursorId, Boolean hasNext, - List collectionLists, + List collectionLists, String folderName ) { - public static CollectionResponse of( + public static CollectionFindResponse of( Long cursorId, Boolean hasNext, List collects, String folderName ) { - return new CollectionResponse(cursorId, hasNext, toList(collects), folderName); + return new CollectionFindResponse(cursorId, hasNext, toList(collects), folderName); } - public static List toList(List collects) { + public static List toList(List 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, @@ -54,11 +54,11 @@ public record ListsResponse( String representativeImageUrl, String category, LocalDateTime updatedDate, - List listItems + List 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()) @@ -72,23 +72,23 @@ public static ListsResponse of(ListEntity list) { .build(); } - public static List toList(List items) { + public static List toList(List 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()) diff --git a/src/main/java/com/listywave/collection/application/service/CollectionService.java b/src/main/java/com/listywave/collection/application/service/CollectionService.java index a2ea3389..931fcb1a 100644 --- a/src/main/java/com/listywave/collection/application/service/CollectionService.java +++ b/src/main/java/com/listywave/collection/application/service/CollectionService.java @@ -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; @@ -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); @@ -60,9 +62,13 @@ 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 result = collectionRepository.getAllCollectionList(cursorId, pageable, user.getId(), folderId); List collectionList = result.getContent(); @@ -70,7 +76,7 @@ public CollectionResponse getCollection(Long loginUserId, Long cursorId, Pageabl 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 getCategoriesOfCollection(Long loginUserId) { diff --git a/src/main/java/com/listywave/collection/presentation/controller/CollectionController.java b/src/main/java/com/listywave/collection/presentation/controller/CollectionController.java index 2a97d509..6e85ec53 100644 --- a/src/main/java/com/listywave/collection/presentation/controller/CollectionController.java +++ b/src/main/java/com/listywave/collection/presentation/controller/CollectionController.java @@ -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 @@ -30,13 +34,13 @@ ResponseEntity collectOrCancel( } @GetMapping("/folder/{folderId}/collections") - ResponseEntity getCollection( + ResponseEntity 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); } diff --git a/src/main/java/com/listywave/collection/repository/custom/impl/CustomCollectionRepositoryImpl.java b/src/main/java/com/listywave/collection/repository/custom/impl/CustomCollectionRepositoryImpl.java index ad5289c0..4d469afe 100644 --- a/src/main/java/com/listywave/collection/repository/custom/impl/CustomCollectionRepositoryImpl.java +++ b/src/main/java/com/listywave/collection/repository/custom/impl/CustomCollectionRepositoryImpl.java @@ -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 { @@ -42,7 +42,7 @@ public Slice 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) { diff --git a/src/test/java/com/listywave/acceptance/collection/CollectionAcceptanceTest.java b/src/test/java/com/listywave/acceptance/collection/CollectionAcceptanceTest.java index 335718be..c0fa209d 100644 --- a/src/test/java/com/listywave/acceptance/collection/CollectionAcceptanceTest.java +++ b/src/test/java/com/listywave/acceptance/collection/CollectionAcceptanceTest.java @@ -1,21 +1,14 @@ 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.정수; @@ -23,6 +16,15 @@ 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 { @@ -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 diff --git a/src/test/java/com/listywave/acceptance/user/UserAcceptanceTest.java b/src/test/java/com/listywave/acceptance/user/UserAcceptanceTest.java index ba436c5b..ac5402d3 100644 --- a/src/test/java/com/listywave/acceptance/user/UserAcceptanceTest.java +++ b/src/test/java/com/listywave/acceptance/user/UserAcceptanceTest.java @@ -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; @@ -159,21 +174,6 @@ class 추천_사용자 { assertThat(결과.get(0).nickname()).isEqualTo(유진.getNickname()); } - @Test - void 비회원이_추천_사용자_조회한다() { - // given - 회원을_저장한다(동호()); - 회원을_저장한다(정수()); - 회원을_저장한다(유진()); - - // when - List 결과 = 비회원_추천_API().as(new TypeRef<>() { - }); - - // then - assertThat(결과).hasSize(3); - } - @Test void 가장_최근에_리스트_생성_또는_수정한_사용자_순위대로_추천_사용자_조회한다() { // given