2026-04-17 15:12:59 +02:00
|
|
|
import type { RequestHandler } from './$types';
|
2026-04-18 22:19:12 +02:00
|
|
|
import { json, error, isHttpError } from '@sveltejs/kit';
|
2026-04-18 08:28:02 +02:00
|
|
|
import { z } from 'zod';
|
2026-04-17 15:12:59 +02:00
|
|
|
import { getDb } from '$lib/server/db';
|
2026-04-18 22:19:12 +02:00
|
|
|
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
|
2026-04-18 08:28:02 +02:00
|
|
|
import {
|
|
|
|
|
removeDomain,
|
|
|
|
|
updateDomain,
|
|
|
|
|
setDomainFavicon
|
|
|
|
|
} from '$lib/server/domains/repository';
|
|
|
|
|
import { fetchAndStoreFavicon } from '$lib/server/domains/favicons';
|
2026-04-18 22:41:02 +02:00
|
|
|
import { IMAGE_DIR } from '$lib/server/paths';
|
2026-04-18 08:28:02 +02:00
|
|
|
|
|
|
|
|
const UpdateSchema = z.object({
|
|
|
|
|
domain: z.string().min(3).max(253).optional(),
|
|
|
|
|
display_name: z.string().max(100).nullable().optional()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const PATCH: RequestHandler = async ({ params, request }) => {
|
2026-04-18 22:19:12 +02:00
|
|
|
const id = parsePositiveIntParam(params.id, 'id');
|
|
|
|
|
const data = validateBody(await request.json().catch(() => null), UpdateSchema);
|
2026-04-18 08:28:02 +02:00
|
|
|
try {
|
|
|
|
|
const db = getDb();
|
2026-04-18 22:19:12 +02:00
|
|
|
const updated = updateDomain(db, id, data);
|
2026-04-18 08:28:02 +02:00
|
|
|
if (!updated) error(404, { message: 'Not found' });
|
|
|
|
|
// Wenn updateDomain favicon_path genullt hat (Domain geändert), frisch laden.
|
|
|
|
|
if (updated.favicon_path === null) {
|
|
|
|
|
const path = await fetchAndStoreFavicon(updated.domain, IMAGE_DIR);
|
|
|
|
|
if (path) {
|
|
|
|
|
setDomainFavicon(db, updated.id, path);
|
|
|
|
|
updated.favicon_path = path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return json(updated);
|
|
|
|
|
} catch (e) {
|
2026-04-18 22:19:12 +02:00
|
|
|
// HTTP-Errors aus error() durchreichen, sonst landet ein 404 als 409.
|
|
|
|
|
if (isHttpError(e)) throw e;
|
2026-04-18 08:28:02 +02:00
|
|
|
error(409, { message: (e as Error).message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const DELETE: RequestHandler = async ({ params }) => {
|
2026-04-18 22:19:12 +02:00
|
|
|
const id = parsePositiveIntParam(params.id, 'id');
|
2026-04-17 15:12:59 +02:00
|
|
|
removeDomain(getDb(), id);
|
|
|
|
|
return json({ ok: true });
|
|
|
|
|
};
|