Refactor: migrate files
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace routes;
|
||||
|
||||
use classes\authentication;
|
||||
use objects\customer_vehicles_o;
|
||||
use objects\logs_o;
|
||||
use objects\users_o;
|
||||
use traits\route_t;
|
||||
|
||||
class vehiclesRoute
|
||||
{
|
||||
use route_t;
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$this->get('/user/vehicles', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('list_own_vehicles');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Log the incident
|
||||
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'LIST_OWN_VEHICLES', 'Successfully listed own vehicles');
|
||||
// Return the list of the user's vehicles
|
||||
$response->success(
|
||||
(new customer_vehicles_o())->getCustomerVehiclesPaginated(
|
||||
$user->id,
|
||||
($this->fromRequest('page') ?? 1),
|
||||
($this->fromRequest('limit') ?? 10)
|
||||
)
|
||||
);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('vehicles', 'global', 1, 0, 'LIST_OWN_VEHICLES', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
});
|
||||
|
||||
$this->post('/user/vehicles', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('add_vehicle');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Get the post data
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
// Check if the required fields are set
|
||||
$data = $this->getData($data, $response);
|
||||
// Make sure the registration number is valid
|
||||
$this->validateRegistrationNumber($data['reg'], $response);
|
||||
// Make sure the type is valid
|
||||
$this->validateType($data['type'], $response);
|
||||
// Make sure the notes are valid
|
||||
$this->validateNotes($data['notes'], $response);
|
||||
// Create a new vehicle
|
||||
$vehicle = (new customer_vehicles_o())->add(
|
||||
$user->id,
|
||||
$data['type'],
|
||||
$data['reg'],
|
||||
$data['notes']
|
||||
);
|
||||
// Log the incident
|
||||
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'ADD_VEHICLE', 'Successfully added vehicle');
|
||||
// Return the new vehicle
|
||||
$response->success($vehicle->getArrayByObjectProperties());
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('vehicles', 'global', 1, 0, 'ADD_VEHICLE', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 401);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function getData(mixed $data, $response)
|
||||
{
|
||||
if (!isset($data['type'])) {
|
||||
$response->error('Type is required', 400);
|
||||
}
|
||||
if (!isset($data['reg'])) {
|
||||
$response->error('Registration number is required', 400);
|
||||
}
|
||||
if (!isset($data['notes'])) {
|
||||
$response->error('Notes is required', 400);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function validateRegistrationNumber(mixed $reg, $response): void
|
||||
{
|
||||
if (!preg_match('/^[A-Z0-9]{4,10}$/', $reg)) {
|
||||
$response->error('Invalid registration number, it must be 4-10 characters long, and only contain uppercase letters and numbers', 400);
|
||||
}
|
||||
}
|
||||
|
||||
private function validateType(mixed $type, $response): void
|
||||
{
|
||||
// Make sure the type is more than 2 characters
|
||||
if (strlen($type) < 2) {
|
||||
$response->error('Type is too short, it must be at least 2 characters', 400);
|
||||
}
|
||||
// Make sure the type is less than 50 characters
|
||||
if (strlen($type) > 50) {
|
||||
$response->error('Type is too long, it must be less than 50 characters', 400);
|
||||
}
|
||||
}
|
||||
|
||||
private function validateNotes(mixed $notes, $response): void
|
||||
{
|
||||
// If the notes are set, make sure they are less than 250 characters
|
||||
if (isset($notes) && strlen($notes) > 250) {
|
||||
$response->error('Notes are too long, they must be less than 250 characters', 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user