Skip to content

Commit

Permalink
operationalise search
Browse files Browse the repository at this point in the history
  • Loading branch information
rjzondervan committed Oct 21, 2024
1 parent 95a68df commit 74f115a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,16 @@ public function getObjects(
?int $limit = null,
?int $offset = null,
?array $filters = [],
?array $searchConditions = [],
?array $searchParams = [],
?array $sort = [],
?array $extend = []
?array $extend = [],
?string $search = null
): array
{

// Get the appropriate mapper for the object type
$mapper = $this->getMapper($objectType);
// Use the mapper to find and return the objects based on the provided parameters
$objects = $mapper->findAll($limit, $offset, $filters, sort: $sort);
$objects = $mapper->findAll($limit, $offset, $filters, sort: $sort, search: $search);

// Convert entity objects to arrays using jsonSerialize
$objects = array_map(function($object) {
Expand Down Expand Up @@ -349,11 +348,11 @@ public function getOpenRegisters(): ?\OCA\OpenRegister\Service\ObjectService
return null;
}

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

return 0;
Expand All @@ -376,6 +375,7 @@ public function getResultArrayForRequest(string $objectType, array $requestParam
$order = $requestParams['order'] ?? $requestParams['_order'] ?? [];
$extend = $requestParams['extend'] ?? $requestParams['_extend'] ?? null;
$page = $requestParams['page'] ?? $requestParams['_page'] ?? null;
$search = $requestParams['_search'] ?? null;

if ($page !== null && isset($limit)) {
$offset = $limit * ($page - 1);
Expand All @@ -393,7 +393,7 @@ public function getResultArrayForRequest(string $objectType, array $requestParam
// Remove unnecessary parameters from filters
$filters = $requestParams;
unset($filters['_route']); // TODO: Investigate why this is here and if it's needed
unset($filters['_extend'], $filters['_limit'], $filters['_offset'], $filters['_order'], $filters['_page']);
unset($filters['_extend'], $filters['_limit'], $filters['_offset'], $filters['_order'], $filters['_page'], $filters['_search']);
unset($filters['extend'], $filters['limit'], $filters['offset'], $filters['order'], $filters['page']);

// Fetch objects based on filters and order
Expand All @@ -403,15 +403,16 @@ public function getResultArrayForRequest(string $objectType, array $requestParam
offset: $offset,
filters: $filters,
sort: $order,
extend: $extend
extend: $extend,
search: $search
);
$facets = $this->getFacets($objectType, $filters);

// Prepare response data
return [
'results' => $objects,
'facets' => $facets,
'total' => $this->getCount(objectType: $objectType, filters: $filters),
'total' => $this->getCount(objectType: $objectType, filters: $filters, search: $search),
];
}

Expand Down

0 comments on commit 74f115a

Please sign in to comment.