feat: api v1 garden bangs routes and openapi
This commit is contained in:
@@ -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,
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user