rename clients to devices

This commit is contained in:
2025-01-07 16:20:40 -08:00
parent d99ee9ef1e
commit cc7c94417d
25 changed files with 284 additions and 287 deletions

View File

@@ -1,20 +0,0 @@
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { findClient, mapClientToDetails } from '$lib/server/clients';
export const GET: RequestHandler = async (event) => {
if (!event.locals.user) {
return error(401, 'Unauthorized');
}
const { id } = event.params;
const clientId = parseInt(id);
if (isNaN(clientId)) {
return error(400, 'Invalid client ID');
}
const client = await findClient(event.locals.user.id, clientId);
if (!client) {
return error(404, 'Client not found');
}
return new Response(JSON.stringify(mapClientToDetails(client)));
};

View File

@@ -1,29 +1,29 @@
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { createClient, findClients, mapClientToDetails } from '$lib/server/clients';
import { createDevice, findDevices, mapDeviceToDetails } from '$lib/server/devices';
export const GET: RequestHandler = async (event) => {
if (!event.locals.user) {
return error(401, 'Unauthorized');
}
const clients = await findClients(event.locals.user.id);
const devices = await findDevices(event.locals.user.id);
return new Response(
JSON.stringify({
clients: clients.map(mapClientToDetails),
devices: devices.map(mapDeviceToDetails),
}),
);
};
export type Clients = Awaited<ReturnType<typeof findClients>>;
export type Devices = Awaited<ReturnType<typeof findDevices>>;
export const POST: RequestHandler = async (event) => {
if (!event.locals.user) {
return error(401, 'Unauthorized');
}
const { name } = await event.request.json();
const res = await createClient({
const res = await createDevice({
name,
user: event.locals.user,
});

View File

@@ -0,0 +1,20 @@
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { findDevice, mapDeviceToDetails } from '$lib/server/devices';
export const GET: RequestHandler = async (event) => {
if (!event.locals.user) {
return error(401, 'Unauthorized');
}
const { id } = event.params;
const deviceId = parseInt(id);
if (isNaN(deviceId)) {
return error(400, 'Invalid device ID');
}
const device = await findDevice(event.locals.user.id, deviceId);
if (!device) {
return error(404, 'Device not found');
}
return new Response(JSON.stringify(mapDeviceToDetails(device)));
};