feat: api v1 routes for calendar lists and notes
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { deleteCalendarForScope, updateCalendarForScope } from "@/modules/calendar/server/actions";
|
||||
import { calendarUpdateInput } from "@/modules/calendar/server/schemas";
|
||||
import { getCalendarForScope } from "@/modules/calendar/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 calendar = await getCalendarForScope(scope, id);
|
||||
return apiJson(calendar);
|
||||
});
|
||||
}
|
||||
|
||||
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 = calendarUpdateInput.parse(body);
|
||||
await updateCalendarForScope(scope, { id, ...parsed });
|
||||
const calendar = await getCalendarForScope(scope, id);
|
||||
return apiJson(calendar);
|
||||
});
|
||||
}
|
||||
|
||||
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 deleteCalendarForScope(scope, { id });
|
||||
return apiJson({ ok: true });
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { createCalendarForScope } from "@/modules/calendar/server/actions";
|
||||
import { calendarInput } from "@/modules/calendar/server/schemas";
|
||||
import { listCalendarsForScope } from "@/modules/calendar/server/queries";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
return withApiHandler(request, async (scope) => {
|
||||
const calendars = await listCalendarsForScope(scope);
|
||||
return apiJson(calendars);
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
const body: unknown = await req.json();
|
||||
const parsed = calendarInput.parse(body);
|
||||
const calendar = await createCalendarForScope(scope, parsed);
|
||||
return apiJson(
|
||||
{
|
||||
id: calendar.id,
|
||||
name: calendar.name,
|
||||
color: calendar.color,
|
||||
visibility: calendar.visibility as "private" | "household",
|
||||
ownerId: calendar.ownerId,
|
||||
},
|
||||
201,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { deleteEventForScope, updateEventForScope } from "@/modules/calendar/server/actions";
|
||||
import { eventUpdateInput } from "@/modules/calendar/server/schemas";
|
||||
import { getEventForScope } from "@/modules/calendar/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 event = await getEventForScope(scope, id);
|
||||
return apiJson(event);
|
||||
});
|
||||
}
|
||||
|
||||
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 = eventUpdateInput.parse(body);
|
||||
await updateEventForScope(scope, { id, ...parsed });
|
||||
const event = await getEventForScope(scope, id);
|
||||
return apiJson(event);
|
||||
});
|
||||
}
|
||||
|
||||
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 deleteEventForScope(scope, { id });
|
||||
return apiJson({ ok: true });
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { apiError, apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { createEventForScope } from "@/modules/calendar/server/actions";
|
||||
import { eventInput } from "@/modules/calendar/server/schemas";
|
||||
import { listEventsForScope } from "@/modules/calendar/server/queries";
|
||||
|
||||
function parseCalendarIds(value: string | null): "all" | string[] {
|
||||
if (!value || value === "all") return "all";
|
||||
return value
|
||||
.split(",")
|
||||
.map((id) => id.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
const url = new URL(req.url);
|
||||
const from = url.searchParams.get("from");
|
||||
const to = url.searchParams.get("to");
|
||||
const calendarIds = parseCalendarIds(url.searchParams.get("calendarIds"));
|
||||
|
||||
if (!from || !to) {
|
||||
return apiError("from and to query parameters are required", 400);
|
||||
}
|
||||
|
||||
const events = await listEventsForScope(scope, { from, to, calendarIds });
|
||||
return apiJson(events);
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
const body: unknown = await req.json();
|
||||
const parsed = eventInput.parse(body);
|
||||
const event = await createEventForScope(scope, parsed);
|
||||
return apiJson(event, 201);
|
||||
});
|
||||
}
|
||||
@@ -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,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { deleteNoteForScope, updateNoteForScope } from "@/modules/notes/server/actions";
|
||||
import { updateNoteInput } from "@/modules/notes/server/schemas";
|
||||
import { getNoteForScope } from "@/modules/notes/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 note = await getNoteForScope(scope.householdId, id);
|
||||
return apiJson(note);
|
||||
});
|
||||
}
|
||||
|
||||
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 = updateNoteInput.omit({ id: true }).parse(body);
|
||||
const note = await updateNoteForScope(scope, { id, ...parsed });
|
||||
return apiJson(note);
|
||||
});
|
||||
}
|
||||
|
||||
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 deleteNoteForScope(scope, { id });
|
||||
return apiJson({ ok: true });
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
||||
import { createNoteForScope } from "@/modules/notes/server/actions";
|
||||
import { noteInput } from "@/modules/notes/server/schemas";
|
||||
import { listNotesForScope } from "@/modules/notes/server/queries";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
return withApiHandler(request, async (scope) => {
|
||||
const notes = await listNotesForScope(scope.householdId);
|
||||
return apiJson(notes);
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
return withApiHandler(request, async (scope, req) => {
|
||||
const body: unknown = await req.json();
|
||||
const parsed = noteInput.parse(body);
|
||||
const note = await createNoteForScope(scope, parsed);
|
||||
return apiJson(note, 201);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user