package app.mealsmadeeasy.api.recipe; import app.mealsmadeeasy.api.image.ImageException; import app.mealsmadeeasy.api.recipe.body.RecipeAiSearchBody; import app.mealsmadeeasy.api.recipe.body.RecipeSearchBody; import app.mealsmadeeasy.api.recipe.body.RecipeUpdateBody; import app.mealsmadeeasy.api.recipe.comment.RecipeComment; import app.mealsmadeeasy.api.recipe.comment.RecipeCommentCreateBody; import app.mealsmadeeasy.api.recipe.comment.RecipeCommentService; import app.mealsmadeeasy.api.recipe.comment.RecipeCommentView; import app.mealsmadeeasy.api.recipe.spec.RecipeUpdateSpec; import app.mealsmadeeasy.api.recipe.star.RecipeStar; import app.mealsmadeeasy.api.recipe.star.RecipeStarService; import app.mealsmadeeasy.api.recipe.view.FullRecipeView; import app.mealsmadeeasy.api.recipe.view.RecipeExceptionView; import app.mealsmadeeasy.api.recipe.view.RecipeInfoView; import app.mealsmadeeasy.api.sliceview.SliceViewService; import app.mealsmadeeasy.api.user.User; import com.fasterxml.jackson.databind.ObjectMapper; import org.jetbrains.annotations.Nullable; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/recipes") public class RecipeController { private final RecipeService recipeService; private final RecipeStarService recipeStarService; private final RecipeCommentService recipeCommentService; private final SliceViewService sliceViewService; private final ObjectMapper objectMapper; public RecipeController( RecipeService recipeService, RecipeStarService recipeStarService, RecipeCommentService recipeCommentService, SliceViewService sliceViewService, ObjectMapper objectMapper ) { this.recipeService = recipeService; this.recipeStarService = recipeStarService; this.recipeCommentService = recipeCommentService; this.sliceViewService = sliceViewService; this.objectMapper = objectMapper; } @ExceptionHandler(RecipeException.class) public ResponseEntity onRecipeException(RecipeException recipeException) { final HttpStatus status = switch (recipeException.getType()) { case INVALID_ID, INVALID_USERNAME_OR_SLUG -> HttpStatus.NOT_FOUND; case INVALID_COMMENT_ID -> HttpStatus.BAD_REQUEST; }; return ResponseEntity.status(status.value()).body(new RecipeExceptionView( recipeException.getType().toString(), recipeException.getMessage() )); } private Map getFullViewWrapper(String username, String slug, FullRecipeView view, @Nullable User viewer) { Map wrapper = new HashMap<>(); wrapper.put("recipe", view); wrapper.put("isStarred", this.recipeService.isStarer(username, slug, viewer)); wrapper.put("isOwner", this.recipeService.isOwner(username, slug, viewer)); return wrapper; } @GetMapping("/{username}/{slug}") public ResponseEntity> getByUsernameAndSlug( @PathVariable String username, @PathVariable String slug, @RequestParam(defaultValue = "false") boolean includeRawText, @AuthenticationPrincipal User viewer ) throws RecipeException { final FullRecipeView view = this.recipeService.getFullViewByUsernameAndSlug( username, slug, includeRawText, viewer ); return ResponseEntity.ok(this.getFullViewWrapper(username, slug, view, viewer)); } @PostMapping("/{username}/{slug}") public ResponseEntity> updateByUsernameAndSlug( @PathVariable String username, @PathVariable String slug, @RequestParam(defaultValue = "true") boolean includeRawText, @RequestBody RecipeUpdateBody updateBody, @AuthenticationPrincipal User principal ) throws ImageException, RecipeException { final RecipeUpdateSpec spec = RecipeUpdateSpec.from(updateBody); final Recipe updated = this.recipeService.update(username, slug, spec, principal); final FullRecipeView view = this.recipeService.toFullRecipeView(updated, includeRawText, principal); return ResponseEntity.ok(this.getFullViewWrapper(username, slug, view, principal)); } @GetMapping public ResponseEntity> getRecipeInfoViews( Pageable pageable, @AuthenticationPrincipal User user ) { final Slice slice = this.recipeService.getInfoViewsViewableBy(pageable, user); return ResponseEntity.ok(this.sliceViewService.getSliceView(slice)); } @PostMapping public ResponseEntity> searchRecipes( @RequestBody(required = false) RecipeSearchBody recipeSearchBody, @AuthenticationPrincipal User user ) { if (recipeSearchBody.getType() == RecipeSearchBody.Type.AI_PROMPT) { final RecipeAiSearchBody spec = this.objectMapper.convertValue( recipeSearchBody.getData(), RecipeAiSearchBody.class ); final List results = this.recipeService.aiSearch(spec, user); return ResponseEntity.ok(Map.of("results", results)); } else { throw new IllegalArgumentException("Invalid recipeSearchBody type: " + recipeSearchBody.getType()); } } @PostMapping("/{username}/{slug}/star") public ResponseEntity addStar( @PathVariable String username, @PathVariable String slug, @Nullable @AuthenticationPrincipal User principal ) throws RecipeException { if (principal == null) { throw new AccessDeniedException("Must be logged in to star a recipe."); } return ResponseEntity.status(HttpStatus.CREATED).body(this.recipeStarService.create(username, slug, principal)); } @GetMapping("/{username}/{slug}/star") public ResponseEntity> getStar( @PathVariable String username, @PathVariable String slug, @Nullable @AuthenticationPrincipal User principal ) throws RecipeException { if (principal == null) { throw new AccessDeniedException("Must be logged in to get a recipe star."); } final @Nullable RecipeStar star = this.recipeStarService.find(username, slug, principal).orElse(null); if (star != null) { return ResponseEntity.ok(Map.of("isStarred", true, "star", star)); } else { return ResponseEntity.ok(Map.of("isStarred", false)); } } @DeleteMapping("/{username}/{slug}/star") public ResponseEntity removeStar( @PathVariable String username, @PathVariable String slug, @Nullable @AuthenticationPrincipal User principal ) throws RecipeException { if (principal == null) { throw new AccessDeniedException("Must be logged in to delete a recipe star."); } this.recipeStarService.delete(username, slug, principal); return ResponseEntity.noContent().build(); } @GetMapping("/{username}/{slug}/comments") public ResponseEntity> getComments( @PathVariable String username, @PathVariable String slug, Pageable pageable, @Nullable @AuthenticationPrincipal User principal ) throws RecipeException { final Slice slice = this.recipeCommentService.getComments( username, slug, pageable, principal ); return ResponseEntity.ok(this.sliceViewService.getSliceView(slice)); } @PostMapping("/{username}/{slug}/comments") public ResponseEntity addComment( @PathVariable String username, @PathVariable String slug, @RequestBody RecipeCommentCreateBody body, @Nullable @AuthenticationPrincipal User principal ) throws RecipeException { if (principal == null) { throw new AccessDeniedException("Must be logged in to comment on a recipe."); } final RecipeComment comment = this.recipeCommentService.create(username, slug, principal, body); return ResponseEntity.ok(RecipeCommentView.from(comment, false)); } }