rename clients to devices
This commit is contained in:
@@ -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)));
|
||||
};
|
||||
@@ -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,
|
||||
});
|
||||
20
src/routes/api/devices/[id]/+server.ts
Normal file
20
src/routes/api/devices/[id]/+server.ts
Normal 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)));
|
||||
};
|
||||
Reference in New Issue
Block a user