Added ability to get Recipe.rawText from api.
This commit is contained in:
parent
84596865dd
commit
9b82e549ca
@ -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("<h1>Hello, World!</h1>"))
|
||||
.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);
|
||||
|
@ -46,11 +46,17 @@ public class RecipeController {
|
||||
public ResponseEntity<Map<String, Object>> 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<String, Object> 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);
|
||||
|
@ -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<RecipeInfoView> getInfoViewsViewableBy(Pageable pageable, @Nullable User viewer);
|
||||
List<Recipe> getByMinimumStars(long minimumStars, @Nullable User viewer);
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user