diff --git a/backend/src/routes/recipes.ts b/backend/src/routes/recipes.ts index ee19416..76a5fb9 100644 --- a/backend/src/routes/recipes.ts +++ b/backend/src/routes/recipes.ts @@ -4,7 +4,9 @@ import * as svc from '../services/recipe.service.js'; export async function recipeRoutes(app: FastifyInstance) { app.get('/api/recipes/random', async (request, reply) => { - const recipe = await svc.getRandomRecipe(); + const { categories } = request.query as { categories?: string }; + const categorySlugs = categories ? categories.split(',') : undefined; + const recipe = await svc.getRandomRecipe(categorySlugs); if (!recipe) return reply.status(404).send({ error: 'No recipes found' }); return recipe; }); diff --git a/backend/src/services/recipe.service.ts b/backend/src/services/recipe.service.ts index d1488ea..f49ebda 100644 --- a/backend/src/services/recipe.service.ts +++ b/backend/src/services/recipe.service.ts @@ -261,8 +261,18 @@ export async function toggleFavorite(id: string, userId?: string) { } } -export async function getRandomRecipe() { - const { rows } = await query('SELECT slug FROM recipes ORDER BY RANDOM() LIMIT 1'); +export async function getRandomRecipe(categorySlugs?: string[]) { + let sql = 'SELECT r.slug FROM recipes r'; + const params: string[] = []; + + if (categorySlugs && categorySlugs.length > 0) { + sql += ' JOIN categories c ON r.category_id = c.id WHERE c.slug = ANY($1)'; + params.push(categorySlugs as any); + } + + sql += ' ORDER BY RANDOM() LIMIT 1'; + + const { rows } = await query(sql, params); if (rows.length === 0) return null; return getRecipeBySlug(rows[0].slug); } diff --git a/frontend/src/api/recipes.ts b/frontend/src/api/recipes.ts index 15267db..ca1a7a2 100644 --- a/frontend/src/api/recipes.ts +++ b/frontend/src/api/recipes.ts @@ -24,8 +24,9 @@ export function fetchRecipe(slug: string) { return apiFetch(`/recipes/${slug}`) } -export function fetchRandomRecipe() { - return apiFetch('/recipes/random') +export function fetchRandomRecipe(categories?: string[]) { + const qs = categories ? `?categories=${categories.join(',')}` : '' + return apiFetch(`/recipes/random${qs}`) } export function searchRecipes(q: string) { diff --git a/frontend/src/pages/HomePage.tsx b/frontend/src/pages/HomePage.tsx index 07e89b5..99f7e43 100644 --- a/frontend/src/pages/HomePage.tsx +++ b/frontend/src/pages/HomePage.tsx @@ -48,13 +48,18 @@ export function HomePage() { ? recipes.filter(r => r.tags?.includes(activeTag)) : recipes - const handleRandomRecipe = async () => { + const handleRandomCook = async () => { try { - const recipe = await fetchRandomRecipe() + const recipe = await fetchRandomRecipe(['mittag', 'abend', 'fruehstueck']) if (recipe?.slug) navigate(`/recipe/${recipe.slug}?from=random`) - } catch { - // no recipes - } + } catch { /* no recipes */ } + } + + const handleRandomBake = async () => { + try { + const recipe = await fetchRandomRecipe(['backen', 'torten', 'desserts']) + if (recipe?.slug) navigate(`/recipe/${recipe.slug}?from=random`) + } catch { /* no recipes */ } } const activeTags = (tags || []).filter(t => t.recipe_count > 0) @@ -80,15 +85,22 @@ export function HomePage() { )} - {/* Random Recipe Button */} + {/* Random Recipe Buttons */} {totalCount > 1 && ( -
+
+
)}