diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeRepository.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeRepository.java index de5497f..d65ae67 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeRepository.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeRepository.java @@ -71,7 +71,7 @@ public interface RecipeRepository extends JpaRepository { int countByIsPublicIsTrue(); - @Query("SELECT count(r) FROM Recipe r WHERE ?1 MEMBER OF r.viewers OR r.isPublic IS TRUE") + @Query("SELECT count(r) FROM Recipe r WHERE ?1 = r.owner OR ?1 MEMBER OF r.viewers OR r.isPublic IS TRUE") int countViewableBy(User viewer); void deleteRecipeByOwnerUsernameAndSlug(String username, String slug); diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipesController.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipesController.java index 376ceb5..66678b2 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipesController.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipesController.java @@ -100,6 +100,17 @@ public class RecipesController { } } + @GetMapping("/meta/count") + public ResponseEntity> getRecipeCount(@AuthenticationPrincipal User user) { + final int count; + if (user == null) { + count = this.recipeService.countPublicRecipes(); + } else { + count = this.recipeService.countViewableBy(user); + } + return ResponseEntity.ok(Map.of("count", count)); + } + @PostMapping public ResponseEntity> searchRecipes( @RequestBody(required = false) RecipeSearchBody recipeSearchBody,