37 lines
1.5 KiB
TypeScript
37 lines
1.5 KiB
TypeScript
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
|
import { deleteContainerForScope, updateContainerForScope } from "@/modules/garden/server/actions";
|
|
import { containerUpdateInput } from "@/modules/garden/server/schemas";
|
|
import { getContainerForScope } from "@/modules/garden/server/queries";
|
|
import { z } from "zod";
|
|
|
|
export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
const { id } = await params;
|
|
return withApiHandler(request, async (scope) => {
|
|
z.string().uuid().parse(id);
|
|
const container = await getContainerForScope(scope.householdId, id);
|
|
if (!container) throw new Error("Container not found");
|
|
return apiJson(container);
|
|
});
|
|
}
|
|
|
|
export async function PATCH(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
const { id } = await params;
|
|
return withApiHandler(request, async (scope, req) => {
|
|
const body: unknown = await req.json();
|
|
const parsed = containerUpdateInput.parse(body);
|
|
await updateContainerForScope(scope, { id, ...parsed });
|
|
const container = await getContainerForScope(scope.householdId, id);
|
|
if (!container) throw new Error("Container not found");
|
|
return apiJson(container);
|
|
});
|
|
}
|
|
|
|
export async function DELETE(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
const { id } = await params;
|
|
return withApiHandler(request, async (scope) => {
|
|
z.string().uuid().parse(id);
|
|
await deleteContainerForScope(scope, { id });
|
|
return apiJson({ ok: true });
|
|
});
|
|
}
|