Skip to content

Commit

Permalink
โ™ป๏ธ ingredient, category change
Browse files Browse the repository at this point in the history
  • Loading branch information
HaiSeong committed Oct 23, 2024
1 parent 77302ca commit 9470538
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ public RecipeResponse createRecipe(@LoginUser UserInfo userInfo, @RequestBody @V
return recipeService.createRecipe(userInfo, recipeRequest);
}

@PutMapping
@PutMapping("/{recipeId}")
public void updateRecipe(
@LoginUser UserInfo userInfo,
@PathVariable Long recipeId,
@RequestBody @Valid RecipeUpdateRequest recipeUpdateRequest
) {
recipeService.updateRecipe(userInfo, recipeUpdateRequest);
recipeService.updateRecipe(userInfo, recipeId, recipeUpdateRequest);
}

@GetMapping("/{recipeId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import net.pengcook.ingredient.dto.IngredientCreateRequest;

public record RecipeUpdateRequest(
@NotNull Long id,
@NotBlank String title,
@NotBlank String cookingTime,
@NotBlank String thumbnail,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,9 @@ public RecipeResponse createRecipe(UserInfo userInfo, RecipeRequest recipeReques
}

@Transactional
public void updateRecipe(UserInfo userInfo, RecipeUpdateRequest recipeUpdateRequest) {
User author = userRepository.findById(userInfo.getId()).orElseThrow();
Recipe recipe = recipeRepository.findById(userInfo.getId()).orElseThrow();
validateRecipeAuthor(author, recipe);
public void updateRecipe(UserInfo userInfo, Long recipeId, RecipeUpdateRequest recipeUpdateRequest) {
Recipe recipe = recipeRepository.findById(recipeId).orElseThrow();
verifyRecipeOwner(userInfo, recipe);

Recipe updatedRecipe = recipe.updateRecipe(
recipeUpdateRequest.title(),
Expand All @@ -183,17 +182,16 @@ public void updateRecipe(UserInfo userInfo, RecipeUpdateRequest recipeUpdateRequ
recipeUpdateRequest.description()
);

ingredientRecipeService.deleteIngredientRecipe(recipe.getId());
ingredientService.register(recipeUpdateRequest.ingredients(), updatedRecipe);
categoryService.deleteCategoryRecipe(recipe);
categoryService.saveCategories(updatedRecipe, recipeUpdateRequest.categories());

recipeStepService.deleteRecipeStepsByRecipe(updatedRecipe.getId());
recipeStepRepository.flush();
recipeStepService.saveRecipeSteps(updatedRecipe.getId(), recipeUpdateRequest.recipeSteps());
}

private void validateRecipeAuthor(User author, Recipe recipe) {
if (recipe.getAuthor().getId() != author.getId()) {
throw new UnauthorizedException("๋ ˆ์‹œํ”ผ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
}
}

@Transactional(readOnly = true)
public RecipeDescriptionResponse readRecipeDescription(UserInfo userInfo, long recipeId) {
List<RecipeDataResponse> recipeDataResponses = recipeRepository.findRecipeData(recipeId);
Expand All @@ -211,7 +209,7 @@ public void deleteRecipe(UserInfo userInfo, long recipeId) {
Optional<Recipe> targetRecipe = recipeRepository.findById(recipeId);

targetRecipe.ifPresent(recipe -> {
verifyUserCanDeleteRecipe(userInfo, recipe);
verifyRecipeOwner(userInfo, recipe);
ingredientRecipeService.deleteIngredientRecipe(recipe.getId());
categoryService.deleteCategoryRecipe(recipe);
commentService.deleteCommentsByRecipe(recipe.getId());
Expand Down Expand Up @@ -261,9 +259,11 @@ private List<CategoryResponse> getCategoryResponses(List<RecipeDataResponse> gro
.collect(Collectors.toList());
}

private void verifyUserCanDeleteRecipe(UserInfo userInfo, Recipe recipe) {
if (recipe.getAuthor().getId() != userInfo.getId()) {
throw new UnauthorizedException("๋ ˆ์‹œํ”ผ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
private void verifyRecipeOwner(UserInfo userInfo, Recipe recipe) {
User author = recipe.getAuthor();
long authorId = author.getId();
if (!userInfo.isSameUser(authorId)) {
throw new UnauthorizedException("๋ ˆ์‹œํ”ผ์— ๋Œ€ํ•œ ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ void createRecipe() {
@WithLoginUser(email = "[email protected]")
@DisplayName("๋ ˆ์‹œํ”ผ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.")
void updateRecipe() {
long recipeId = 1L;
List<String> categories = List.of("Dessert", "NewCategory");
List<String> substitutions = List.of("Water", "Orange");
List<IngredientCreateRequest> ingredients = List.of(
Expand All @@ -319,7 +320,6 @@ void updateRecipe() {
new RecipeStepRequest(null, "์Šคํ…2 ์„ค๋ช…", 2, "00:20:00")
);
RecipeUpdateRequest recipeUpdateRequest = new RecipeUpdateRequest(
1L,
"๋ณ€๊ฒฝ๋œ ๋ ˆ์‹œํ”ผ ์ œ๋ชฉ",
"00:30:00",
"๋ณ€๊ฒฝ๋œ ์ธ๋„ค์ผ.jpg",
Expand Down Expand Up @@ -353,7 +353,7 @@ void updateRecipe() {
)))
.contentType(ContentType.JSON)
.body(recipeUpdateRequest)
.when().put("/recipes")
.when().put("/recipes/" + recipeId)
.then().log().all()
.statusCode(200);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ void createRecipe() {
}

@Test
@DisplayName("์ƒˆ๋กœ์šด ๋ ˆ์‹œํ”ผ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.")
@DisplayName("๋ ˆ์‹œํ”ผ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.")
void updateRecipe() {
UserInfo userInfo = new UserInfo(1L, "[email protected]");
Long recipeId = 1L;

List<String> categories = List.of("๋ณ€๊ฒฝ๋œ ์นดํ…Œ๊ณ ๋ฆฌ 1", "๋ณ€๊ฒฝ๋œ ์นดํ…Œ๊ณ ๋ฆฌ 2");
List<String> substitutions = List.of("๋ณ€๊ฒฝ๋œ ์žฌ๋ฃŒ 1", "๋ณ€๊ฒฝ๋œ ์žฌ๋ฃŒ 2");
Expand All @@ -124,7 +125,6 @@ void updateRecipe() {
new RecipeStepRequest(null, "๋ณ€๊ฒฝ๋œ ์Šคํ…2 ์„ค๋ช…", 2, "00:30:00")
);
RecipeUpdateRequest recipeUpdateRecipe = new RecipeUpdateRequest(
1L,
"๋ณ€๊ฒฝ๋œ ์ œ๋ชฉ",
"00:10:00",
"๋ณ€๊ฒฝ๋œ ๋ ˆ์‹œํ”ผ ์ธ๋„ค์ผ.jpg",
Expand All @@ -135,7 +135,7 @@ void updateRecipe() {
recipeStepRequests
);

recipeService.updateRecipe(userInfo, recipeUpdateRecipe);
recipeService.updateRecipe(userInfo, recipeId, recipeUpdateRecipe);

Recipe recipe = recipeRepository.findById(1L).orElseThrow();
assertAll(
Expand Down

0 comments on commit 9470538

Please sign in to comment.