From 6a7e89008b35c3f40b295db9d59977edd2943638 Mon Sep 17 00:00:00 2001 From: LoV432 Date: Fri, 3 Nov 2023 19:55:26 +0500 Subject: [PATCH] add index logic --- app/api/dhcp-event/route.ts | 16 ++++- app/api/edit/change-index/route.ts | 70 +++++++++++++++++++ .../user-cards/UserCards.server.tsx | 3 + lib/db.ts | 1 + 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 app/api/edit/change-index/route.ts diff --git a/app/api/dhcp-event/route.ts b/app/api/dhcp-event/route.ts index 5c3e699..203be99 100644 --- a/app/api/dhcp-event/route.ts +++ b/app/api/dhcp-event/route.ts @@ -9,6 +9,7 @@ type dhcpEventType = { export type userReturnType = { id: number; + indexNumber: number; displayName: string; name: string; ip: string; @@ -50,11 +51,22 @@ export async function POST(request: Request) { status: 200 }); } else { + let highestIndex = db + .prepare('SELECT MAX(indexNumber) FROM users') + .get() as { 'MAX(indexNumber)': number }; let insertDevice = db .prepare( - 'INSERT INTO users (name, ip, macaddress, lastupdated, devicetype, lastEventType) VALUES (?, ?, ?, ?, ?, ?)' + 'INSERT INTO users (indexNumber, name, ip, macaddress, lastupdated, devicetype, lastEventType) VALUES (?, ?, ?, ?, ?, ?, ?)' ) - .run(body.hostname, body.ip, body.mac, Date.now(), 'generic', body.type); + .run( + highestIndex['MAX(indexNumber)'] + 1, + body.hostname, + body.ip, + body.mac, + Date.now(), + 'generic', + body.type + ); return new Response(JSON.stringify(insertDevice), { status: 200 }); diff --git a/app/api/edit/change-index/route.ts b/app/api/edit/change-index/route.ts new file mode 100644 index 0000000..ab7910c --- /dev/null +++ b/app/api/edit/change-index/route.ts @@ -0,0 +1,70 @@ +import { db } from '@/lib/db'; +import { userReturnType } from '../../dhcp-event/route'; + +type changeIndexType = { + index: string; + macAddress: string; +}; + +export async function POST(request: Request) { + const body = (await request.json()) as changeIndexType; + if ( + !body.index || + body.index === '' || + !body.macAddress || + body.macAddress === '' + ) { + return new Response(JSON.stringify({ error: 'Missing required fields' }), { + status: 400 + }); + } + + let currentUser = db + .prepare('SELECT * FROM users WHERE macaddress = ?') + .get(body.macAddress) as userReturnType | undefined; + if (!currentUser) { + return new Response( + JSON.stringify({ error: 'No device with that MAC address' }), + { + status: 400 + } + ); + } + + let conflictingUser = db + .prepare('SELECT * FROM users WHERE indexNumber = ?') + .get(body.index) as userReturnType | undefined; + if (!conflictingUser) { + let updateIndex = db + .prepare('UPDATE users SET indexNumber = ? WHERE macaddress = ?') + .run(body.index, body.macAddress); + return new Response(JSON.stringify(updateIndex), { + status: 200 + }); + } + + let updateIndex = db.prepare( + 'UPDATE users SET indexNumber = @index WHERE macaddress = @mac' + ); + + let updateMany = db.transaction((users: { index: number; mac: string }[]) => { + users.forEach((user) => { + updateIndex.run(user); + }); + }); + + let updateIndexMany = updateMany([ + { + index: parseInt(body.index), + mac: body.macAddress + }, + { + index: currentUser.indexNumber, + mac: conflictingUser.macaddress + } + ]); + + return new Response(JSON.stringify(updateIndexMany), { + status: 200 + }); +} diff --git a/app/components/user-cards/UserCards.server.tsx b/app/components/user-cards/UserCards.server.tsx index 436e2ea..68d578c 100644 --- a/app/components/user-cards/UserCards.server.tsx +++ b/app/components/user-cards/UserCards.server.tsx @@ -3,6 +3,9 @@ import UserCard from './UserCard.client'; import { userReturnType } from '@/app/api/dhcp-event/route'; export default function UserCards() { let allUsers = db.prepare('SELECT * FROM users').all() as userReturnType[]; + allUsers.sort((a, b) => { + return a.indexNumber - b.indexNumber; + }); return allUsers.map((user) => (