3636ab27ae
AI-generated weekly meal plans with pantry-awareness. Manual entry per slot. Pantry inventory management. Auto-generated shopping lists from meal plan. Weekly nutrition bar chart vs daily goals. Nutrition goals settings.
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { db, pantryItems, eq, desc } from "@epicure/db";
|
|
import { requireSession } from "@/lib/api-auth";
|
|
|
|
const Schema = z.object({
|
|
rawName: z.string().min(1).max(200),
|
|
quantity: z.string().optional(),
|
|
unit: z.string().optional(),
|
|
expiresAt: z.string().datetime().optional(),
|
|
});
|
|
|
|
export async function GET(_req: NextRequest) {
|
|
const { session, response } = await requireSession();
|
|
if (response) return response;
|
|
|
|
const items = await db.query.pantryItems.findMany({
|
|
where: eq(pantryItems.userId, session!.user.id),
|
|
orderBy: desc(pantryItems.createdAt),
|
|
});
|
|
|
|
return NextResponse.json(items);
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const { session, response } = await requireSession();
|
|
if (response) return response;
|
|
|
|
const body = await req.json() as unknown;
|
|
const parsed = Schema.safeParse(body);
|
|
if (!parsed.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
|
|
|
|
const id = crypto.randomUUID();
|
|
await db.insert(pantryItems).values({
|
|
id,
|
|
userId: session!.user.id,
|
|
rawName: parsed.data.rawName,
|
|
quantity: parsed.data.quantity,
|
|
unit: parsed.data.unit,
|
|
expiresAt: parsed.data.expiresAt ? new Date(parsed.data.expiresAt) : undefined,
|
|
});
|
|
|
|
return NextResponse.json({ id }, { status: 201 });
|
|
}
|