Skip to content

Commit

Permalink
refactor: List로 반환한 정보를 Page로 반환하도록 변경
Browse files Browse the repository at this point in the history
List to Page 변경
  • Loading branch information
yunjunghun0116 committed Aug 1, 2024
1 parent a8a4281 commit 33714c7
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 81 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 @@ -7,6 +7,7 @@
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 All @@ -21,7 +22,6 @@
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("/api/orders")
Expand Down Expand Up @@ -51,7 +51,7 @@ public ResponseEntity<GiftOrderResponse> getOrder(@PathVariable Long id) {
}

@GetMapping
public ResponseEntity<List<GiftOrderResponse>> getOrders(@RequestAttribute("memberId") Long memberId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
public ResponseEntity<Page<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
Expand Up @@ -6,6 +6,7 @@
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 All @@ -21,7 +22,6 @@
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("/api/products")
Expand Down Expand Up @@ -52,7 +52,7 @@ public ResponseEntity<ProductResponse> getProduct(@PathVariable Long id) {
}

@GetMapping
public ResponseEntity<List<ProductResponse>> getProducts(@RequestParam(required = false) Long categoryId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
public ResponseEntity<Page<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
Expand Up @@ -6,6 +6,7 @@
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 All @@ -21,7 +22,6 @@
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("/api/wishes")
Expand Down Expand Up @@ -52,7 +52,7 @@ public ResponseEntity<WishProductResponse> getWishProduct(@RequestAttribute("mem
}

@GetMapping
public ResponseEntity<List<WishProductResponse>> getWishProducts(@RequestAttribute("memberId") Long memberId, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
public ResponseEntity<Page<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
5 changes: 2 additions & 3 deletions src/main/java/gift/controller/api/GiftOrderApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
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;

import java.util.List;

@Tag(name = "상품 주문 API")
public interface GiftOrderApi {

Expand All @@ -39,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<List<GiftOrderResponse>> getOrders(Long memberId, Pageable pageable);
ResponseEntity<Page<GiftOrderResponse>> getOrders(Long memberId, Pageable pageable);

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

import java.util.List;

@Tag(name = "상품 API")
public interface ProductApi {

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

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

import java.util.List;

@Tag(name = "위시리스트 API")
public interface WishProductApi {

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

@Operation(summary = "회원의 위시 리스트에서 상품을 삭제한다.")
@ApiResponses(value = {
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/gift/dto/category/CategoryInformation.java

This file was deleted.

8 changes: 4 additions & 4 deletions src/main/java/gift/dto/product/ProductResponse.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gift.dto.product;

import com.fasterxml.jackson.annotation.JsonProperty;
import gift.dto.category.CategoryInformation;
import gift.dto.category.CategoryResponse;
import gift.dto.option.OptionResponse;

import java.util.List;
Expand All @@ -12,10 +12,10 @@ public record ProductResponse(
Integer price,
String imageUrl,
@JsonProperty("category")
CategoryInformation categoryInformation,
CategoryResponse categoryResponse,
List<OptionResponse> options
) {
public static ProductResponse of(Long id, String name, Integer price, String imageUrl, CategoryInformation categoryInformation, List<OptionResponse> options) {
return new ProductResponse(id, name, price, imageUrl, categoryInformation, options);
public static ProductResponse of(Long id, String name, Integer price, String imageUrl, CategoryResponse categoryResponse, List<OptionResponse> options) {
return new ProductResponse(id, name, price, imageUrl, categoryResponse, options);
}
}
5 changes: 2 additions & 3 deletions src/main/java/gift/repository/GiftOrderRepository.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package gift.repository;

import gift.model.GiftOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GiftOrderRepository extends JpaRepository<GiftOrder, Long> {

List<GiftOrder> findAllByMemberId(Long memberId, Pageable pageable);
Page<GiftOrder> findAllByMemberId(Long memberId, Pageable pageable);

void deleteAllByOptionId(Long optionId);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/gift/repository/ProductRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gift.repository;

import gift.model.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -11,5 +12,5 @@
public interface ProductRepository extends JpaRepository<Product, Long> {
List<Product> findAllByCategoryId(Long categoryId);

List<Product> findAllByCategoryId(Long categoryId, Pageable pageable);
Page<Product> findAllByCategoryId(Long categoryId, Pageable pageable);
}
4 changes: 2 additions & 2 deletions src/main/java/gift/repository/WishProductRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import gift.model.Member;
import gift.model.Product;
import gift.model.WishProduct;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
Expand All @@ -16,7 +16,7 @@ public interface WishProductRepository extends JpaRepository<WishProduct, Long>

Optional<WishProduct> findByProductAndMember(Product product, Member member);

List<WishProduct> findAllByMemberId(Long memberId, Pageable pageable);
Page<WishProduct> findAllByMemberId(Long memberId, Pageable pageable);

void deleteAllByProductId(Long id);

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/gift/service/GiftOrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
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;

import java.util.List;

@Service
@Transactional
public class GiftOrderService {
Expand Down Expand Up @@ -43,11 +43,15 @@ public GiftOrderResponse getGiftOrder(Long id) {
}

@Transactional(readOnly = true)
public List<GiftOrderResponse> getGiftOrders(Long memberId, Pageable pageable) {
return giftOrderRepository.findAllByMemberId(memberId, pageable)
public Page<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());
}

public void deleteOrder(Long id) {
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/gift/service/ProductService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gift.service;

import gift.dto.category.CategoryInformation;
import gift.dto.category.CategoryResponse;
import gift.dto.option.OptionRequest;
import gift.dto.product.ProductAddRequest;
import gift.dto.product.ProductResponse;
Expand All @@ -11,6 +11,8 @@
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 @@ -53,19 +55,27 @@ public ProductResponse getProduct(Long id) {
}

@Transactional(readOnly = true)
public List<ProductResponse> getProducts(Pageable pageable) {
return productRepository.findAll(pageable)
public Page<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());
}

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

public void deleteProduct(Long productId) {
Expand Down Expand Up @@ -108,17 +118,17 @@ private void productNameValidation(String name) {
}

private ProductResponse getProductResponseFromProduct(Product product) {
var categoryInformation = getCategoryInformationFromCategory(product.getCategory());
var categoryResponse = getCategoryResponseFromCategory(product.getCategory());
var options = optionService.getOptions(product.getId());
return ProductResponse.of(product.getId(), product.getName(), product.getPrice(), product.getImageUrl(), categoryInformation, options);
return ProductResponse.of(product.getId(), product.getName(), product.getPrice(), product.getImageUrl(), categoryResponse, options);
}

private Product findProductById(Long id) {
return productRepository.findById(id)
.orElseThrow(() -> new NotFoundElementException(id + "를 가진 상품옵션이 존재하지 않습니다."));
}

private CategoryInformation getCategoryInformationFromCategory(Category category) {
return CategoryInformation.of(category.getId(), category.getName());
private CategoryResponse getCategoryResponseFromCategory(Category category) {
return CategoryResponse.of(category.getId(), category.getName(), category.getDescription(), category.getColor(), category.getImageUrl());
}
}
12 changes: 8 additions & 4 deletions src/main/java/gift/service/WishProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
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;

import java.util.List;

@Service
@Transactional
public class WishProductService {
Expand Down Expand Up @@ -63,11 +63,15 @@ public WishProductResponse getWishProduct(Long memberId, Long id) {
}

@Transactional(readOnly = true)
public List<WishProductResponse> getWishProducts(Long memberId, Pageable pageable) {
return wishProductRepository.findAllByMemberId(memberId, pageable)
public Page<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());
}

public void deleteWishProduct(Long wishProductId) {
Expand Down
7 changes: 1 addition & 6 deletions src/test/java/gift/controller/ProductControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gift.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import gift.dto.auth.LoginRequest;
import gift.dto.option.OptionRequest;
Expand Down Expand Up @@ -210,11 +209,7 @@ void successGetProductsWithPageable() throws Exception {
//when
var getResult = mockMvc.perform(getRequest);
//then
var productResult = getResult.andExpect(status().isOk()).andReturn();
var productsString = productResult.getResponse().getContentAsString();
var productResponseResult = objectMapper.readValue(productsString, new TypeReference<List<ProductResponse>>() {
});
Assertions.assertThat(productResponseResult.size()).isEqualTo(4);
getResult.andExpect(status().isOk());

deleteProducts(productResponseList);
}
Expand Down
Loading

0 comments on commit 33714c7

Please sign in to comment.