This commit is contained in:
emmymayo
2025-02-04 23:06:08 +01:00
commit 77037e7e84
74 changed files with 33573 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
<?php
Flight::route('GET /v1/api/images', function()
{
$model = new ImagesModel();
$type_array = [
'id' => 'integer',
'url' => 'string',
'user_id' => 'integer',
'caption' => 'string'
];
$allow_fields = ['id', 'url', 'user_id', 'caption'];
$allow_column_fields = ['ID', 'URL', 'User ID', 'Caption'];
$format = isset($_GET['format']) ? $_GET['format'] : 'json';
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$per_page = isset($_GET['size']) ? intval($_GET['size']) : 25;
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'id';
$direction = isset($_GET['direction']) ? $_GET['direction'] : 'ASC';
//TODO: default sort/direction from cms builder
$query_service = new QueryService();
$format_service = new FormatService();
$custom_where = $query_service->create_where(
$type_array,
Flight::request()->query['field'] ?? [],
Flight::request()->query['operator'] ?? [],
Flight::request()->query['value'] ?? []);
$result = $model->get_paginated($page, $per_page, $custom_where, $sort, $direction);
// $result = $model->get_cursor_paginated($page, $per_page, $custom_where, $sort, $direction, $id);
if ($result)
{
$result['code'] = 200;
$result['error'] = false;
$result['query'] = $custom_where;
if ($format == 'json')
{
$result['data'] = $format_service->to_json($result['data'], $model->get_mapping(), $allow_column_fields, $allow_fields);
echo json_encode($result);
exit;
}
if ($format == 'csv')
{
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
echo implode("\n", $format_service->to_csv($result['list'], $model->get_mapping(), $allow_column_fields), $allow_fields);
exit();
}
}
else
{
echo json_encode([
'code' => 409,
'error' => false
]);
http_response_code(409);
exit;
}
});