feat: rating stars on recipe cards (Search, Explore)
New lib/recipe-ratings.ts helper batches avg score + review count for a set of recipe ids in one grouped query, kept separate from the main paginated/sorted search query rather than joining+grouping it directly (would've forced grouping by every selected column). Wired into /api/v1/search and the Explore page's trending/recent queries; SearchResultCard renders the star only when a recipe has at least one rating. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
sql,
|
||||
desc,
|
||||
} from "@epicure/db";
|
||||
import { getAvgRatingsByRecipeId } from "@/lib/recipe-ratings";
|
||||
|
||||
const VALID_DIETARY = ["vegan", "vegetarian", "glutenFree", "dairyFree"] as const;
|
||||
type DietaryTag = (typeof VALID_DIETARY)[number];
|
||||
@@ -143,5 +144,12 @@ export async function GET(req: NextRequest) {
|
||||
|
||||
const total = countResult[0]?.total ?? 0;
|
||||
|
||||
return NextResponse.json({ data: rows, total, limit, offset });
|
||||
const ratingByRecipe = await getAvgRatingsByRecipeId(rows.map((r) => r.id));
|
||||
const data = rows.map((r) => ({
|
||||
...r,
|
||||
avgRating: ratingByRecipe.get(r.id)?.avgRating ?? null,
|
||||
ratingCount: ratingByRecipe.get(r.id)?.ratingCount ?? 0,
|
||||
}));
|
||||
|
||||
return NextResponse.json({ data, total, limit, offset });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user