Skip to content

Commit

Permalink
refactor: Page -> PageResponse 라는 DTO를 만들어 반환하도록 변경
Browse files Browse the repository at this point in the history
DTO로 응답 타입 변환 과정 거침
  • Loading branch information
yunjunghun0116 committed Aug 1, 2024
1 parent 33714c7 commit 516b4e0
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/main/java/gift/controller/GiftOrderController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +51,7 @@ public ResponseEntity<GiftOrderResponse> getOrder(@PathVariable Long id) {
}

@GetMapping
public ResponseEntity<Page<GiftOrderResponse>> getOrders(@RequestAttribute("memberId") Long memberId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
public ResponseEntity<PageResponse<GiftOrderResponse>> getOrders(@RequestAttribute("memberId") Long memberId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
var orders = giftOrderService.getGiftOrders(memberId, pageable);
return ResponseEntity.ok(orders);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gift/controller/ProductController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -52,7 +52,7 @@ public ResponseEntity<ProductResponse> getProduct(@PathVariable Long id) {
}

@GetMapping
public ResponseEntity<Page<ProductResponse>> getProducts(@RequestParam(required = false) Long categoryId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
public ResponseEntity<PageResponse<ProductResponse>> 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);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gift/controller/WishProductController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -52,7 +52,7 @@ public ResponseEntity<WishProductResponse> getWishProduct(@RequestAttribute("mem
}

@GetMapping
public ResponseEntity<Page<WishProductResponse>> getWishProducts(@RequestAttribute("memberId") Long memberId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
public ResponseEntity<PageResponse<WishProductResponse>> getWishProducts(@RequestAttribute("memberId") Long memberId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
var wishProducts = wishProductService.getWishProducts(memberId, pageable);
return ResponseEntity.ok(wishProducts);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gift/controller/api/GiftOrderApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

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;
import io.swagger.v3.oas.annotations.media.Schema;
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;

Expand Down Expand Up @@ -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<Page<GiftOrderResponse>> getOrders(Long memberId, Pageable pageable);
ResponseEntity<PageResponse<GiftOrderResponse>> getOrders(Long memberId, Pageable pageable);

@Operation(summary = "특정 주문을 삭제한다.")
@ApiResponses(value = {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gift/controller/api/ProductApi.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<Page<ProductResponse>> getProducts(Long categoryId, Pageable pageable);
ResponseEntity<PageResponse<ProductResponse>> getProducts(Long categoryId, Pageable pageable);

@Operation(summary = "특정 상품을 삭제한다.")
@ApiResponses(value = {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gift/controller/api/WishProductApi.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<Page<WishProductResponse>> getWishProducts(Long memberId, Pageable pageable);
ResponseEntity<PageResponse<WishProductResponse>> getWishProducts(Long memberId, Pageable pageable);

@Operation(summary = "회원의 위시 리스트에서 상품을 삭제한다.")
@ApiResponses(value = {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/gift/dto/page/PageResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gift.dto.page;

import java.util.List;

public record PageResponse<T>(Integer page, Integer size, Long totalElements, Integer totalPages, List<T> content) {
}
7 changes: 3 additions & 4 deletions src/main/java/gift/service/GiftOrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,15 +42,15 @@ public GiftOrderResponse getGiftOrder(Long id) {
}

@Transactional(readOnly = true)
public Page<GiftOrderResponse> getGiftOrders(Long memberId, Pageable pageable) {
public PageResponse<GiftOrderResponse> getGiftOrders(Long memberId, Pageable pageable) {
var pageResult = giftOrderRepository.findAllByMemberId(memberId, pageable);
var orders = pageResult
.getContent()
.stream()
.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) {
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/gift/service/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -55,27 +54,26 @@ public ProductResponse getProduct(Long id) {
}

@Transactional(readOnly = true)
public Page<ProductResponse> getProducts(Pageable pageable) {
public PageResponse<ProductResponse> 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<ProductResponse> getProducts(Long categoryId, Pageable pageable) {
public PageResponse<ProductResponse> getProducts(Long categoryId, Pageable pageable) {
var pageResult = productRepository.findAllByCategoryId(categoryId, 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);
}

public void deleteProduct(Long productId) {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/gift/service/WishProductService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -63,15 +62,15 @@ public WishProductResponse getWishProduct(Long memberId, Long id) {
}

@Transactional(readOnly = true)
public Page<WishProductResponse> getWishProducts(Long memberId, Pageable pageable) {
public PageResponse<WishProductResponse> getWishProducts(Long memberId, Pageable pageable) {
var pageResult = wishProductRepository.findAllByMemberId(memberId, pageable);
var wishProducts = pageResult
.getContent()
.stream()
.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) {
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/gift/controller/WishProductControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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
Expand All @@ -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());
}
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/gift/service/WishProductServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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());
Expand All @@ -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());
}
Expand All @@ -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());
Expand Down

0 comments on commit 516b4e0

Please sign in to comment.