36 lines
996 B
TypeScript
36 lines
996 B
TypeScript
import { db } from "#server/db";
|
|
import * as tables from "#server/db/schema";
|
|
import { eq } from "drizzle-orm";
|
|
import { z } from "zod";
|
|
|
|
const paramsSchema = z.object({
|
|
id: z.string().transform(Number),
|
|
});
|
|
|
|
const bodySchema = z.object({
|
|
name: z.string().min(1).optional(),
|
|
path: z.string().min(1).optional(),
|
|
url: z.url().optional(),
|
|
});
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const params = await getValidatedRouterParams(event, paramsSchema.parse);
|
|
const body = await readValidatedBody(event, bodySchema.parse);
|
|
|
|
const conflicts = await findConflictingFields(body, params.id);
|
|
if (conflicts.length > 0) {
|
|
throw createError({ statusCode: 409, message: `A link with this ${joinFields(conflicts)} already exists` });
|
|
}
|
|
|
|
const [link] = await db
|
|
.update(tables.links)
|
|
.set(body)
|
|
.where(eq(tables.links.id, params.id))
|
|
.returning();
|
|
if (!link) {
|
|
throw createError({ statusCode: 404, message: "Link not found" });
|
|
}
|
|
|
|
return link;
|
|
});
|