MME-36 Add recipes/meta/count endpoint and fix counting bug.

This commit is contained in:
Jesse Brault 2026-02-16 18:00:47 -06:00
parent ca926d9ada
commit 0d80c92850
2 changed files with 12 additions and 1 deletions

View File

@ -71,7 +71,7 @@ public interface RecipeRepository extends JpaRepository<Recipe, Integer> {
int countByIsPublicIsTrue(); 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); int countViewableBy(User viewer);
void deleteRecipeByOwnerUsernameAndSlug(String username, String slug); void deleteRecipeByOwnerUsernameAndSlug(String username, String slug);

View File

@ -100,6 +100,17 @@ public class RecipesController {
} }
} }
@GetMapping("/meta/count")
public ResponseEntity<Map<String, Object>> 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 @PostMapping
public ResponseEntity<Map<String, Object>> searchRecipes( public ResponseEntity<Map<String, Object>> searchRecipes(
@RequestBody(required = false) RecipeSearchBody recipeSearchBody, @RequestBody(required = false) RecipeSearchBody recipeSearchBody,