Skip to content

Commit

Permalink
Fix counts and attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
rjzondervan committed Oct 18, 2024
1 parent 64280d9 commit 84a2261
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
['name' => 'search#preflighted_cors', 'url' => '/api/search/{path}', 'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']],
['name' => 'search#index', 'url' => '/api/search', 'verb' => 'GET'],
['name' => 'search#publications', 'url' => '/api/search/publications', 'verb' => 'GET'],
['name' => 'search#publication', 'url' => '/api/search/publications/{publicationId}', 'verb' => 'GET', 'requirements' => ['publicationId' => '\d+']],
['name' => 'search#attachments', 'url' => '/api/search/publications/{publicationId}/attachments', 'verb' => 'GET', 'requirements' => ['publicationId' => '\d+']],
['name' => 'search#publication', 'url' => '/api/search/publications/{publicationId}', 'verb' => 'GET', 'requirements' => ['publicationId' => '[^/]+']],
['name' => 'search#attachments', 'url' => '/api/search/publications/{publicationId}/attachments', 'verb' => 'GET', 'requirements' => ['publicationId' => '[^/]+']],
['name' => 'search#themes', 'url' => '/api/search/themes', 'verb' => 'GET'],
['name' => 'search#theme', 'url' => '/api/search/themes/{themeId}', 'verb' => 'GET', 'requirements' => ['themeId' => '\d+']]
]
Expand Down
5 changes: 3 additions & 2 deletions lib/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ public function publications(): JSONResponse
// Prepare the response data
$data = [
'results' => array_values($filteredObjects), // Reset array keys
'total' => count($filteredObjects)
'facets' => $objects['facets'],
'total' => $objects['total'],
];

return new JSONResponse($data);
Expand Down Expand Up @@ -190,7 +191,7 @@ public function publication(string|int $publicationId): JSONResponse
public function attachments(string|int $publicationId): JSONResponse
{
// Get all attachment objects
$objects = $this->objectService->getObjects('attachment');
$objects = $this->objectService->getObject(objectType: 'publication', id: $publicationId, extend: ['attachments'])['attachments'];

// Prepare the response data
$data = [
Expand Down
14 changes: 11 additions & 3 deletions lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,16 @@ public function getOpenRegisters(): ?\OCA\OpenRegister\Service\ObjectService
return null;
}

private function getCount(string $objectType, array $filters = []): int
{
$mapper = $this->getMapper($objectType);
if($mapper instanceof \OCA\OpenRegister\Service\ObjectService === true) {
return $mapper->count(filters: $filters);
}

return 0;
}

/**
* Get a result array for a request based on the request and the object type.
*
Expand Down Expand Up @@ -389,8 +399,6 @@ public function getResultArrayForRequest(string $objectType, array $requestParam
limit: $limit,
offset: $offset,
filters: $filters,
searchConditions: null,
searchParams: null,
sort: $order,
extend: $extend
);
Expand All @@ -400,7 +408,7 @@ public function getResultArrayForRequest(string $objectType, array $requestParam
return [
'results' => $objects,
'facets' => $facets,
'total' => count($objects)
'total' => $this->getCount(objectType: $objectType, filters: $filters),
];
}

Expand Down

0 comments on commit 84a2261

Please sign in to comment.