feat: api v1 routes for calendar lists and notes
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { deleteItemForScope, updateItemForScope } from "@/modules/lists/server/actions";
|
||||
import { updateItemInput } from "@/modules/lists/server/schemas";
|
||||
import { z } from "zod";
|
||||
|
||||
export async function PATCH(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ id: string; itemId: string }> },
|
||||
) {
|
||||
const { itemId } = await params;
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
z.string().uuid().parse(itemId);
|
||||
const body: unknown = await req.json();
|
||||
const parsed = updateItemInput.omit({ id: true }).parse(body);
|
||||
const list = await updateItemForScope(scope, { id: itemId, ...parsed });
|
||||
const item = list.items.find((entry) => entry.id === itemId);
|
||||
if (!item) throw new Error("Item not found");
|
||||
return apiJson(item);
|
||||
});
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ id: string; itemId: string }> },
|
||||
) {
|
||||
const { itemId } = await params;
|
||||
return withApiHandler(request, async (scope) => {
|
||||
z.string().uuid().parse(itemId);
|
||||
await deleteItemForScope(scope, { id: itemId });
|
||||
return apiJson({ ok: true });
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { addItemForScope } from "@/modules/lists/server/actions";
|
||||
import { itemInput } from "@/modules/lists/server/schemas";
|
||||
import { listItemsForScope } from "@/modules/lists/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 items = await listItemsForScope(scope.householdId, id);
|
||||
return apiJson(items);
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await params;
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
z.string().uuid().parse(id);
|
||||
const body: unknown = await req.json();
|
||||
const parsed = itemInput.omit({ listId: true }).parse(body);
|
||||
const item = await addItemForScope(scope, { listId: id, ...parsed });
|
||||
return apiJson(item, 201);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { deleteListForScope, updateListForScope } from "@/modules/lists/server/actions";
|
||||
import { listUpdateInput } from "@/modules/lists/server/schemas";
|
||||
import { getListForScope } from "@/modules/lists/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) => {
|
||||
const list = await getListForScope(scope.householdId, id);
|
||||
return apiJson(list);
|
||||
});
|
||||
}
|
||||
|
||||
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 = listUpdateInput.parse(body);
|
||||
await updateListForScope(scope, { id, ...parsed });
|
||||
const list = await getListForScope(scope.householdId, id);
|
||||
return apiJson(list);
|
||||
});
|
||||
}
|
||||
|
||||
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 deleteListForScope(scope, { id });
|
||||
return apiJson({ ok: true });
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { createListForScope } from "@/modules/lists/server/actions";
|
||||
import { listInput } from "@/modules/lists/server/schemas";
|
||||
import { listListsForScope } from "@/modules/lists/server/queries";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
const url = new URL(req.url);
|
||||
const type = url.searchParams.get("type") ?? undefined;
|
||||
const lists = await listListsForScope(scope.householdId, type ? { type } : undefined);
|
||||
return apiJson(lists);
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
const body: unknown = await req.json();
|
||||
const parsed = listInput.parse(body);
|
||||
const list = await createListForScope(scope, parsed);
|
||||
return apiJson(
|
||||
{
|
||||
id: list.id,
|
||||
type: list.type,
|
||||
name: list.name,
|
||||
archived: list.archived,
|
||||
openCount: 0,
|
||||
doneCount: 0,
|
||||
createdAt: list.createdAt.toISOString(),
|
||||
},
|
||||
201,
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user