-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: 특정 Products의 meal type 조회 기능의 컨트롤러 구현
- API : GET /api/products?meal={value} - 컨트롤러 테스트 구현 - 서비스 계층은 Mock 처리 - Product 도메인 객체 생성 - ProductsDtoMapper 객체 생성 : Product 도메인 <-> ProductsMealTypeResponse 변환
- Loading branch information
Showing
8 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
BE/src/main/java/sidedish/com/controller/ProductsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package sidedish.com.controller; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import javax.validation.constraints.NotEmpty; | ||
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 sidedish.com.controller.model.ProductsMealTypeResponse; | ||
import sidedish.com.domain.Product; | ||
import sidedish.com.service.ProductsService; | ||
|
||
@RestController | ||
@RequestMapping("/api/products") | ||
public class ProductsController { | ||
|
||
ProductsService productsService; | ||
|
||
public ProductsController(ProductsService productsService) { | ||
this.productsService = productsService; | ||
} | ||
|
||
@GetMapping | ||
public List<ProductsMealTypeResponse> findProductsMealType( | ||
@RequestParam @NotEmpty String meal) { | ||
List<Product> products = productsService.findByMealType(meal); | ||
|
||
return products.stream() | ||
.map(ProductsDtoMapper::toProductsMealTypeResponseFromDomain) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
BE/src/main/java/sidedish/com/controller/ProductsDtoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package sidedish.com.controller; | ||
|
||
import sidedish.com.controller.model.ProductsMealTypeResponse; | ||
import sidedish.com.domain.Product; | ||
|
||
public class ProductsDtoMapper { | ||
|
||
private ProductsDtoMapper() { | ||
} | ||
|
||
public static ProductsMealTypeResponse toProductsMealTypeResponseFromDomain(Product product) { | ||
return new ProductsMealTypeResponse(product.getId() | ||
, product.getImage().get(0).getImage(), | ||
product.getProductName(), | ||
product.getDescription(), | ||
product.getFixedPrice(), | ||
product.getOriginalPrice(), | ||
product.getDisCountPolicy().getPolicyName()); | ||
|
||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
BE/src/main/java/sidedish/com/controller/model/ProductsMealTypeResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package sidedish.com.controller.model; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class ProductsMealTypeResponse { | ||
|
||
private Long id; | ||
private String image; | ||
private String productName; | ||
private String description; | ||
private long fixedPrice; | ||
private long originalPrice; | ||
private String event; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package sidedish.com.domain; | ||
|
||
import java.time.LocalDateTime; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class DisCountPolicy { | ||
|
||
private Long id; | ||
private String policyName; | ||
private float discountRate; | ||
private LocalDateTime startAt; | ||
private LocalDateTime endAt; | ||
|
||
public DisCountPolicy(String policyName) { | ||
this.policyName = policyName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package sidedish.com.domain; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class Image { | ||
|
||
private String image; | ||
|
||
public Image(String image) { | ||
this.image = image; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package sidedish.com.domain; | ||
|
||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class Product { | ||
|
||
private Long id; | ||
private List<Image> image; | ||
private String productName; | ||
private String description; | ||
private long fixedPrice; | ||
private long originalPrice; | ||
private DisCountPolicy disCountPolicy; | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
BE/src/main/java/sidedish/com/service/ProductsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package sidedish.com.service; | ||
|
||
import java.util.List; | ||
import org.springframework.stereotype.Service; | ||
import sidedish.com.domain.Product; | ||
|
||
@Service | ||
public class ProductsService { | ||
|
||
public List<Product> findByMealType(String meal) { | ||
return null; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
BE/src/test/java/sidedish/com/controller/ProductsControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package sidedish.com.controller; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
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.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import sidedish.com.domain.DisCountPolicy; | ||
import sidedish.com.domain.Image; | ||
import sidedish.com.domain.Product; | ||
import sidedish.com.service.ProductsService; | ||
|
||
@WebMvcTest(ProductsController.class) | ||
@DisplayName("ProductsControllerTest 클래스") | ||
class ProductsControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private ProductsService productsService; | ||
|
||
@Test | ||
void 만약_meal_type이_soup인_경우_soup음식_타입_조회_성공() throws Exception { | ||
given(productsService.findByMealType("soup")) | ||
.willReturn(createListProducts()); | ||
|
||
ResultActions perform = mockMvc.perform(get("/api/products?meal=soup")); | ||
|
||
perform | ||
.andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)); | ||
} | ||
|
||
private List<Product> createListProducts() { | ||
List<Product> products = new ArrayList<>(); | ||
|
||
products.add(new Product(5L, List.of(new Image("http://kukukukukukukukuku.com/test.jpg")), | ||
"한돈 돼지 김치찌개", "김치찌개에는 역시 돼지고기", 8370, 9300, new DisCountPolicy("이벤트특가"))); | ||
products.add(new Product(6L, List.of(new Image("http://kukukukukukukukuku.com/test2.jpg")), | ||
"하하하 테스트 음식", "맛있어요", 8370, 9300, new DisCountPolicy("이벤트특가"))); | ||
|
||
return products; | ||
} | ||
} |