feat: 全局设置系统 — settings 表、CRUD 路由、主题偏好持久化
This commit is contained in:
38
src/server/routes/settings.ts
Normal file
38
src/server/routes/settings.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import type Database from "bun:sqlite";
|
||||
|
||||
import type { RuntimeMode, SettingsData } from "../../shared/api";
|
||||
import type { Logger } from "../logger";
|
||||
|
||||
import { getSettings, updateSettings } from "../db/settings";
|
||||
import { createApiError, jsonResponse } from "../helpers";
|
||||
|
||||
export function handleGetSettings(_req: Request, db: Database, mode: RuntimeMode, _logger: Logger): Response {
|
||||
const data = getSettings(db);
|
||||
return jsonResponse(data, { mode });
|
||||
}
|
||||
|
||||
export async function handleUpdateSettings(
|
||||
req: Request,
|
||||
db: Database,
|
||||
mode: RuntimeMode,
|
||||
logger: Logger,
|
||||
): Promise<Response> {
|
||||
let body: Partial<SettingsData>;
|
||||
try {
|
||||
body = (await req.json()) as Partial<SettingsData>;
|
||||
} catch {
|
||||
return jsonResponse(createApiError("Invalid JSON body", 400), { mode, status: 400 });
|
||||
}
|
||||
|
||||
if (body.theme !== undefined && typeof body.theme !== "string") {
|
||||
return jsonResponse(createApiError("theme must be a string", 400), { mode, status: 400 });
|
||||
}
|
||||
|
||||
if (body.theme !== undefined && body.theme !== "dark" && body.theme !== "light" && body.theme !== "system") {
|
||||
return jsonResponse(createApiError("theme 仅支持 dark、light、system", 400), { mode, status: 400 });
|
||||
}
|
||||
|
||||
const result = updateSettings(db, body, logger);
|
||||
logger.info({ data: result }, "设置已更新");
|
||||
return jsonResponse(result, { mode });
|
||||
}
|
||||
Reference in New Issue
Block a user