29 lines
978 B
TypeScript
29 lines
978 B
TypeScript
|
|
import type { RequestHandler } from './$types';
|
||
|
|
import { json, error } from '@sveltejs/kit';
|
||
|
|
import { z } from 'zod';
|
||
|
|
import { getDb } from '$lib/server/db';
|
||
|
|
import { deleteProfile, renameProfile } from '$lib/server/profiles/repository';
|
||
|
|
|
||
|
|
const RenameSchema = z.object({ name: z.string().min(1).max(50) });
|
||
|
|
|
||
|
|
function parseId(raw: string): number {
|
||
|
|
const id = Number(raw);
|
||
|
|
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
|
||
|
|
return id;
|
||
|
|
}
|
||
|
|
|
||
|
|
export const PATCH: RequestHandler = async ({ params, request }) => {
|
||
|
|
const id = parseId(params.id!);
|
||
|
|
const body = await request.json().catch(() => null);
|
||
|
|
const parsed = RenameSchema.safeParse(body);
|
||
|
|
if (!parsed.success) error(400, { message: 'Invalid body' });
|
||
|
|
renameProfile(getDb(), id, parsed.data.name);
|
||
|
|
return json({ ok: true });
|
||
|
|
};
|
||
|
|
|
||
|
|
export const DELETE: RequestHandler = async ({ params }) => {
|
||
|
|
const id = parseId(params.id!);
|
||
|
|
deleteProfile(getDb(), id);
|
||
|
|
return json({ ok: true });
|
||
|
|
};
|