generated from Dicken/dickendock
Commit 2: REST-API für Geräte und Dienste (Zod-Validierung, Drizzle-Repositories)
This commit is contained in:
122
apps/backend/src/db/repositories/services.ts
Normal file
122
apps/backend/src/db/repositories/services.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { eq } from "drizzle-orm";
|
||||
import type { Service, ServiceCreateInput, ServiceUpdateInput } from "@launchpad/shared";
|
||||
import { db } from "../client.js";
|
||||
import { services } from "../schema.js";
|
||||
|
||||
function nowIso(): string {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
function mapRow(row: typeof services.$inferSelect): Service {
|
||||
return {
|
||||
id: row.id,
|
||||
deviceId: row.deviceId,
|
||||
displayName: row.displayName,
|
||||
hostname: row.hostname,
|
||||
url: row.url,
|
||||
https: row.https,
|
||||
port: row.port,
|
||||
category: row.category,
|
||||
icon: row.icon,
|
||||
favicon: row.favicon,
|
||||
description: row.description,
|
||||
favorite: row.favorite,
|
||||
alias: JSON.parse(row.alias) as string[],
|
||||
order: row.order,
|
||||
};
|
||||
}
|
||||
|
||||
export interface ServiceFilter {
|
||||
deviceId?: string;
|
||||
category?: string;
|
||||
favorite?: boolean;
|
||||
}
|
||||
|
||||
export function listServices(filter: ServiceFilter = {}): Service[] {
|
||||
return db
|
||||
.select()
|
||||
.from(services)
|
||||
.all()
|
||||
.map(mapRow)
|
||||
.filter((service) => {
|
||||
if (filter.deviceId && service.deviceId !== filter.deviceId) return false;
|
||||
if (filter.category && service.category !== filter.category) return false;
|
||||
if (filter.favorite !== undefined && service.favorite !== filter.favorite) return false;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
export function listServicesByDevice(deviceId: string): Service[] {
|
||||
return listServices({ deviceId });
|
||||
}
|
||||
|
||||
export function getService(id: string): Service | null {
|
||||
const row = db.select().from(services).where(eq(services.id, id)).get();
|
||||
return row ? mapRow(row) : null;
|
||||
}
|
||||
|
||||
export function createService(input: ServiceCreateInput): Service {
|
||||
const id = randomUUID();
|
||||
const timestamp = nowIso();
|
||||
|
||||
db.insert(services)
|
||||
.values({
|
||||
id,
|
||||
deviceId: input.deviceId,
|
||||
displayName: input.displayName,
|
||||
hostname: input.hostname,
|
||||
url: input.url,
|
||||
https: input.https ?? false,
|
||||
port: input.port,
|
||||
category: input.category ?? null,
|
||||
icon: input.icon ?? null,
|
||||
favicon: input.favicon ?? null,
|
||||
description: input.description ?? null,
|
||||
favorite: input.favorite ?? false,
|
||||
alias: JSON.stringify(input.alias ?? []),
|
||||
order: input.order ?? 0,
|
||||
createdAt: timestamp,
|
||||
updatedAt: timestamp,
|
||||
})
|
||||
.run();
|
||||
|
||||
return getService(id)!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktualisiert einen Dienst anhand von Benutzereingaben (z. B. über die Admin-UI).
|
||||
* Ein separater Pfad für automatische Scan-Ergebnisse folgt in einem späteren
|
||||
* Commit (siehe docs/ROADMAP.md, Commit 5 – Scanner): Scans dürfen displayName,
|
||||
* category, favorite, order, alias und icon niemals überschreiben.
|
||||
*/
|
||||
export function updateService(id: string, input: ServiceUpdateInput): Service | null {
|
||||
const existing = getService(id);
|
||||
if (!existing) return null;
|
||||
|
||||
db.update(services)
|
||||
.set({
|
||||
...(input.displayName !== undefined && { displayName: input.displayName }),
|
||||
...(input.hostname !== undefined && { hostname: input.hostname }),
|
||||
...(input.url !== undefined && { url: input.url }),
|
||||
...(input.https !== undefined && { https: input.https }),
|
||||
...(input.port !== undefined && { port: input.port }),
|
||||
...(input.category !== undefined && { category: input.category }),
|
||||
...(input.icon !== undefined && { icon: input.icon }),
|
||||
...(input.favicon !== undefined && { favicon: input.favicon }),
|
||||
...(input.description !== undefined && { description: input.description }),
|
||||
...(input.favorite !== undefined && { favorite: input.favorite }),
|
||||
...(input.alias !== undefined && { alias: JSON.stringify(input.alias) }),
|
||||
...(input.order !== undefined && { order: input.order }),
|
||||
updatedAt: nowIso(),
|
||||
})
|
||||
.where(eq(services.id, id))
|
||||
.run();
|
||||
|
||||
return getService(id);
|
||||
}
|
||||
|
||||
export function deleteService(id: string): boolean {
|
||||
const result = db.delete(services).where(eq(services.id, id)).run();
|
||||
return result.changes > 0;
|
||||
}
|
||||
Reference in New Issue
Block a user