73 lines
3.3 KiB
PHP
73 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use objects\logs_o;
|
|
use objects\plate_scanners_o;
|
|
use objects\plate_scans_o;
|
|
use traits\route_t;
|
|
|
|
class plateScansRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
|
|
$this->post('/numberplatescans', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePlateScannerAuth();
|
|
// Get the plate scanner object
|
|
$plate_scanner = (new authentication())->get_plate_scanner();
|
|
// Get the post data
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
// Check if the required fields are set
|
|
if (!isset($data['plate'])) {
|
|
$response->error('Missing required body parameter plate', 400);
|
|
}
|
|
// Add the number plate scanner
|
|
(new plate_scans_o())->add($plate_scanner->id, $data['plate']);
|
|
// Log the incident
|
|
(new logs_o())->add('numberplatescans', $plate_scanner->department_id->value(), 1, 0, 'ADD_NUMBER_PLATE_SCANS', 'Successfully added a number plate scan' . $data['plate']);
|
|
// Return a success message
|
|
$response->success(['message' => 'License plate scan recorded.', 'plate' => $data['plate'], 'scanner' => $plate_scanner->name->value()], 201);
|
|
});
|
|
|
|
$this->post('/numberplatescans/department', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('list_number_plate_scans_department');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Check if a department is set in the body
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
if (!isset($data['department_id'])) {
|
|
$response->error('Missing required body parameter department', 400);
|
|
}
|
|
$this->requirePermission('list_number_plate_scans_department_' . $data['department_id']);
|
|
// Check if the pagination parameters are set
|
|
if (!isset($data['page'])) {
|
|
$response->error('Missing required body parameter page', 400);
|
|
}
|
|
if (!isset($data['limit'])) {
|
|
$response->error('Missing required body parameter limit', 400);
|
|
}
|
|
// Get the number plate scans
|
|
$number_plate_scans = (new plate_scans_o())->getPlateScansByDepartment((int)$data['department_id'], (int)$data['page'], (int)$data['limit']);
|
|
// Log the incident
|
|
(new logs_o())->add('numberplatescans', $data['department_id'], 1, $user->id, 'LIST_NUMBER_PLATE_SCANS_DEPARTMENT', 'Successfully listed number plate scans for department: ' . $data['department_id']);
|
|
// Return the number plate scans
|
|
$response->success($number_plate_scans);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('numberplatescans', 'global', 1, 0, 'LIST_NUMBER_PLATE_SCANS_DEPARTMENT', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
}
|
|
} |