Refactor database operations and enhance routes for consistency

Refactored database methods to improve code readability, reusability, and error handling. Introduced input validation helper functions and parsing capabilities for objects with optional callbacks. Added new routes for handling categories and improved product-related functionality to align with the new architecture.
This commit is contained in:
Jepp9350
2025-02-12 16:39:52 +01:00
parent c240bbd94f
commit 7525a89154
11 changed files with 375 additions and 66 deletions
+68 -48
View File
@@ -14,6 +14,22 @@ class productsRoute
public function run(): void
{
$this->get('/products', function () {
function parseProduct($product): array
{
return [
'id' => (int)$product['id'],
'name' => (string)$product['name'],
'description' => (string)$product['description'],
'price' => (int)$product['price'],
'category' => (int)$product['category'],
'piktogram' => (string)$product['piktogram'],
'economic_product_id' => (int)$product['economic_product_id'],
'apply_category_discount' => (int)$product['apply_category_discount'],
'created_at' => (string)$product['created_at'],
'updated_at' => (string)$product['updated_at']
];
}
// Require the user to be logged in
global $response;
$this->requirePermission('list_products');
@@ -44,12 +60,18 @@ class productsRoute
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'])
(new products_o())->applyDepartmentPricing((array)(new products_o())->listObjectsWithPaginationIfSet(
function ($product) {
return parseProduct($product);
}
), (int)$data['department_id'])
);
}
// Return the list of products
$response->success(
(new products_o())->listObjectsWithPaginationIfSet()
(new products_o())->listObjectsWithPaginationIfSet(function ($product) {
return parseProduct($product);
})
);
} else {
// Log the incident
@@ -67,31 +89,31 @@ class productsRoute
$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);
// Get the request data
$name = $response->getRequestParameter('name');
$description = $response->getRequestParameter('description') ?? null;
$price = $response->getRequestParameter('price');
$category = $response->getRequestParameter('category') ?? null;
$piktogram = $response->getRequestParameter('piktogram') ?? null;
$economicProductId = $response->getRequestParameter('economicProductId') ?? null;
// 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);
}
self::requireParameters(
[
'name',
'price'
]
);
// Add the product
(new products_o())->add($data['name'], $data['description'], $data['price'], $data['category'], $data['piktogram'], $data['economicProductId']);
(new products_o())->add(
(string)$name,
(string)$description ?? '',
(int)$price,
(int)$category ?? '',
(int)$piktogram ?? null,
(int)$economicProductId ?? null
);
// Log the incident
(new logs_o())->add('products', 'global', 1, $user->id, 'ADD_PRODUCT', 'Product name: ' . $data['name']);
(new logs_o())->add('products', 'global', 1, $user->id, 'ADD_PRODUCT', 'Product name: ' . $name);
// Return a success message
$response->success(['message' => 'Product added successfully']);
} else {
@@ -110,39 +132,37 @@ class productsRoute
$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);
self::requireParameters(['id']);
$id = $response->getRequestParameter('id');
$product = (new products_o())->select((int)$id);
// Check if the product exists
if (!$product->exists()) {
$response->error('Product not found', 404);
}
if (!isset($data['name'])) {
$response->error('Name is required', 400);
// Update the fields that are set
if (self::isParametersSet(['name'])) {
$product->name->set($response->getRequestParameter('name'));
}
if (!isset($data['description'])) {
$response->error('Description is required', 400);
if (self::isParametersSet(['description'])) {
$product->description->set($response->getRequestParameter('description') ?? '');
}
if (!isset($data['price'])) {
$response->error('Price is required', 400);
if (self::isParametersSet(['price'])) {
$product->price->set($response->getRequestParameter('price'));
}
if (!isset($data['category'])) {
$response->error('Category is required', 400);
if (self::isParametersSet(['category'])) {
$product->category->set($response->getRequestParameter('category') ?? '');
}
if (!isset($data['piktogram'])) {
$response->error('Piktogram is required', 400);
if (self::isParametersSet(['piktogram'])) {
$product->piktogram->set($response->getRequestParameter('piktogram') ?? '');
}
if (!isset($data['economicProductId'])) {
$response->error('Economic product ID is required', 400);
if (self::isParametersSet(['economic_product_id'])) {
$product->economic_product_id->set($response->getRequestParameter('economic_product_id') ?? '');
}
if (!isset($data['applyCategoryDiscount'])) {
$data['applyCategoryDiscount'] = true;
} else {
$data['applyCategoryDiscount'] = (bool)$data['applyCategoryDiscount'];
if (self::isParametersSet(['apply_category_discount'])) {
$product->apply_category_discount->set($response->getRequestParameter('apply_category_discount') ? 1 : 0);
}
// 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']);
(new logs_o())->add('products', 'global', 1, $user->id, 'EDIT_PRODUCT', 'Product id: ' . $id);
// Return a success message
$response->success(['message' => 'Product edited successfully']);
} else {