-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters