Commit b2ba39d2 by Pfuhl, Thomas

API filter better adapted to DINA guidelines

parent c96a6e73
......@@ -645,71 +645,76 @@ class CAP_NAMEController extends Controller {
*/
public function apiFilter() {
// examples:
// ?id=1
// ?filter[id]=1
// ?filter[id]=1,2
// ?filter[id]=1&filter[title]=my title
// ?filter[amount]=>,25
// ?filter[amount]=<,25
// ?filter[amount]=<,25&page[offset]=0&page[limit]=10
$startTime = microtime(true);
$query = MODEL_NAME::query();
$order="id";
$direction="asc";
$filter = \Request::get('filter');
$humanReadableFilter = "any";
if ($filter)
foreach ($filter as $key=>$value) {
list($operator,$val) = explode(",", $value);
$humanReadableFilter .= " AND $key $operator '$val'";
$query = $query->where($key, $operator, $val);
}
// examples:
// ?id=1
// ?filter[id]=1
// ?filter[id]=1,2
// ?filter[id]=1&filter[title]=my title
// ?filter[amount]=>,25
// ?filter[amount]=<,25
// ?filter[amount]=<,25&page[offset]=0&page[limit]=10
$query = $query->orderBy($order, $direction);
$startTime = microtime(true);
$pagination = \Request::get('page');
if ($pagination)
$query = $query->skip(intval($pagination["offset"]))->take(intval($pagination["limit"]));
$query = MODEL_NAME::query();
$records = $query->get();
$order="id";
$direction="asc";
if (!count($records)) {
return "{}";
}
$filter = \Request::get('filter');
$humanReadableFilter = "any";
if ($filter)
foreach ($filter as $key=>$value) {
list($operator,$val) = explode(",", $value);
$humanReadableFilter .= " AND $key $operator '$val'";
$query = $query->where($key, $operator, $val);
}
//$propertyNames = MODEL_NAME::first()["fillable"]; // without field "id"
$propertyNames = array_keys($records[0]->getAttributes()); // with field "id"
$attributes = array_keys($records[0]->getAttributes());
$query = $query->orderBy($order, $direction);
$extPropertyValues = [];
$pagination = \Request::get('page');
if ($pagination)
$query = $query->skip(intval($pagination["offset"]))->take(intval($pagination["limit"]));
// @todo make this a recursive function, using env('GDM_DATA_DEPTH')
foreach ($records as $record) {
$records = $query->get();
$basicValues = [];
//$basicValues["self"] = env('GDM_NAME') .'/' . env('GDM_DATAMODEL_VERSION') . '/CAP_NAME/' . $record["attributes"]["id"];
//$basicValues["self"] = 'GDM_NAME/GDM_DATAMODEL_VERSION/CAP_NAME/' . $record["attributes"]["id"];
$basicValues["self"] = 'api/' . env('GDM_NAME') . '/' . env('GDM_DATAMODEL_VERSION') . '/CAP_NAME/' . $record["attributes"]["id"];
$basicValues["type"] = "record";
$basicValues["id"] = $record["attributes"]["id"];
if (!count($records)) {
$collection = [];
}
else {
$dina_attributes = $record["attributes"];
array_shift($dina_attributes);
//$propertyNames = MODEL_NAME::first()["fillable"]; // without field "id"
$propertyNames = array_keys($records[0]->getAttributes()); // with field "id"
$attributes = array_keys($records[0]->getAttributes());
$basicValues["attributes"] = $dina_attributes;
$extPropertyValues = [];
RELATIONS
$extValues = $basicValues;
$extPropertyValues[] = $extValues;
}
// @todo make this a recursive function, using env('GDM_DATA_DEPTH')
foreach ($records as $record) {
$collection = collect($extPropertyValues);
$basicValues = [];
$basicValues["self"] = env('GDM_PROTOCOL') . '://' . env('GDM_URL') . (env('GDM_PORT')==80 ? '' : ':' . env('GDM_PORT'))
. '/api/' . env('GDM_NAME') . '/' . env('GDM_DATAMODEL_VERSION') . '/CAP_NAME/' . $record["attributes"]["id"];
$basicValues["type"] = "record";
$basicValues["id"] = $record["attributes"]["id"];
$dina_attributes = $record["attributes"];
array_shift($dina_attributes);
$basicValues["attributes"] = $dina_attributes;
$dina_links = new \stdClass();
$dina_links->self = $basicValues["self"];
$basicValues["links"] = $dina_links;
RELATIONS
$extValues = $basicValues;
$extPropertyValues[] = $extValues;
}
$collection = collect($extPropertyValues);
}
$milliseconds = round(1000 * (microtime(true) - $startTime));
$jsonapi = [];
......@@ -720,11 +725,13 @@ class CAP_NAMEController extends Controller {
$meta_infos["copyright"] = env('GDM_COPYRIGHT');
$meta_infos["authors"] = explode(",", env('GDM_AUTHORS'));
$meta_infos["results"] = count($records);
$meta_infos["self"] = env('GDM_PROTOCOL') . '://' . env('GDM_URL') . (env('GDM_PORT')==80 ? '' : ':' . env('GDM_PORT')) . $_SERVER['REQUEST_URI'];
$meta_infos["filter"] = $filter;
$meta_infos["offset"] = intval($pagination["offset"]);
$meta_infos["limit"] = intval($pagination["limit"]);
$meta_infos["sort"] = ["id"];
$meta_infos["orderBy"] = ["id"];
$meta_infos["callDate"] = date("Y-m-d\Th:m:sO");
$meta_infos["response_time"] = $milliseconds;
$meta_infos["responseTime"] = $milliseconds;
$meta_infos["resultLanguage"] = "EN_en";
$meta_infos["contentLicenses"] = explode(',', env('GDM_CONTENT_LICENSES'));
$meta_infos["message"] = env('GDM_TITLE') . ": CAP_NAME filtered by: " . $humanReadableFilter;
......@@ -736,7 +743,7 @@ class CAP_NAMEController extends Controller {
$result["meta"] = $meta_infos;
$result["data"] = $collection;
$json = json_encode($result, JSON_FORCE_OBJECT);
$json = json_encode($result);
return response($json)->header('Content-Type', 'application/json');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment