Skip to content

Commit

Permalink
Merge pull request #21 from lotteon2/O2F-476/feat/OOTD-feign
Browse files Browse the repository at this point in the history
  • Loading branch information
CokeLee777 authored Dec 1, 2023
2 parents 181e5b6 + be9db92 commit a95deeb
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dailyon.productservice.product.controller;

import com.dailyon.productservice.product.dto.response.ReadOOTDProductListResponse;
import com.dailyon.productservice.product.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/clients")
@RequiredArgsConstructor
public class ProductFeignController {
private final ProductService productService;

@GetMapping("/post-image/products")
ResponseEntity<ReadOOTDProductListResponse> readOOTDProductDetail(@RequestParam List<Long> id) {
return ResponseEntity.status(HttpStatus.OK).body(productService.readOOTDProductDetails(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.dailyon.productservice.product.dto.response;

import com.dailyon.productservice.product.entity.Product;
import lombok.Builder;
import lombok.Getter;

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

@Getter
@Builder
public class ReadOOTDProductListResponse {
private List<ReadOOTDProductResponse> productInfos;

public static ReadOOTDProductListResponse fromEntity(List<Product> products) {
return ReadOOTDProductListResponse.builder()
.productInfos(products.stream()
.map(ReadOOTDProductResponse::fromEntity)
.collect(Collectors.toList()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.dailyon.productservice.product.dto.response;

import com.dailyon.productservice.product.entity.Product;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class ReadOOTDProductResponse {
private Long id;
private String name;
private String brandName;
private String imgUrl;
private Integer price;

public static ReadOOTDProductResponse fromEntity(Product product) {
return ReadOOTDProductResponse.builder()
.id(product.getId())
.brandName(product.getBrand().getName())
.imgUrl(product.getImgUrl())
.price(product.getPrice())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

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

@Repository
Expand All @@ -23,4 +24,9 @@ public interface ProductRepository extends JpaRepository<Product, Long>, Product
Optional<Product> findProductDetailById(Long id);
Optional<Product> findByIdAndDeletedIsFalse(Long id);
Optional<Product> findProductByCode(String code);
@Query(value = "SELECT p " +
"FROM Product p " +
"JOIN FETCH p.brand " +
"WHERE p.id IN :id AND p.deleted = false")
List<Product> findOOTDProductDetails(List<Long> id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,8 @@ public ReadProductSliceResponse readProductSlice(Long brandId, Long categoryId,
public ReadProductPageResponse readProductPage(Long brandId, Long categoryId, ProductType type, Pageable pageable) {
return ReadProductPageResponse.fromEntity(productRepository.findProductPage(brandId, categoryId, type, pageable));
}

public ReadOOTDProductListResponse readOOTDProductDetails(List<Long> id) {
return ReadOOTDProductListResponse.fromEntity(productRepository.findOOTDProductDetails(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.dailyon.productservice.controller.product;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

@SpringBootTest
@Transactional
@AutoConfigureMockMvc
@ActiveProfiles(value = {"test"})
public class ProductFeignControllerTests {
@Autowired
MockMvc mockMvc;

@Test
@DisplayName("query parameter를 반드시 입력해야 한다")
void readOOTDProductDetailFail() throws Exception {
// given, when
ResultActions resultActions = mockMvc.perform(
get("/clients/post-image/products")
);

// then
resultActions.andExpect(MockMvcResultMatchers.status().isBadRequest());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
@Transactional
@ActiveProfiles(value = {"test"})
Expand Down Expand Up @@ -61,4 +64,38 @@ void createProductSuccess() {
assertEquals(price, product.getPrice());
assertEquals(ProductType.NORMAL, product.getType());
}

@Test
@DisplayName("OOTD 상품 상세 정보 조회")
void readOOTDProductDetails() {
// given
String imgUrl = "imgUrl";
Integer price = 1000;
Product product1 = productRepository.save(Product.create(
brand, category, ProductType.NORMAL, Gender.COMMON,
"name", "code", imgUrl, price
));

Product product2 = productRepository.save(Product.create(
brand, category, ProductType.NORMAL, Gender.COMMON,
"name1", "code1", imgUrl, price
));

Product product3 = productRepository.save(Product.create(
brand, category, ProductType.NORMAL, Gender.COMMON,
"name2", "code2", imgUrl, price
));

List<Long> id = new ArrayList<>();
id.add(product1.getId());
id.add(product2.getId());
id.add(product3.getId());

// when

List<Product> productInfos = productRepository.findOOTDProductDetails(id);

// then
assertEquals(productInfos.size(), id.size());
}
}

0 comments on commit a95deeb

Please sign in to comment.