From 9b82e549cace9e93c989f2dff749520757dd0946 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Thu, 15 Aug 2024 14:20:38 -0500 Subject: [PATCH] Added ability to get Recipe.rawText from api. --- .../api/recipe/RecipeControllerTests.java | 17 +++++++++++++++++ .../api/recipe/RecipeController.java | 10 ++++++++-- .../mealsmadeeasy/api/recipe/RecipeService.java | 7 ++++++- .../api/recipe/RecipeServiceImpl.java | 14 ++++++++++---- .../api/recipe/view/FullRecipeView.java | 16 ++++++++++++++++ 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java b/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java index 90877dd..ffc6a3f 100644 --- a/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java +++ b/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java @@ -88,6 +88,7 @@ public class RecipeControllerTests { .andExpect(jsonPath("$.recipe.cookingTime").value(recipe.getCookingTime())) .andExpect(jsonPath("$.recipe.totalTime").value(recipe.getTotalTime())) .andExpect(jsonPath("$.recipe.text").value("

Hello, World!

")) + .andExpect(jsonPath("$.recipe.rawText").doesNotExist()) .andExpect(jsonPath("$.recipe.owner.id").value(owner.getId())) .andExpect(jsonPath("$.recipe.owner.username").value(owner.getUsername())) .andExpect(jsonPath("$.recipe.starCount").value(0)) @@ -95,7 +96,22 @@ public class RecipeControllerTests { .andExpect(jsonPath("$.recipe.isPublic").value(true)) .andExpect(jsonPath("$.isStarred").value(nullValue())) .andExpect(jsonPath("$.isOwner").value(nullValue())); + } + @Test + @DirtiesContext + public void getFullRecipeViewIncludeRawText() throws Exception { + final User owner = this.createTestUser("owner"); + final Recipe recipe = this.createTestRecipe(owner, true); + this.mockMvc.perform( + get( + "/recipes/{username}/{slug}?includeRawText=true", + recipe.getOwner().getUsername(), + recipe.getSlug() + ) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.recipe.rawText").value(recipe.getRawText())); } @Test @@ -114,6 +130,7 @@ public class RecipeControllerTests { } @Test + @DirtiesContext public void getFullRecipeViewPrincipalIsNotStarer() throws Exception { final User owner = this.createTestUser("owner"); final Recipe recipe = this.createTestRecipe(owner, false); diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java index 32f935b..c011b9c 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java @@ -46,11 +46,17 @@ public class RecipeController { public ResponseEntity> getByUsernameAndSlug( @PathVariable String username, @PathVariable String slug, + @RequestParam(defaultValue = "false") boolean includeRawText, @AuthenticationPrincipal User viewer ) throws RecipeException { - final FullRecipeView recipe = this.recipeService.getFullViewByUsernameAndSlug(username, slug, viewer); + final FullRecipeView view = this.recipeService.getFullViewByUsernameAndSlug( + username, + slug, + includeRawText, + viewer + ); final Map body = new HashMap<>(); - body.put("recipe", recipe); + body.put("recipe", view); body.put("isStarred", this.recipeService.isStarer(username, slug, viewer)); body.put("isOwner", this.recipeService.isOwner(username, slug, viewer)); return ResponseEntity.ok(body); diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeService.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeService.java index a481d04..e7724b0 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeService.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeService.java @@ -21,7 +21,12 @@ public interface RecipeService { Recipe getByUsernameAndSlug(String username, String slug, @Nullable User viewer) throws RecipeException; FullRecipeView getFullViewById(long id, @Nullable User viewer) throws RecipeException; - FullRecipeView getFullViewByUsernameAndSlug(String username, String slug, @Nullable User viewer) throws RecipeException; + FullRecipeView getFullViewByUsernameAndSlug( + String username, + String slug, + boolean includeRawText, + @Nullable User viewer + ) throws RecipeException; Slice getInfoViewsViewableBy(Pageable pageable, @Nullable User viewer); List getByMinimumStars(long minimumStars, @Nullable User viewer); diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeServiceImpl.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeServiceImpl.java index b402952..6a7ce73 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeServiceImpl.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeServiceImpl.java @@ -125,10 +125,11 @@ public class RecipeServiceImpl implements RecipeService { } } - private FullRecipeView getFullView(RecipeEntity recipe, @Nullable User viewer) { + private FullRecipeView getFullView(RecipeEntity recipe, boolean includeRawText, @Nullable User viewer) { return FullRecipeView.from( recipe, this.getRenderedMarkdown(recipe), + includeRawText, this.getStarCount(recipe), this.getViewerCount(recipe.getId()), this.getImageView(recipe.getMainImage(), viewer) @@ -149,18 +150,23 @@ public class RecipeServiceImpl implements RecipeService { final RecipeEntity recipe = this.recipeRepository.findById(id).orElseThrow(() -> new RecipeException( RecipeException.Type.INVALID_ID, "No such Recipe for id: " + id )); - return this.getFullView(recipe, viewer); + return this.getFullView(recipe, false, viewer); } @Override @PreAuthorize("@recipeSecurity.isViewableBy(#username, #slug, #viewer)") - public FullRecipeView getFullViewByUsernameAndSlug(String username, String slug, @Nullable User viewer) throws RecipeException { + public FullRecipeView getFullViewByUsernameAndSlug( + String username, + String slug, + boolean includeRawText, + @Nullable User viewer + ) throws RecipeException { final RecipeEntity recipe = this.recipeRepository.findByOwnerUsernameAndSlug(username, slug) .orElseThrow(() -> new RecipeException( RecipeException.Type.INVALID_USERNAME_OR_SLUG, "No such Recipe for username " + username + " and slug: " + slug )); - return this.getFullView(recipe, viewer); + return this.getFullView(recipe, includeRawText, viewer); } @Override diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/view/FullRecipeView.java b/src/main/java/app/mealsmadeeasy/api/recipe/view/FullRecipeView.java index c64278f..0270681 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/view/FullRecipeView.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/view/FullRecipeView.java @@ -3,6 +3,8 @@ package app.mealsmadeeasy.api.recipe.view; import app.mealsmadeeasy.api.image.view.ImageView; import app.mealsmadeeasy.api.recipe.Recipe; import app.mealsmadeeasy.api.user.view.UserInfoView; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; import org.jetbrains.annotations.Nullable; import java.time.LocalDateTime; @@ -12,6 +14,7 @@ public class FullRecipeView { public static FullRecipeView from( Recipe recipe, String renderedText, + boolean includeRawText, int starCount, int viewerCount, ImageView mainImage @@ -26,6 +29,9 @@ public class FullRecipeView { view.setCookingTime(recipe.getCookingTime()); view.setTotalTime(recipe.getTotalTime()); view.setText(renderedText); + if (includeRawText) { + view.setRawText(recipe.getRawText()); + } view.setOwner(UserInfoView.from(recipe.getOwner())); view.setStarCount(starCount); view.setViewerCount(viewerCount); @@ -43,6 +49,7 @@ public class FullRecipeView { private @Nullable Integer cookingTime; private @Nullable Integer totalTime; private String text; + private @Nullable String rawText; private UserInfoView owner; private int starCount; private int viewerCount; @@ -121,6 +128,15 @@ public class FullRecipeView { this.text = text; } + @JsonInclude(Include.NON_NULL) + public @Nullable String getRawText() { + return this.rawText; + } + + public void setRawText(@Nullable String rawText) { + this.rawText = rawText; + } + public UserInfoView getOwner() { return this.owner; }