Refactor: migrate files
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace routes;
|
||||
|
||||
use classes\authentication;
|
||||
use objects\logs_o;
|
||||
use objects\products_o;
|
||||
use traits\route_t;
|
||||
|
||||
class productsRoute
|
||||
{
|
||||
use route_t;
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$this->get('/products', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('list_products');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Check if the category is set in the request
|
||||
$data = $_GET ?? [];
|
||||
// Check if the category is set
|
||||
if (isset($data['category'])) {
|
||||
// Log the incident
|
||||
(new logs_o())->add('products', 'global', 1, $user->id, 'LIST_PRODUCTS', 'Successfully listed products in category ' . $data['category']);
|
||||
// Return the list of products
|
||||
$products = (new products_o())->listObjectsByCategory($data['category']);
|
||||
// Check if the department_id is set
|
||||
if (isset($data['department_id'])) {
|
||||
// Apply the departments unique pricing
|
||||
$products = (new products_o())->applyDepartmentPricing((array)$products, (int)$data['department_id']);
|
||||
}
|
||||
$response->success(
|
||||
$products
|
||||
);
|
||||
}
|
||||
// Log the incident
|
||||
(new logs_o())->add('products', 'global', 1, $user->id, 'LIST_PRODUCTS', 'Successfully listed products');
|
||||
// Check if the department_id is set
|
||||
if (isset($data['department_id'])) {
|
||||
// Return the list of products
|
||||
$response->success(
|
||||
(new products_o())->applyDepartmentPricing((array)(new products_o())->listObjectsWithPaginationIfSet(), (int)$data['department_id'])
|
||||
);
|
||||
}
|
||||
// Return the list of products
|
||||
$response->success(
|
||||
(new products_o())->listObjectsWithPaginationIfSet()
|
||||
);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('products', 'global', 1, 0, 'LIST_PRODUCTS', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
});
|
||||
|
||||
$this->post('/products', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('add_product');
|
||||
// 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
|
||||
if (!isset($data['name'])) {
|
||||
$response->error('Name is required', 400);
|
||||
}
|
||||
if (!isset($data['description'])) {
|
||||
$response->error('Description is required', 400);
|
||||
}
|
||||
if (!isset($data['price'])) {
|
||||
$response->error('Price is required', 400);
|
||||
}
|
||||
if (!isset($data['category'])) {
|
||||
$response->error('Category is required', 400);
|
||||
}
|
||||
if (!isset($data['piktogram'])) {
|
||||
$response->error('Piktogram is required', 400);
|
||||
}
|
||||
if (!isset($data['economicProductId'])) {
|
||||
$response->error('Economic product ID is required', 400);
|
||||
}
|
||||
// Add the product
|
||||
(new products_o())->add($data['name'], $data['description'], $data['price'], $data['category'], $data['piktogram'], $data['economicProductId']);
|
||||
// Log the incident
|
||||
(new logs_o())->add('products', 'global', 1, $user->id, 'ADD_PRODUCT', 'Product name: ' . $data['name']);
|
||||
// Return a success message
|
||||
$response->success(['message' => 'Product added successfully']);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('products', 'global', 1, 0, 'ADD_PRODUCT', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
});
|
||||
|
||||
$this->put('/products', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('edit_product');
|
||||
// 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
|
||||
if (!isset($data['id'])) {
|
||||
$response->error('ID is required', 400);
|
||||
}
|
||||
if (!isset($data['name'])) {
|
||||
$response->error('Name is required', 400);
|
||||
}
|
||||
if (!isset($data['description'])) {
|
||||
$response->error('Description is required', 400);
|
||||
}
|
||||
if (!isset($data['price'])) {
|
||||
$response->error('Price is required', 400);
|
||||
}
|
||||
if (!isset($data['category'])) {
|
||||
$response->error('Category is required', 400);
|
||||
}
|
||||
if (!isset($data['piktogram'])) {
|
||||
$response->error('Piktogram is required', 400);
|
||||
}
|
||||
if (!isset($data['economicProductId'])) {
|
||||
$response->error('Economic product ID is required', 400);
|
||||
}
|
||||
if (!isset($data['applyCategoryDiscount'])) {
|
||||
$data['applyCategoryDiscount'] = true;
|
||||
} else {
|
||||
$data['applyCategoryDiscount'] = (bool)$data['applyCategoryDiscount'];
|
||||
}
|
||||
// Edit the product
|
||||
(new products_o())->edit($data['id'], $data['name'], $data['description'], $data['price'], $data['category'], $data['piktogram'], $data['economicProductId'], $data['applyCategoryDiscount']);
|
||||
// Log the incident
|
||||
(new logs_o())->add('products', 'global', 1, $user->id, 'EDIT_PRODUCT', 'Product id: ' . $data['id']);
|
||||
// Return a success message
|
||||
$response->success(['message' => 'Product edited successfully']);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('products', 'global', 1, 0, 'EDIT_PRODUCT', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user