Skip to content

Commit

Permalink
Merge pull request #124 from ConductionNL/feature/AXCVDWOF-24/cleanup
Browse files Browse the repository at this point in the history
Bring the mappers in line with the OReg ObjectService
  • Loading branch information
rjzondervan authored Oct 24, 2024
2 parents f52c60e + bc04bcc commit d6ab809
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 87 deletions.
2 changes: 1 addition & 1 deletion lib/Db/AttachmentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
16 changes: 7 additions & 9 deletions lib/Db/CatalogMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}

Expand Down
14 changes: 3 additions & 11 deletions lib/Db/ListingMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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();

Expand All @@ -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);
}

/**
Expand Down
10 changes: 1 addition & 9 deletions lib/Db/OrganizationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}

Expand Down
30 changes: 1 addition & 29 deletions lib/Db/PublicationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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) {
Expand Down
21 changes: 3 additions & 18 deletions lib/Db/PublicationTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

Expand Down
16 changes: 7 additions & 9 deletions lib/Db/ThemeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}

Expand Down
8 changes: 7 additions & 1 deletion lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d6ab809

Please sign in to comment.