Files
homepage/src/routes/api/fitness/custom-meals/[id]/+server.ts
Alexander Bocken c4420b73d2
All checks were successful
CI / update (push) Successful in 4m47s
feat: add nutrition/food logging to fitness section
Daily food log with calorie and macro tracking against configurable diet
goals (presets: WHO balanced, cut, bulk, keto, etc.). Includes USDA/BLS
food search with portion-based units, favorite ingredients, custom
reusable meals, per-food micronutrient detail pages, and recipe-to-log
integration via AddToFoodLogButton. Extends FitnessGoal with nutrition
targets and adds birth year to user profile for BMR calculation.
2026-04-04 14:34:47 +02:00

40 lines
1.3 KiB
TypeScript

import { json, error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { requireAuth } from '$lib/server/middleware/auth';
import { dbConnect } from '$utils/db';
import { CustomMeal } from '$models/CustomMeal';
export const PUT: RequestHandler = async ({ params, request, locals }) => {
const user = await requireAuth(locals);
await dbConnect();
const meal = await CustomMeal.findById(params.id);
if (!meal) throw error(404, 'Meal not found');
if (meal.createdBy !== user.nickname) throw error(403, 'Not authorized');
const body = await request.json();
if (body.name !== undefined) {
if (!body.name?.trim()) throw error(400, 'name cannot be empty');
meal.name = body.name.trim();
}
if (body.ingredients !== undefined) {
if (!Array.isArray(body.ingredients) || body.ingredients.length === 0) throw error(400, 'At least one ingredient is required');
meal.ingredients = body.ingredients;
}
await meal.save();
return json(meal.toObject());
};
export const DELETE: RequestHandler = async ({ params, locals }) => {
const user = await requireAuth(locals);
await dbConnect();
const deleted = await CustomMeal.findOneAndDelete({
_id: params.id,
createdBy: user.nickname,
});
if (!deleted) throw error(404, 'Meal not found');
return json({ ok: true });
};