From 516b4e0c558e174a081c079ba9b6999cd7a66e38 Mon Sep 17 00:00:00 2001 From: yunjunghun0116 Date: Thu, 1 Aug 2024 15:11:53 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20Page=20->=20PageResponse=20?= =?UTF-8?q?=EB=9D=BC=EB=8A=94=20DTO=EB=A5=BC=20=EB=A7=8C=EB=93=A4=EC=96=B4?= =?UTF-8?q?=20=EB=B0=98=ED=99=98=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DTO로 응답 타입 변환 과정 거침 --- .../gift/controller/GiftOrderController.java | 4 ++-- .../gift/controller/ProductController.java | 4 ++-- .../controller/WishProductController.java | 4 ++-- .../gift/controller/api/GiftOrderApi.java | 4 ++-- .../java/gift/controller/api/ProductApi.java | 4 ++-- .../gift/controller/api/WishProductApi.java | 4 ++-- src/main/java/gift/dto/page/PageResponse.java | 6 ++++++ .../java/gift/service/GiftOrderService.java | 7 +++---- .../java/gift/service/ProductService.java | 12 +++++------ .../java/gift/service/WishProductService.java | 7 +++---- .../controller/WishProductControllerTest.java | 14 ++++++------- .../gift/service/WishProductServiceTest.java | 20 +++++++++---------- 12 files changed, 46 insertions(+), 44 deletions(-) create mode 100644 src/main/java/gift/dto/page/PageResponse.java diff --git a/src/main/java/gift/controller/GiftOrderController.java b/src/main/java/gift/controller/GiftOrderController.java index 5249c1e37..dbc4b10f1 100644 --- a/src/main/java/gift/controller/GiftOrderController.java +++ b/src/main/java/gift/controller/GiftOrderController.java @@ -3,11 +3,11 @@ import gift.controller.api.GiftOrderApi; import gift.dto.giftorder.GiftOrderRequest; import gift.dto.giftorder.GiftOrderResponse; +import gift.dto.page.PageResponse; import gift.service.GiftOrderService; import gift.service.KakaoService; import gift.service.OptionService; import jakarta.validation.Valid; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.web.PageableDefault; @@ -51,7 +51,7 @@ public ResponseEntity getOrder(@PathVariable Long id) { } @GetMapping - public ResponseEntity> getOrders(@RequestAttribute("memberId") Long memberId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) { + public ResponseEntity> getOrders(@RequestAttribute("memberId") Long memberId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) { var orders = giftOrderService.getGiftOrders(memberId, pageable); return ResponseEntity.ok(orders); } diff --git a/src/main/java/gift/controller/ProductController.java b/src/main/java/gift/controller/ProductController.java index b18b67656..4fd28e4cf 100644 --- a/src/main/java/gift/controller/ProductController.java +++ b/src/main/java/gift/controller/ProductController.java @@ -1,12 +1,12 @@ package gift.controller; import gift.controller.api.ProductApi; +import gift.dto.page.PageResponse; import gift.dto.product.ProductAddRequest; import gift.dto.product.ProductResponse; import gift.dto.product.ProductUpdateRequest; import gift.service.ProductService; import jakarta.validation.Valid; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.web.PageableDefault; @@ -52,7 +52,7 @@ public ResponseEntity getProduct(@PathVariable Long id) { } @GetMapping - public ResponseEntity> getProducts(@RequestParam(required = false) Long categoryId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) { + public ResponseEntity> getProducts(@RequestParam(required = false) Long categoryId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) { if (categoryId == null) { var products = productService.getProducts(pageable); return ResponseEntity.ok(products); diff --git a/src/main/java/gift/controller/WishProductController.java b/src/main/java/gift/controller/WishProductController.java index f95c5d75f..595421354 100644 --- a/src/main/java/gift/controller/WishProductController.java +++ b/src/main/java/gift/controller/WishProductController.java @@ -1,12 +1,12 @@ package gift.controller; import gift.controller.api.WishProductApi; +import gift.dto.page.PageResponse; import gift.dto.wishproduct.WishProductAddRequest; import gift.dto.wishproduct.WishProductResponse; import gift.dto.wishproduct.WishProductUpdateRequest; import gift.service.WishProductService; import jakarta.validation.Valid; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.web.PageableDefault; @@ -52,7 +52,7 @@ public ResponseEntity getWishProduct(@RequestAttribute("mem } @GetMapping - public ResponseEntity> getWishProducts(@RequestAttribute("memberId") Long memberId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) { + public ResponseEntity> getWishProducts(@RequestAttribute("memberId") Long memberId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) { var wishProducts = wishProductService.getWishProducts(memberId, pageable); return ResponseEntity.ok(wishProducts); } diff --git a/src/main/java/gift/controller/api/GiftOrderApi.java b/src/main/java/gift/controller/api/GiftOrderApi.java index f1d1c5ebd..9760ca130 100644 --- a/src/main/java/gift/controller/api/GiftOrderApi.java +++ b/src/main/java/gift/controller/api/GiftOrderApi.java @@ -2,6 +2,7 @@ import gift.dto.giftorder.GiftOrderRequest; import gift.dto.giftorder.GiftOrderResponse; +import gift.dto.page.PageResponse; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.ArraySchema; import io.swagger.v3.oas.annotations.media.Content; @@ -9,7 +10,6 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; @@ -38,7 +38,7 @@ public interface GiftOrderApi { @ApiResponse(responseCode = "401", description = "허용되지 않는 요청", content = @Content(schema = @Schema(hidden = true))), @ApiResponse(responseCode = "500", description = "내부 서버의 오류", content = @Content(schema = @Schema(hidden = true))) }) - ResponseEntity> getOrders(Long memberId, Pageable pageable); + ResponseEntity> getOrders(Long memberId, Pageable pageable); @Operation(summary = "특정 주문을 삭제한다.") @ApiResponses(value = { diff --git a/src/main/java/gift/controller/api/ProductApi.java b/src/main/java/gift/controller/api/ProductApi.java index 23465aca7..f4ebecff1 100644 --- a/src/main/java/gift/controller/api/ProductApi.java +++ b/src/main/java/gift/controller/api/ProductApi.java @@ -1,5 +1,6 @@ package gift.controller.api; +import gift.dto.page.PageResponse; import gift.dto.product.ProductAddRequest; import gift.dto.product.ProductResponse; import gift.dto.product.ProductUpdateRequest; @@ -10,7 +11,6 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; @@ -49,7 +49,7 @@ public interface ProductApi { @ApiResponse(responseCode = "401", description = "허용되지 않는 요청", content = @Content(schema = @Schema(hidden = true))), @ApiResponse(responseCode = "500", description = "내부 서버의 오류", content = @Content(schema = @Schema(hidden = true))) }) - ResponseEntity> getProducts(Long categoryId, Pageable pageable); + ResponseEntity> getProducts(Long categoryId, Pageable pageable); @Operation(summary = "특정 상품을 삭제한다.") @ApiResponses(value = { diff --git a/src/main/java/gift/controller/api/WishProductApi.java b/src/main/java/gift/controller/api/WishProductApi.java index 537fbd2f4..37356e2c3 100644 --- a/src/main/java/gift/controller/api/WishProductApi.java +++ b/src/main/java/gift/controller/api/WishProductApi.java @@ -1,6 +1,7 @@ package gift.controller.api; import gift.dto.option.OptionResponse; +import gift.dto.page.PageResponse; import gift.dto.wishproduct.WishProductAddRequest; import gift.dto.wishproduct.WishProductResponse; import gift.dto.wishproduct.WishProductUpdateRequest; @@ -11,7 +12,6 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; @@ -49,7 +49,7 @@ public interface WishProductApi { @ApiResponse(responseCode = "401", description = "허용되지 않는 요청", content = @Content(schema = @Schema(hidden = true))), @ApiResponse(responseCode = "500", description = "내부 서버의 오류", content = @Content(schema = @Schema(hidden = true))) }) - ResponseEntity> getWishProducts(Long memberId, Pageable pageable); + ResponseEntity> getWishProducts(Long memberId, Pageable pageable); @Operation(summary = "회원의 위시 리스트에서 상품을 삭제한다.") @ApiResponses(value = { diff --git a/src/main/java/gift/dto/page/PageResponse.java b/src/main/java/gift/dto/page/PageResponse.java new file mode 100644 index 000000000..d3b695ba6 --- /dev/null +++ b/src/main/java/gift/dto/page/PageResponse.java @@ -0,0 +1,6 @@ +package gift.dto.page; + +import java.util.List; + +public record PageResponse(Integer page, Integer size, Long totalElements, Integer totalPages, List content) { +} diff --git a/src/main/java/gift/service/GiftOrderService.java b/src/main/java/gift/service/GiftOrderService.java index 4af2fdbff..ba5a5a116 100644 --- a/src/main/java/gift/service/GiftOrderService.java +++ b/src/main/java/gift/service/GiftOrderService.java @@ -3,14 +3,13 @@ import gift.dto.giftorder.GiftOrderRequest; import gift.dto.giftorder.GiftOrderResponse; import gift.dto.option.OptionResponse; +import gift.dto.page.PageResponse; import gift.dto.product.ProductBasicInformation; import gift.exception.NotFoundElementException; import gift.model.GiftOrder; import gift.model.Option; import gift.repository.GiftOrderRepository; import gift.repository.MemberRepository; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -43,7 +42,7 @@ public GiftOrderResponse getGiftOrder(Long id) { } @Transactional(readOnly = true) - public Page getGiftOrders(Long memberId, Pageable pageable) { + public PageResponse getGiftOrders(Long memberId, Pageable pageable) { var pageResult = giftOrderRepository.findAllByMemberId(memberId, pageable); var orders = pageResult .getContent() @@ -51,7 +50,7 @@ public Page getGiftOrders(Long memberId, Pageable pageable) { .map(this::getGiftOrderResponseFromGiftOrder) .toList(); - return new PageImpl<>(orders, pageable, pageResult.getTotalElements()); + return new PageResponse<>(pageResult.getNumber(), pageResult.getSize(), pageResult.getTotalElements(), pageResult.getTotalPages(), orders); } public void deleteOrder(Long id) { diff --git a/src/main/java/gift/service/ProductService.java b/src/main/java/gift/service/ProductService.java index c8dda1a03..1ffe68f1e 100644 --- a/src/main/java/gift/service/ProductService.java +++ b/src/main/java/gift/service/ProductService.java @@ -2,6 +2,7 @@ import gift.dto.category.CategoryResponse; import gift.dto.option.OptionRequest; +import gift.dto.page.PageResponse; import gift.dto.product.ProductAddRequest; import gift.dto.product.ProductResponse; import gift.dto.product.ProductUpdateRequest; @@ -11,8 +12,6 @@ import gift.model.Product; import gift.repository.CategoryRepository; import gift.repository.ProductRepository; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -55,19 +54,18 @@ public ProductResponse getProduct(Long id) { } @Transactional(readOnly = true) - public Page getProducts(Pageable pageable) { + public PageResponse getProducts(Pageable pageable) { var pageResult = productRepository.findAll(pageable); var products = pageResult .getContent() .stream() .map(this::getProductResponseFromProduct) .toList(); - - return new PageImpl<>(products, pageable, pageResult.getTotalElements()); + return new PageResponse<>(pageResult.getNumber(), pageResult.getSize(), pageResult.getTotalElements(), pageResult.getTotalPages(), products); } @Transactional(readOnly = true) - public Page getProducts(Long categoryId, Pageable pageable) { + public PageResponse getProducts(Long categoryId, Pageable pageable) { var pageResult = productRepository.findAllByCategoryId(categoryId, pageable); var products = pageResult .getContent() @@ -75,7 +73,7 @@ public Page getProducts(Long categoryId, Pageable pageable) { .map(this::getProductResponseFromProduct) .toList(); - return new PageImpl<>(products, pageable, pageResult.getTotalElements()); + return new PageResponse<>(pageResult.getNumber(), pageResult.getSize(), pageResult.getTotalElements(), pageResult.getTotalPages(), products); } public void deleteProduct(Long productId) { diff --git a/src/main/java/gift/service/WishProductService.java b/src/main/java/gift/service/WishProductService.java index 5a11b893a..37248c925 100644 --- a/src/main/java/gift/service/WishProductService.java +++ b/src/main/java/gift/service/WishProductService.java @@ -1,5 +1,6 @@ package gift.service; +import gift.dto.page.PageResponse; import gift.dto.product.ProductBasicInformation; import gift.dto.wishproduct.WishProductAddRequest; import gift.dto.wishproduct.WishProductResponse; @@ -12,8 +13,6 @@ import gift.repository.MemberRepository; import gift.repository.ProductRepository; import gift.repository.WishProductRepository; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -63,7 +62,7 @@ public WishProductResponse getWishProduct(Long memberId, Long id) { } @Transactional(readOnly = true) - public Page getWishProducts(Long memberId, Pageable pageable) { + public PageResponse getWishProducts(Long memberId, Pageable pageable) { var pageResult = wishProductRepository.findAllByMemberId(memberId, pageable); var wishProducts = pageResult .getContent() @@ -71,7 +70,7 @@ public Page getWishProducts(Long memberId, Pageable pageabl .map(this::getWishProductResponseFromWishProduct) .toList(); - return new PageImpl<>(wishProducts, pageable, pageResult.getTotalElements()); + return new PageResponse<>(pageResult.getNumber(), pageResult.getSize(), pageResult.getTotalElements(), pageResult.getTotalPages(), wishProducts); } public void deleteWishProduct(Long wishProductId) { diff --git a/src/test/java/gift/controller/WishProductControllerTest.java b/src/test/java/gift/controller/WishProductControllerTest.java index fff8bc66a..8e306af14 100644 --- a/src/test/java/gift/controller/WishProductControllerTest.java +++ b/src/test/java/gift/controller/WishProductControllerTest.java @@ -117,8 +117,8 @@ void successAddWishProductAlreadyExists() throws Exception { //then addResult.andExpect(status().isCreated()); var wishProducts = wishProductService.getWishProducts(1L, PageRequest.of(0, 10)); - Assertions.assertThat(wishProducts.getContent().size()).isEqualTo(1); - Assertions.assertThat(wishProducts.getContent().get(0).quantity()).isEqualTo(20); + Assertions.assertThat(wishProducts.content().size()).isEqualTo(1); + Assertions.assertThat(wishProducts.content().get(0).quantity()).isEqualTo(20); wishProductService.deleteWishProduct(wishProduct.id()); } @@ -141,9 +141,9 @@ void successGetDifferentWishProducts() throws Exception { var managerWishLength = managerWishResult.getResponse().getContentLength(); Assertions.assertThat(managerWishLength).isEqualTo(0); var memberWishProducts = wishProductService.getWishProducts(1L, PageRequest.of(0, 10)); - Assertions.assertThat(memberWishProducts.getContent().size()).isEqualTo(2); + Assertions.assertThat(memberWishProducts.content().size()).isEqualTo(2); - deleteWishProducts(memberWishProducts.getContent()); + deleteWishProducts(memberWishProducts.content()); } @Test @@ -161,8 +161,8 @@ void successUpdateQuantity() throws Exception { //then result.andExpect(status().isNoContent()); var wishProducts = wishProductService.getWishProducts(1L, PageRequest.of(0, 10)); - Assertions.assertThat(wishProducts.getContent().size()).isEqualTo(1); - Assertions.assertThat(wishProducts.getContent().get(0).quantity()).isEqualTo(30); + Assertions.assertThat(wishProducts.content().size()).isEqualTo(1); + Assertions.assertThat(wishProducts.content().get(0).quantity()).isEqualTo(30); wishProductService.deleteWishProduct(wishProduct.id()); } @@ -183,7 +183,7 @@ void successUpdateQuantityZero() throws Exception { //then result.andExpect(status().isNoContent()); var wishProducts = wishProductService.getWishProducts(1L, PageRequest.of(0, 10)); - Assertions.assertThat(wishProducts.getContent().size()).isEqualTo(0); + Assertions.assertThat(wishProducts.content().size()).isEqualTo(0); } @Test diff --git a/src/test/java/gift/service/WishProductServiceTest.java b/src/test/java/gift/service/WishProductServiceTest.java index 1fb6b5df0..0a7724374 100644 --- a/src/test/java/gift/service/WishProductServiceTest.java +++ b/src/test/java/gift/service/WishProductServiceTest.java @@ -23,11 +23,11 @@ class WishProductServiceTest { void successAddWishProduct() { //given var wishProductAddRequest = new WishProductAddRequest(1L, 5); - Assertions.assertThat(wishProductService.getWishProducts(1L, PageRequest.of(0, 10)).getContent().size()).isEqualTo(0); + Assertions.assertThat(wishProductService.getWishProducts(1L, PageRequest.of(0, 10)).content().size()).isEqualTo(0); //when var wishProduct = wishProductService.addWishProduct(wishProductAddRequest, 1L); //then - Assertions.assertThat(wishProductService.getWishProducts(1L, PageRequest.of(0, 10)).getContent().size()).isEqualTo(1); + Assertions.assertThat(wishProductService.getWishProducts(1L, PageRequest.of(0, 10)).content().size()).isEqualTo(1); wishProductService.deleteWishProduct(wishProduct.id()); } @@ -38,11 +38,11 @@ void successDeleteWishProduct() { //given var wishProductAddRequest = new WishProductAddRequest(1L, 5); var wishProduct = wishProductService.addWishProduct(wishProductAddRequest, 1L); - Assertions.assertThat(wishProductService.getWishProducts(1L, PageRequest.of(0, 10)).getContent().size()).isEqualTo(1); + Assertions.assertThat(wishProductService.getWishProducts(1L, PageRequest.of(0, 10)).content().size()).isEqualTo(1); //when wishProductService.deleteWishProduct(wishProduct.id()); //then - Assertions.assertThat(wishProductService.getWishProducts(1L, PageRequest.of(0, 10)).getContent().size()).isEqualTo(0); + Assertions.assertThat(wishProductService.getWishProducts(1L, PageRequest.of(0, 10)).content().size()).isEqualTo(0); } @Test @@ -51,12 +51,12 @@ void successUpdateWishProductWithZeroQuantity() { //given var wishProductAddRequest = new WishProductAddRequest(1L, 5); var wishProduct = wishProductService.addWishProduct(wishProductAddRequest, 1L); - Assertions.assertThat(wishProductService.getWishProducts(1L, PageRequest.of(0, 10)).getContent().size()).isEqualTo(1); + Assertions.assertThat(wishProductService.getWishProducts(1L, PageRequest.of(0, 10)).content().size()).isEqualTo(1); var wishProductUpdateRequest = new WishProductUpdateRequest(0); //when wishProductService.updateWishProduct(wishProduct.id(), wishProductUpdateRequest); //then - Assertions.assertThat(wishProductService.getWishProducts(1L, PageRequest.of(0, 10)).getContent().size()).isEqualTo(0); + Assertions.assertThat(wishProductService.getWishProducts(1L, PageRequest.of(0, 10)).content().size()).isEqualTo(0); } @Test @@ -70,7 +70,7 @@ void successGetDifferentWishProducts() { //when var memberWishProducts = wishProductService.getWishProducts(2L, PageRequest.of(0, 10)); //then - Assertions.assertThat(memberWishProducts.getContent().size()).isEqualTo(0); + Assertions.assertThat(memberWishProducts.content().size()).isEqualTo(0); wishProductService.deleteWishProduct(managerWishProduct1.id()); wishProductService.deleteWishProduct(managerWishProduct2.id()); @@ -96,8 +96,8 @@ void successAddWishProductAlreadyExistsWishProduct() { var wishProduct = wishProductService.addWishProduct(wishProduct1AddRequest, 1L); //then var wishProducts = wishProductService.getWishProducts(1L, PageRequest.of(0, 10)); - Assertions.assertThat(wishProducts.getContent().size()).isEqualTo(1); - Assertions.assertThat(wishProducts.getContent().get(0).quantity()).isEqualTo(10); + Assertions.assertThat(wishProducts.content().size()).isEqualTo(1); + Assertions.assertThat(wishProducts.content().get(0).quantity()).isEqualTo(10); wishProductService.deleteWishProduct(wishProduct.id()); } @@ -113,7 +113,7 @@ void successGetProductsWishPageSizeOne() { //when var wishProducts = wishProductService.getWishProducts(1L, PageRequest.of(0, 1)); //then - Assertions.assertThat(wishProducts.getContent().size()).isEqualTo(1); + Assertions.assertThat(wishProducts.content().size()).isEqualTo(1); wishProductService.deleteWishProduct(wishProduct1.id()); wishProductService.deleteWishProduct(wishProduct2.id());