Livestock and household pets as leaves on the Location tree, mirroring Garden. - Animal model (species/breed/sex/birthdate/location, soft-delete) + AnimalLog (feeding, vet, weight, medication, breeding, acquired, died); Task.animalId for recurring care reminders - API-first /api/v1/animals (CRUD) + logs, animals:read/write scopes; animals service shared with the coming MCP - Animals pages: list grouped by species with auto-computed age, detail with facts + latest weight + care log; add/edit/remove + log entry. Nav entry. Backup/restore cover the new tables - Pure age.ts (newborn/days/months/years) with tests; verified end-to-end against a running server (create, vet + weigh-in logs, read-back) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
29 lines
975 B
TypeScript
29 lines
975 B
TypeScript
import { NextResponse } from "next/server";
|
|
import { authenticateRequest, handleApiError } from "@/lib/api/authenticate";
|
|
import { animalCreateInput } from "@/lib/api/schemas";
|
|
import { listAnimals, createAnimal } from "@/lib/services/animals";
|
|
|
|
// GET /api/v1/animals?q=&species=
|
|
export async function GET(req: Request) {
|
|
try {
|
|
const ctx = await authenticateRequest(req, "animals:read");
|
|
const sp = new URL(req.url).searchParams;
|
|
return NextResponse.json(
|
|
await listAnimals(ctx, { q: sp.get("q") ?? undefined, species: sp.get("species") ?? undefined }),
|
|
);
|
|
} catch (err) {
|
|
return handleApiError(err);
|
|
}
|
|
}
|
|
|
|
// POST /api/v1/animals
|
|
export async function POST(req: Request) {
|
|
try {
|
|
const ctx = await authenticateRequest(req, "animals:write");
|
|
const input = animalCreateInput.parse(await req.json());
|
|
return NextResponse.json(await createAnimal(ctx, input), { status: 201 });
|
|
} catch (err) {
|
|
return handleApiError(err);
|
|
}
|
|
}
|