From bc04bcc33fe27c91dd7e257ab5d16d638711a858 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Tue, 22 Oct 2024 16:17:42 +0200 Subject: [PATCH] Bring the mappers in line with the OReg ObjectService --- lib/Db/AttachmentMapper.php | 2 +- lib/Db/CatalogMapper.php | 16 +++++++--------- lib/Db/ListingMapper.php | 14 +++----------- lib/Db/OrganizationMapper.php | 10 +--------- lib/Db/PublicationMapper.php | 30 +----------------------------- lib/Db/PublicationTypeMapper.php | 21 +++------------------ lib/Db/ThemeMapper.php | 16 +++++++--------- lib/Service/ObjectService.php | 8 +++++++- 8 files changed, 30 insertions(+), 87 deletions(-) diff --git a/lib/Db/AttachmentMapper.php b/lib/Db/AttachmentMapper.php index c5ad71c2..ba1e090e 100644 --- a/lib/Db/AttachmentMapper.php +++ b/lib/Db/AttachmentMapper.php @@ -80,7 +80,7 @@ public function findMultiple(array $ids): array * @param int|null $offset Number of results to skip * @return array An array of all found Attachment entities */ - public function findAll($limit = null, $offset = null): array + public function findAll(int $limit = null, int $offset = null, array $filters = [], array $sort = [], ?string $search = null): array { $qb = $this->db->getQueryBuilder(); diff --git a/lib/Db/CatalogMapper.php b/lib/Db/CatalogMapper.php index 4e75f6ca..063f922f 100644 --- a/lib/Db/CatalogMapper.php +++ b/lib/Db/CatalogMapper.php @@ -79,7 +79,13 @@ public function findMultiple(array $ids): array * @param array|null $searchParams Array of search parameters * @return array An array of found Catalog entities */ - public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array + public function findAll( + ?int $limit = null, + ?int $offset = null, + array $filters = [], + array $sort = [], + ?string $search = null + ): array { $qb = $this->db->getQueryBuilder(); @@ -99,14 +105,6 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters } } - // Apply search conditions - if (empty($searchConditions) === false) { - $qb->andWhere('(' . implode(' OR ', $searchConditions) . ')'); - foreach ($searchParams as $param => $value) { - $qb->setParameter($param, $value); - } - } - return $this->findEntities(query: $qb); } diff --git a/lib/Db/ListingMapper.php b/lib/Db/ListingMapper.php index 4cef2ac6..a35a9f57 100644 --- a/lib/Db/ListingMapper.php +++ b/lib/Db/ListingMapper.php @@ -49,7 +49,7 @@ public function find($id): Listing $qb->expr()->eq('uuid', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR)) )); - return $this->findEntityCustom(query: $qb); + return $this->findEntity(query: $qb); } /** @@ -151,7 +151,7 @@ protected function findEntitiesCustom(IQueryBuilder $query): array { * @param array|null $searchParams Array of search parameters * @return array An array of found Listing entities */ - public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array + public function findAll(?int $limit = null, ?int $offset = null, array $filters = [], array $sort = [], ?string $search = null): array { $qb = $this->db->getQueryBuilder(); @@ -171,16 +171,8 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters } } - // Apply search conditions - if (empty($searchConditions) === false) { - $qb->andWhere('(' . implode(' OR ', $searchConditions) . ')'); - foreach ($searchParams as $param => $value) { - $qb->setParameter($param, $value); - } - } - // Use the existing findEntities method to fetch and map the results - return $this->findEntitiesCustom($qb); + return $this->findEntities($qb); } /** diff --git a/lib/Db/OrganizationMapper.php b/lib/Db/OrganizationMapper.php index e1711453..6e110c82 100644 --- a/lib/Db/OrganizationMapper.php +++ b/lib/Db/OrganizationMapper.php @@ -81,7 +81,7 @@ public function findMultiple(array $ids): array * @param array|null $searchParams Array of search parameters * @return array An array of all found Organization entities */ - public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array + public function findAll(?int $limit = null, ?int $offset = null, array $filters = [], array $sort = [], ?string $search = null): array { $qb = $this->db->getQueryBuilder(); @@ -101,14 +101,6 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters } } - // Apply search conditions - if (empty($searchConditions) === false) { - $qb->andWhere('(' . implode(' OR ', $searchConditions) . ')'); - foreach ($searchParams as $param => $value) { - $qb->setParameter($param, $value); - } - } - return $this->findEntities(query: $qb); } diff --git a/lib/Db/PublicationMapper.php b/lib/Db/PublicationMapper.php index 649821d7..2321fd71 100644 --- a/lib/Db/PublicationMapper.php +++ b/lib/Db/PublicationMapper.php @@ -170,7 +170,7 @@ public function count(?array $filters = [], ?array $searchConditions = [], ?arra * @param array|null $sort Associative array of sort fields and directions * @return array An array of found Publication entities */ - public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = [], ?array $sort = []): array + public function findAll(?int $limit = null, ?int $offset = null, array $filters = [], array $sort = [], ?string $search = null): array { $qb = $this->db->getQueryBuilder(); @@ -181,34 +181,6 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters $qb = $this->addFilters(queryBuilder: $qb, filters: $filters); - // Add search conditions - if (empty($searchConditions) === false) { - foreach ($searchConditions as $condition) { - $qb->andWhere($condition); - } - - // Bind all parameters at once using setParameters() - $paramBindings = []; - foreach ($searchParams as $param => $value) { - // Handle catalogi parameters explicitly as integers - if (strpos($param, ':catalogi_') === 0) { - $paramBindings[$param] = [$value, \PDO::PARAM_INT]; - } else { - // For all other parameters, bind normally - $paramBindings[$param] = $value; - } - } - - // Use setParameters to bind all at once - foreach ($paramBindings as $param => $binding) { - if (is_array($binding) === true) { - $qb->setParameter($param, $binding[0], $binding[1]); // Bind with type - } else { - $qb->setParameter($param, $binding); // Bind normally - } - } - } - // Add sorting if (empty($sort) === false) { foreach ($sort as $field => $direction) { diff --git a/lib/Db/PublicationTypeMapper.php b/lib/Db/PublicationTypeMapper.php index 11d77824..d77d0aa5 100644 --- a/lib/Db/PublicationTypeMapper.php +++ b/lib/Db/PublicationTypeMapper.php @@ -94,11 +94,9 @@ public function findMultiple(array $ids): array public function findAll( ?int $limit = null, ?int $offset = null, - ?array $filters = [], - ?array $searchConditions = [], - ?array $searchParams = [], - ?array $orderBy = [], - ?array $extend = [] + array $filters = [], + array $sort = [], + ?string $search = null ): array { $qb = $this->db->getQueryBuilder(); @@ -119,21 +117,8 @@ public function findAll( } } - // Apply search conditions - if (empty($searchConditions) === false) { - $qb->andWhere('(' . implode(' OR ', $searchConditions) . ')'); - foreach ($searchParams as $param => $value) { - $qb->setParameter($param, $value); - } - } - $entities = $this->findEntities(query: $qb); - // TODO: Implement extending functionality - if (!empty($extend)) { - // todo: implement extending - } - return $entities; } diff --git a/lib/Db/ThemeMapper.php b/lib/Db/ThemeMapper.php index aacd5cd7..b1b2c2d3 100644 --- a/lib/Db/ThemeMapper.php +++ b/lib/Db/ThemeMapper.php @@ -79,7 +79,13 @@ public function findMultiple(array $ids): array * @param array|null $searchParams Array of search parameters * @return array An array of found Theme entities */ - public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array + public function findAll( + ?int $limit = null, + ?int $offset = null, + array $filters = [], + array $sort = [], + ?string $search = null + ): array { $qb = $this->db->getQueryBuilder(); @@ -99,14 +105,6 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters } } - // Apply search conditions - if (empty($searchConditions) === false) { - $qb->andWhere('(' . implode(' OR ', $searchConditions) . ')'); - foreach ($searchParams as $param => $value) { - $qb->setParameter($param, $value); - } - } - return $this->findEntities(query: $qb); } diff --git a/lib/Service/ObjectService.php b/lib/Service/ObjectService.php index ab22230f..f0688c0b 100644 --- a/lib/Service/ObjectService.php +++ b/lib/Service/ObjectService.php @@ -173,7 +173,13 @@ public function getObjects( // 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, search: $search); + $objects = $mapper->findAll( + limit: $limit, + offset: $offset, + filters: $filters, + sort: $sort, + search: $search + ); // Convert entity objects to arrays using jsonSerialize $objects = array_map(function($object) {