feat: api v1 garden bangs routes and openapi
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { deleteBangForScope, updateBangForScope } from "@/modules/bangs/server/actions";
|
||||
import { updateBangInput } from "@/modules/bangs/server/schemas";
|
||||
import { z } from "zod";
|
||||
|
||||
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 = updateBangInput.parse(body);
|
||||
const bang = await updateBangForScope(scope, { id, ...parsed });
|
||||
return apiJson({
|
||||
id: bang.id,
|
||||
occurredOn: bang.occurredOn,
|
||||
recordedBy: bang.recordedBy,
|
||||
createdAt: bang.createdAt.toISOString(),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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 deleteBangForScope(scope, { id });
|
||||
return apiJson({ ok: true });
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { addBangForScope } from "@/modules/bangs/server/actions";
|
||||
import { addBangInput } from "@/modules/bangs/server/schemas";
|
||||
import { getBangStatsForScope } from "@/modules/bangs/server/queries";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
const url = new URL(req.url);
|
||||
const limitParam = url.searchParams.get("limit");
|
||||
const limit = limitParam ? Math.min(Math.max(Number(limitParam) || 10, 1), 100) : 10;
|
||||
const stats = await getBangStatsForScope(scope.householdId, limit);
|
||||
return apiJson(stats);
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
const body: unknown = await req.json().catch(() => ({}));
|
||||
const parsed = addBangInput.parse(body);
|
||||
const bang = await addBangForScope(scope, parsed);
|
||||
return apiJson(
|
||||
{
|
||||
id: bang.id,
|
||||
occurredOn: bang.occurredOn,
|
||||
recordedBy: bang.recordedBy,
|
||||
createdAt: bang.createdAt.toISOString(),
|
||||
},
|
||||
201,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
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 });
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { createContainerForScope } from "@/modules/garden/server/actions";
|
||||
import { containerInput } from "@/modules/garden/server/schemas";
|
||||
import { getContainerForScope, listContainersForScope } from "@/modules/garden/server/queries";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
return withApiHandler(request, async (scope) => {
|
||||
const containers = await listContainersForScope(scope.householdId);
|
||||
return apiJson(containers);
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
const body: unknown = await req.json();
|
||||
const parsed = containerInput.parse(body);
|
||||
const container = await createContainerForScope(scope, parsed);
|
||||
const detail = await getContainerForScope(scope.householdId, container.id);
|
||||
return apiJson(detail, 201);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { deletePlantForScope, updatePlantForScope } from "@/modules/garden/server/actions";
|
||||
import { plantUpdateInput } from "@/modules/garden/server/schemas";
|
||||
import { getPlantForScope } 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 plant = await getPlantForScope(scope.householdId, id);
|
||||
if (!plant) throw new Error("Plant not found");
|
||||
return apiJson(plant);
|
||||
});
|
||||
}
|
||||
|
||||
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 = plantUpdateInput.parse(body);
|
||||
await updatePlantForScope(scope, { id, ...parsed });
|
||||
const plant = await getPlantForScope(scope.householdId, id);
|
||||
if (!plant) throw new Error("Plant not found");
|
||||
return apiJson(plant);
|
||||
});
|
||||
}
|
||||
|
||||
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 deletePlantForScope(scope, { id });
|
||||
return apiJson({ ok: true });
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { createPlantForScope } from "@/modules/garden/server/actions";
|
||||
import { plantInput } from "@/modules/garden/server/schemas";
|
||||
import { getPlantForScope, listPlantsForScope } from "@/modules/garden/server/queries";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
const url = new URL(req.url);
|
||||
const containerId = url.searchParams.get("containerId") ?? undefined;
|
||||
const plants = await listPlantsForScope(scope.householdId, { containerId });
|
||||
return apiJson(plants);
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
const body: unknown = await req.json();
|
||||
const parsed = plantInput.parse(body);
|
||||
const plant = await createPlantForScope(scope, parsed);
|
||||
const detail = await getPlantForScope(scope.householdId, plant.id);
|
||||
return apiJson(detail, 201);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user