Added ability to get Recipe.rawText from api.

This commit is contained in:
Jesse Brault 2024-08-15 14:20:38 -05:00
parent 84596865dd
commit 9b82e549ca
5 changed files with 57 additions and 7 deletions

View File

@ -88,6 +88,7 @@ public class RecipeControllerTests {
.andExpect(jsonPath("$.recipe.cookingTime").value(recipe.getCookingTime())) .andExpect(jsonPath("$.recipe.cookingTime").value(recipe.getCookingTime()))
.andExpect(jsonPath("$.recipe.totalTime").value(recipe.getTotalTime())) .andExpect(jsonPath("$.recipe.totalTime").value(recipe.getTotalTime()))
.andExpect(jsonPath("$.recipe.text").value("<h1>Hello, World!</h1>")) .andExpect(jsonPath("$.recipe.text").value("<h1>Hello, World!</h1>"))
.andExpect(jsonPath("$.recipe.rawText").doesNotExist())
.andExpect(jsonPath("$.recipe.owner.id").value(owner.getId())) .andExpect(jsonPath("$.recipe.owner.id").value(owner.getId()))
.andExpect(jsonPath("$.recipe.owner.username").value(owner.getUsername())) .andExpect(jsonPath("$.recipe.owner.username").value(owner.getUsername()))
.andExpect(jsonPath("$.recipe.starCount").value(0)) .andExpect(jsonPath("$.recipe.starCount").value(0))
@ -95,7 +96,22 @@ public class RecipeControllerTests {
.andExpect(jsonPath("$.recipe.isPublic").value(true)) .andExpect(jsonPath("$.recipe.isPublic").value(true))
.andExpect(jsonPath("$.isStarred").value(nullValue())) .andExpect(jsonPath("$.isStarred").value(nullValue()))
.andExpect(jsonPath("$.isOwner").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 @Test
@ -114,6 +130,7 @@ public class RecipeControllerTests {
} }
@Test @Test
@DirtiesContext
public void getFullRecipeViewPrincipalIsNotStarer() throws Exception { public void getFullRecipeViewPrincipalIsNotStarer() throws Exception {
final User owner = this.createTestUser("owner"); final User owner = this.createTestUser("owner");
final Recipe recipe = this.createTestRecipe(owner, false); final Recipe recipe = this.createTestRecipe(owner, false);

View File

@ -46,11 +46,17 @@ public class RecipeController {
public ResponseEntity<Map<String, Object>> getByUsernameAndSlug( public ResponseEntity<Map<String, Object>> getByUsernameAndSlug(
@PathVariable String username, @PathVariable String username,
@PathVariable String slug, @PathVariable String slug,
@RequestParam(defaultValue = "false") boolean includeRawText,
@AuthenticationPrincipal User viewer @AuthenticationPrincipal User viewer
) throws RecipeException { ) throws RecipeException {
final FullRecipeView recipe = this.recipeService.getFullViewByUsernameAndSlug(username, slug, viewer); final FullRecipeView view = this.recipeService.getFullViewByUsernameAndSlug(
username,
slug,
includeRawText,
viewer
);
final Map<String, Object> body = new HashMap<>(); final Map<String, Object> body = new HashMap<>();
body.put("recipe", recipe); body.put("recipe", view);
body.put("isStarred", this.recipeService.isStarer(username, slug, viewer)); body.put("isStarred", this.recipeService.isStarer(username, slug, viewer));
body.put("isOwner", this.recipeService.isOwner(username, slug, viewer)); body.put("isOwner", this.recipeService.isOwner(username, slug, viewer));
return ResponseEntity.ok(body); return ResponseEntity.ok(body);

View File

@ -21,7 +21,12 @@ public interface RecipeService {
Recipe getByUsernameAndSlug(String username, String slug, @Nullable User viewer) throws RecipeException; Recipe getByUsernameAndSlug(String username, String slug, @Nullable User viewer) throws RecipeException;
FullRecipeView getFullViewById(long id, @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<RecipeInfoView> getInfoViewsViewableBy(Pageable pageable, @Nullable User viewer); Slice<RecipeInfoView> getInfoViewsViewableBy(Pageable pageable, @Nullable User viewer);
List<Recipe> getByMinimumStars(long minimumStars, @Nullable User viewer); List<Recipe> getByMinimumStars(long minimumStars, @Nullable User viewer);

View File

@ -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( return FullRecipeView.from(
recipe, recipe,
this.getRenderedMarkdown(recipe), this.getRenderedMarkdown(recipe),
includeRawText,
this.getStarCount(recipe), this.getStarCount(recipe),
this.getViewerCount(recipe.getId()), this.getViewerCount(recipe.getId()),
this.getImageView(recipe.getMainImage(), viewer) this.getImageView(recipe.getMainImage(), viewer)
@ -149,18 +150,23 @@ public class RecipeServiceImpl implements RecipeService {
final RecipeEntity recipe = this.recipeRepository.findById(id).orElseThrow(() -> new RecipeException( final RecipeEntity recipe = this.recipeRepository.findById(id).orElseThrow(() -> new RecipeException(
RecipeException.Type.INVALID_ID, "No such Recipe for id: " + id RecipeException.Type.INVALID_ID, "No such Recipe for id: " + id
)); ));
return this.getFullView(recipe, viewer); return this.getFullView(recipe, false, viewer);
} }
@Override @Override
@PreAuthorize("@recipeSecurity.isViewableBy(#username, #slug, #viewer)") @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) final RecipeEntity recipe = this.recipeRepository.findByOwnerUsernameAndSlug(username, slug)
.orElseThrow(() -> new RecipeException( .orElseThrow(() -> new RecipeException(
RecipeException.Type.INVALID_USERNAME_OR_SLUG, RecipeException.Type.INVALID_USERNAME_OR_SLUG,
"No such Recipe for username " + username + " and slug: " + slug "No such Recipe for username " + username + " and slug: " + slug
)); ));
return this.getFullView(recipe, viewer); return this.getFullView(recipe, includeRawText, viewer);
} }
@Override @Override

View File

@ -3,6 +3,8 @@ package app.mealsmadeeasy.api.recipe.view;
import app.mealsmadeeasy.api.image.view.ImageView; import app.mealsmadeeasy.api.image.view.ImageView;
import app.mealsmadeeasy.api.recipe.Recipe; import app.mealsmadeeasy.api.recipe.Recipe;
import app.mealsmadeeasy.api.user.view.UserInfoView; 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 org.jetbrains.annotations.Nullable;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -12,6 +14,7 @@ public class FullRecipeView {
public static FullRecipeView from( public static FullRecipeView from(
Recipe recipe, Recipe recipe,
String renderedText, String renderedText,
boolean includeRawText,
int starCount, int starCount,
int viewerCount, int viewerCount,
ImageView mainImage ImageView mainImage
@ -26,6 +29,9 @@ public class FullRecipeView {
view.setCookingTime(recipe.getCookingTime()); view.setCookingTime(recipe.getCookingTime());
view.setTotalTime(recipe.getTotalTime()); view.setTotalTime(recipe.getTotalTime());
view.setText(renderedText); view.setText(renderedText);
if (includeRawText) {
view.setRawText(recipe.getRawText());
}
view.setOwner(UserInfoView.from(recipe.getOwner())); view.setOwner(UserInfoView.from(recipe.getOwner()));
view.setStarCount(starCount); view.setStarCount(starCount);
view.setViewerCount(viewerCount); view.setViewerCount(viewerCount);
@ -43,6 +49,7 @@ public class FullRecipeView {
private @Nullable Integer cookingTime; private @Nullable Integer cookingTime;
private @Nullable Integer totalTime; private @Nullable Integer totalTime;
private String text; private String text;
private @Nullable String rawText;
private UserInfoView owner; private UserInfoView owner;
private int starCount; private int starCount;
private int viewerCount; private int viewerCount;
@ -121,6 +128,15 @@ public class FullRecipeView {
this.text = text; 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() { public UserInfoView getOwner() {
return this.owner; return this.owner;
} }