Skip to content

Commit

Permalink
add index logic
Browse files Browse the repository at this point in the history
  • Loading branch information
LoV432 committed Nov 3, 2023
1 parent 80567f9 commit 6a7e890
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
16 changes: 14 additions & 2 deletions app/api/dhcp-event/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type dhcpEventType = {

export type userReturnType = {
id: number;
indexNumber: number;
displayName: string;
name: string;
ip: string;
Expand Down Expand Up @@ -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
});
Expand Down
70 changes: 70 additions & 0 deletions app/api/edit/change-index/route.ts
Original file line number Diff line number Diff line change
@@ -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
});
}
3 changes: 3 additions & 0 deletions app/components/user-cards/UserCards.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
<UserCard
name={user.name}
Expand Down
1 change: 1 addition & 0 deletions lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const db = new Database('./db/next-openwrt-stats.db');

const createUserTable = db.prepare(`CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
indexNumber INTEGER,
name TEXT,
displayName TEXT,
ip TEXT,
Expand Down

0 comments on commit 6a7e890

Please sign in to comment.