Skip to content

Commit

Permalink
โ™ป๏ธ change package for category recipe search
Browse files Browse the repository at this point in the history
  • Loading branch information
hyxrxn committed Jul 27, 2024
1 parent b10a175 commit 808e701
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 170 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,11 @@ public class CategoryService {

private final CategoryRepository categoryRepository;
private final CategoryRecipeRepository categoryRecipeRepository;
private final RecipeRepository recipeRepository;
private final RecipeService recipeService;

public void saveCategories(Recipe recipe, List<String> categories) {
categories.forEach(category -> saveCategoryRecipe(recipe, category));
}

public List<MainRecipeResponse> readRecipesOfCategory(RecipeOfCategoryRequest request) {
String categoryName = request.category();
Pageable pageable = PageRequest.of(request.pageNumber(), request.pageSize());
List<Long> recipeIds = categoryRecipeRepository.findRecipeIdsByCategoryName(categoryName, pageable);

List<RecipeDataResponse> recipeDataResponses = recipeRepository.findRecipeData(recipeIds);
return recipeService.convertToMainRecipeResponses(recipeDataResponses);
}

private void saveCategoryRecipe(Recipe recipe, String name) {
Category category = categoryRepository.findByName(name)
.orElseGet(() -> categoryRepository.save(new Category(name)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import net.pengcook.category.dto.RecipeOfCategoryRequest;
import net.pengcook.recipe.dto.MainRecipeResponse;
import net.pengcook.recipe.dto.RecipeStepResponse;
import net.pengcook.recipe.service.RecipeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -27,4 +29,9 @@ public List<MainRecipeResponse> readRecipes(@RequestParam int pageNumber, @Reque
public List<RecipeStepResponse> readRecipeSteps(@PathVariable long id) {
return recipeService.readRecipeSteps(id);
}

@GetMapping("/search")
public List<MainRecipeResponse> readRecipesOfCategory(@ModelAttribute RecipeOfCategoryRequest request) {
return recipeService.readRecipesOfCategory(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import net.pengcook.category.dto.RecipeOfCategoryRequest;
import net.pengcook.category.repository.CategoryRecipeRepository;
import net.pengcook.recipe.domain.RecipeStep;
import net.pengcook.recipe.dto.AuthorResponse;
import net.pengcook.recipe.dto.CategoryResponse;
Expand All @@ -24,6 +26,7 @@ public class RecipeService {

private final RecipeRepository recipeRepository;
private final RecipeStepRepository recipeStepRepository;
private final CategoryRecipeRepository categoryRecipeRepository;

public List<MainRecipeResponse> readRecipes(int pageNumber, int pageSize) {
Pageable pageable = PageRequest.of(pageNumber, pageSize);
Expand All @@ -38,6 +41,15 @@ public List<RecipeStepResponse> readRecipeSteps(long id) {
return convertToRecipeStepResponses(recipeSteps);
}

public List<MainRecipeResponse> readRecipesOfCategory(RecipeOfCategoryRequest request) {
String categoryName = request.category();
Pageable pageable = PageRequest.of(request.pageNumber(), request.pageSize());
List<Long> recipeIds = categoryRecipeRepository.findRecipeIdsByCategoryName(categoryName, pageable);

List<RecipeDataResponse> recipeDataResponses = recipeRepository.findRecipeData(recipeIds);
return convertToMainRecipeResponses(recipeDataResponses);
}

private List<RecipeStepResponse> convertToRecipeStepResponses(List<RecipeStep> recipeSteps) {
return recipeSteps.stream()
.map(RecipeStepResponse::new)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Stream;
import net.pengcook.category.dto.RecipeOfCategoryRequest;
import net.pengcook.category.repository.CategoryRecipeRepository;
import net.pengcook.category.repository.CategoryRepository;
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.recipe.dto.MainRecipeResponse;
import net.pengcook.recipe.service.RecipeService;
import net.pengcook.user.domain.User;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
Expand Down Expand Up @@ -52,27 +46,4 @@ void saveCategories() {
() -> assertThat(categoryRecipeRepository.count()).isEqualTo(INITIAL_CATEGORY_RECIPE_COUNT + 2)
);
}

@ParameterizedTest
@MethodSource("provideParameters")
@DisplayName("ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋ ˆ์‹œํ”ผ๋ฅผ ์ฐพ๋Š”๋‹ค.")
void readRecipesOfCategory(int pageNumber, int pageSize, List<Long> expected) {
RecipeOfCategoryRequest request = new RecipeOfCategoryRequest("ํ•œ์‹", pageNumber, pageSize);

List<MainRecipeResponse> mainRecipeResponses = categoryService.readRecipesOfCategory(request);
List<Long> actual = mainRecipeResponses.stream().map(MainRecipeResponse::recipeId).toList();

assertAll(
() -> assertThat(actual.size()).isEqualTo(pageSize),
() -> assertThat(actual).containsAll(expected)
);
}

static Stream<Arguments> provideParameters() {
return Stream.of(
Arguments.of(0, 2, List.of(2L, 3L)),
Arguments.of(1, 2, List.of(7L, 9L)),
Arguments.of(1, 3, List.of(9L, 14L, 15L))
);
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
package net.pengcook.recipe.controller;

import static com.epages.restdocs.apispec.RestAssuredRestDocumentationWrapper.document;
import static org.hamcrest.Matchers.is;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.queryParameters;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
import net.pengcook.RestDocsSetting;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.context.jdbc.Sql;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Sql(value = "/data/recipe.sql")
class RecipeControllerTest {

@LocalServerPort
private int port;

@BeforeEach
void setUp() {
RestAssured.port = port;
}
class RecipeControllerTest extends RestDocsSetting {

@Test
@DisplayName("๋ ˆ์‹œํ”ผ ๊ฐœ์š” ๋ชฉ๋ก์„ ์กฐํšŒํ•œ๋‹ค.")
Expand All @@ -45,4 +38,43 @@ void readRecipeSteps() {
.then().log().all()
.body("size()", is(3));
}

@Test
@DisplayName("์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ๋ ˆ์‹œํ”ผ ๊ฐœ์š” ๋ชฉ๋ก์„ ์กฐํšŒํ•œ๋‹ค.")
void readRecipesOfCategory() {
RestAssured.given(spec).log().all()
.filter(document(DEFAULT_RESTDOCS_PATH,
queryParameters(
parameterWithName("category").description("์นดํ…Œ๊ณ ๋ฆฌ"),
parameterWithName("pageNumber").description("ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ"),
parameterWithName("pageSize").description("ํŽ˜์ด์ง€ ํฌ๊ธฐ")
),
responseFields(
fieldWithPath("[]").description("๋ ˆ์‹œํ”ผ ๋ชฉ๋ก"),
fieldWithPath("[].recipeId").description("๋ ˆ์‹œํ”ผ ์•„์ด๋””"),
fieldWithPath("[].title").description("๋ ˆ์‹œํ”ผ ์ œ๋ชฉ"),
fieldWithPath("[].author").description("์ž‘์„ฑ์ž ์ •๋ณด"),
fieldWithPath("[].author.authorId").description("์ž‘์„ฑ์ž ์•„์ด๋””"),
fieldWithPath("[].author.authorName").description("์ž‘์„ฑ์ž ์ด๋ฆ„"),
fieldWithPath("[].author.authorImage").description("์ž‘์„ฑ์ž ์ด๋ฏธ์ง€"),
fieldWithPath("[].cookingTime").description("์กฐ๋ฆฌ ์‹œ๊ฐ„"),
fieldWithPath("[].thumbnail").description("์ธ๋„ค์ผ ์ด๋ฏธ์ง€"),
fieldWithPath("[].difficulty").description("๋‚œ์ด๋„"),
fieldWithPath("[].likeCount").description("์ข‹์•„์š” ์ˆ˜"),
fieldWithPath("[].description").description("๋ ˆ์‹œํ”ผ ์„ค๋ช…"),
fieldWithPath("[].category").description("์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก"),
fieldWithPath("[].category[].categoryId").description("์นดํ…Œ๊ณ ๋ฆฌ ์•„์ด๋””"),
fieldWithPath("[].category[].categoryName").description("์นดํ…Œ๊ณ ๋ฆฌ ์ด๋ฆ„"),
fieldWithPath("[].ingredient").description("์žฌ๋ฃŒ ๋ชฉ๋ก"),
fieldWithPath("[].ingredient[].ingredientId").description("์žฌ๋ฃŒ ์•„์ด๋””"),
fieldWithPath("[].ingredient[].ingredientName").description("์žฌ๋ฃŒ ์ด๋ฆ„"),
fieldWithPath("[].ingredient[].requirement").description("์žฌ๋ฃŒ ํ•„์ˆ˜ ์—ฌ๋ถ€")
)))
.queryParam("category", "ํ•œ์‹")
.queryParam("pageNumber", 0)
.queryParam("pageSize", 3)
.when().get("/api/recipes/search")
.then().log().all()
.body("size()", is(3));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ void findRecipeIds() {

List<Long> recipeIds = repository.findRecipeIds(pageable);

assertThat(recipeIds).containsExactly(4L, 3L, 2L);
assertThat(recipeIds).containsExactly(15L, 14L, 13L);
}

@Test
@DisplayName("๋ ˆ์‹œํ”ผ id์— ํ•ด๋‹น๋˜๋Š” ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.")
void findRecipeData() {
List<Long> recipeIds = List.of(4L, 3L);
RecipeDataResponse expectedData = new RecipeDataResponse(4, "ํฐ์Œ€๋ฐฅ", 1, "loki", "loki.jpg", LocalTime.of(0, 40),
"ํฐ์Œ€๋ฐฅ์ด๋ฏธ์ง€.jpg", 2, 4, "ํฐ์Œ€๋ฐฅ ์กฐ๋ฆฌ๋ฒ•", 3, "์ฑ„์‹", 2, "์Œ€", REQUIRED
RecipeDataResponse expectedData = new RecipeDataResponse(4, "ํ† ๋งˆํ† ์ŠคํŒŒ๊ฒŒํ‹ฐ", 1, "loki", "loki.jpg", LocalTime.of(0, 30),
"ํ† ๋งˆํ† ์ŠคํŒŒ๊ฒŒํ‹ฐ์ด๋ฏธ์ง€.jpg", 3, 2, "ํ† ๋งˆํ† ์ŠคํŒŒ๊ฒŒํ‹ฐ ์กฐ๋ฆฌ๋ฒ•", 2, "์–‘์‹", 2, "์Œ€", REQUIRED
);

List<RecipeDataResponse> recipeData = repository.findRecipeData(recipeIds);

assertAll(
() -> assertThat(recipeData).hasSize(8),
() -> assertThat(recipeData).hasSize(6 + 3),
() -> assertThat(recipeData).contains(expectedData)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package net.pengcook.recipe.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import net.pengcook.category.dto.RecipeOfCategoryRequest;
import net.pengcook.recipe.dto.MainRecipeResponse;
import net.pengcook.recipe.dto.RecipeStepResponse;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
Expand All @@ -24,7 +29,7 @@ class RecipeServiceTest {
private RecipeService recipeService;

@ParameterizedTest
@CsvSource(value = {"0,2,4", "1,2,2", "1,3,1"})
@CsvSource(value = {"0,2,15", "1,2,13", "1,3,12"})
@DisplayName("์š”์ฒญ๋ฐ›์€ ํŽ˜์ด์ง€์˜ ๋ ˆ์‹œํ”ผ ๊ฐœ์š” ๋ชฉ๋ก์„ ์กฐํšŒํ•œ๋‹ค.")
void readRecipes(int pageNumber, int pageSize, int expectedFirstRecipeId) {
List<MainRecipeResponse> mainRecipeResponses = recipeService.readRecipes(pageNumber, pageSize);
Expand All @@ -46,4 +51,27 @@ void readRecipeSteps() {

assertThat(recipeStepResponses).isEqualTo(expectedRecipeStepResponses);
}

@ParameterizedTest
@MethodSource("provideParameters")
@DisplayName("ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋ ˆ์‹œํ”ผ๋ฅผ ์ฐพ๋Š”๋‹ค.")
void readRecipesOfCategory(int pageNumber, int pageSize, List<Long> expected) {
RecipeOfCategoryRequest request = new RecipeOfCategoryRequest("ํ•œ์‹", pageNumber, pageSize);

List<MainRecipeResponse> mainRecipeResponses = recipeService.readRecipesOfCategory(request);
List<Long> actual = mainRecipeResponses.stream().map(MainRecipeResponse::recipeId).toList();

assertAll(
() -> assertThat(actual.size()).isEqualTo(pageSize),
() -> assertThat(actual).containsAll(expected)
);
}

static Stream<Arguments> provideParameters() {
return Stream.of(
Arguments.of(0, 2, List.of(2L, 3L)),
Arguments.of(1, 2, List.of(7L, 9L)),
Arguments.of(1, 3, List.of(9L, 14L, 15L))
);
}
}
Loading

0 comments on commit 808e701

Please sign in to comment.