Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1132 adaptation of the api calls for the start page #1142

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/inc/apiv2/common/AbstractBaseAPI.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use DBA\AgentStat;
use DBA\Assignment;
use DBA\Chunk;
use DBA\ComparisonFilter;
use DBA\Config;
use DBA\ConfigSection;
use DBA\CrackerBinary;
Expand Down Expand Up @@ -919,22 +920,22 @@ protected function makeFilter(Request $request, array $features): array
switch($matches['operator']) {
case '':
case '__eq':
array_push($qFs, new QueryFilter($remappedKey, $val, '='));
$operator = '=';
break;
case '__ne':
array_push($qFs, new QueryFilter($remappedKey, $val, '!='));
$operator = '!=';
break;
case '__lt':
array_push($qFs, new QueryFilter($remappedKey, $val, '<'));
$operator = '<';
break;
case '__lte':
array_push($qFs, new QueryFilter($remappedKey, $val, '<='));
$operator = '<=';
break;
case '__gt':
array_push($qFs, new QueryFilter($remappedKey, $val, '>'));
$operator = '>';
break;
case '__gte':
array_push($qFs, new QueryFilter($remappedKey, $val, '>='));
$operator = '>=';
break;
case '__contains':
array_push($qFs, new LikeFilter($remappedKey, "%" . $val . "%"));
Expand All @@ -957,6 +958,14 @@ protected function makeFilter(Request $request, array $features): array
default:
assert(False, "Operator '" . $matches['operator'] . "' not implemented");
}

if ($operator) {
if (array_key_exists($val, $features)) {
array_push($qFs, new ComparisonFilter($remappedKey, $val, $operator));
} else {
array_push($qFs, new QueryFilter($remappedKey, $val, $operator));
}
}
}
return $qFs;
}
Expand Down Expand Up @@ -1257,7 +1266,7 @@ protected static function getOneResource(object $apiClass, object $object, Reque

//Meta response for helper functions that do not respond with resource records
protected static function getMetaResponse(array $meta, Request $request, Response $response, int $statusCode=200) {
$ret = self::createJsonResponse($meta=$meta);
$ret = self::createJsonResponse(meta: $meta);
$body = $response->getBody();
$body->write(self::ret2json($ret));

Expand Down
36 changes: 36 additions & 0 deletions src/inc/apiv2/common/AbstractModelAPI.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,41 @@ public function get(Request $request, Response $response, array $args): Response
return self::getManyResources($this, $request, $response);
}

/**
* API entry point for retrieving count information of data
*/
public function count(Request $request, Response $response, array $args): Response
{
$this->preCommon($request);

$factory = $this->getFactory();
$aliasedfeatures = $this->getAliasedFeatures();

/* Generate filters */
$qFs_Filter = $this->makeFilter($request, $aliasedfeatures);
$qFs_ACL = $this->getFilterACL();
$qFs = array_merge($qFs_ACL, $qFs_Filter);
if (count($qFs) > 0) {
$aFs[Factory::FILTER] = $qFs;
}

$count = $factory->countFilter($aFs);
$meta = ["count" => $count];

$include_total = $request->getQueryParams()['include_total'];
if ($include_total == "true") {
$meta["total_count"] = $factory->countFilter([]);
}

$ret = self::createJsonResponse(meta: $meta);

$body = $response->getBody();
$body->write($this->ret2json($ret));

return $response->withStatus(200)
->withHeader("Content-Type", 'application/vnd.api+json');
}

/**
* Get input field names valid for creation of object
*/
Expand Down Expand Up @@ -1106,6 +1141,7 @@ static public function register($app): void

if (in_array("GET", $available_methods)) {
$app->get($baseUri, $me . ':get')->setname($me . ':get');
$app->get($baseUri . "/count", $me . ':count')->setname($me . ':count');
}

foreach ($me::getToOneRelationships() as $name => $relationship) {
Expand Down
Loading