Skip to content

Commit

Permalink
WIP: listing.organisation
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrands02 committed Aug 9, 2024
1 parent db82028 commit fba074a
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 20 deletions.
3 changes: 3 additions & 0 deletions lib/Db/Listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Listing extends Entity implements JsonSerializable
protected ?DateTime $lastSync = null;
protected ?bool $default = false;
protected ?bool $available = false;
protected ?string $organisation = null;

public function __construct() {
$this->addType(fieldName: 'title', type: 'string');
Expand All @@ -34,6 +35,7 @@ public function __construct() {
$this->addType(fieldName: 'lastSync', type: 'datetime');
$this->addType(fieldName: 'default', type: 'boolean');
$this->addType(fieldName: 'available', type: 'boolean');
$this->addType(fieldName: 'organisation', type: 'string');
}

public function getJsonFields(): array
Expand Down Expand Up @@ -81,6 +83,7 @@ public function jsonSerialize(): array
'lastSync' => $this->lastSync->format('c'),
'default' => $this->default,
'available' => $this->available,
'organisation'=> $this->organisation,
];

$jsonFields = $this->getJsonFields();
Expand Down
109 changes: 90 additions & 19 deletions lib/Db/ListingMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OCA\OpenCatalogi\Db;

use OCA\OpenCatalogi\Db\Listing;
use OCA\OpenCatalogi\Db\Organisation;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
Expand All @@ -28,34 +29,104 @@ public function find(int $id): Listing
return $this->findEntity(query: $qb);
}

public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
{
$qb = $this->db->getQueryBuilder();
/**
* CUSTOM FOR JOINS
*/
protected function mapRowToEntityCustom(array $row): Entity {
unset($row['DOCTRINE_ROWNUM']); // remove doctrine/dbal helper column

// Map the Organisation fields to a sub-array
$organisationData = [
'id' => $row['organisation_id'] ?? null,
'title' => $row['organisation_title'] ?? null,
'summary' => $row['organisation_summary'] ?? null,
'description' => $row['organisation_description'] ?? null,
'image' => $row['organisation_image'] ?? null,
'oin' => $row['organisation_oin'] ?? null,
'tooi' => $row['organisation_tooi'] ?? null,
'rsin' => $row['organisation_rsin'] ?? null,
'pki' => $row['organisation_pki'] ?? null,
];

$organisationIsEmpty = true;
foreach ($organisationData as $key => $value) {
if ($value !== null) {
$organisationIsEmpty = false;
break;
}
}

$qb->select('*')
->from('listings')
->setMaxResults($limit)
->setFirstResult($offset);

foreach($filters as $filter => $value) {
if ($value === 'IS NOT NULL') {
$qb->andWhere($qb->expr()->isNotNull($filter));
} elseif ($value === 'IS NULL') {
$qb->andWhere($qb->expr()->isNull($filter));
} else {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
$row['organisation'] = $organisationIsEmpty === true ? null : Organisation::fromRow($organisationData);

return \call_user_func($this->entityClass .'::fromRow', $row);
}

/**
* Runs a sql query and returns an array of entities CUSTOM FOR JOINS
*
* @param IQueryBuilder $query
* @return Entity[] all fetched entities
* @psalm-return T[] all fetched entities
* @throws Exception
* @since 14.0.0
*/
protected function findEntitiesCustom(IQueryBuilder $query): array {
$result = $query->executeQuery();
try {
$entities = [];
while ($row = $result->fetch()) {
$entities[] = $this->mapRowToEntityCustom($row);
}
}
return $entities;
} finally {
$result->closeCursor();
}
}

public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
{
$qb = $this->db->getQueryBuilder();

$qb->select(
'l.*',
'o.id AS organisation_id',
'o.title AS organisation_title',
'o.summary AS organisation_summary',
'o.description AS organisation_description',
'o.image AS organisation_image',
'o.oin AS organisation_oin',
'o.tooi AS organisation_tooi',
'o.rsin AS organisation_rsin',
'o.pki AS organisation_pki'
)
->from('listings', 'l')
->leftJoin('l', 'organizations', 'o', 'l.organisation = o.id')
->setMaxResults($limit)
->setFirstResult($offset);


// Apply filters
foreach ($filters as $filter => $value) {
if ($value === 'IS NOT NULL') {
$qb->andWhere($qb->expr()->isNotNull($filter));
} elseif ($value === 'IS NULL') {
$qb->andWhere($qb->expr()->isNull($filter));
} else {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
}
}

// Apply search conditions
if (!empty($searchConditions)) {
$qb->andWhere('(' . implode(' OR ', $searchConditions) . ')');
foreach ($searchParams as $param => $value) {
$qb->setParameter($param, $value);
}
}

return $this->findEntities(query: $qb);
}

// Use the existing findEntities method to fetch and map the results
return $this->findEntitiesCustom($qb);
}

public function createFromArray(array $object): Listing
{
Expand Down
72 changes: 72 additions & 0 deletions lib/Migration/Version6Date20240809120147.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\OpenCatalogi\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
* FIXME Auto-generated migration step: Please modify to your needs!
*/
class Version6Date20240809120147 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/**
* @var ISchemaWrapper $schema
*/
$schema = $schemaClosure();

if($schema->hasTable(tableName: 'listings') === true) {
$table = $schema->getTable(tableName: 'listings');

if($table->hasColumn(name: 'organization') === true) {
$column = $table->dropColumn('organization');
}
if($table->hasColumn(name: 'organisation') === false) {
$table->addColumn(
name: 'organisation',
typeName: Types::STRING,
options: [
'notNull' => false,
'default' => null
]);
$output->info('organisation should be added to listing');
}

}

return $schema;
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}
}
4 changes: 3 additions & 1 deletion lib/Service/DirectoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ public function listCatalog (array $catalog): array

$listing = $this->getDirectoryEntry(catalogId: $catalogId);

$listing['title'] = $catalog['title'];
$listing['title'] = $catalog['title'];
$listing['organisation'] = $catalog['organisation'];
$listing['metaData'] = $catalog['metaData'];

if($this->config->hasKey($this->appName, 'mongoStorage') === false
|| $this->config->getValueString($this->appName, 'mongoStorage') !== '1'
Expand Down
1 change: 1 addition & 0 deletions src/views/directory/DirectoryList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { navigationStore, directoryStore } from '../../store/store.js'
:key="`${listing}${i}`"
:name="listing.name ?? listing.title"
:active="directoryStore.listingItem?.id === listing?.id"
:details="listing?.organisation?.title || 'Geen organisatie'"
@click="directoryStore.setListingItem(listing)">
<template #icon>
<LayersOutline :class="directoryStore.listingItem?.id === listing?.id && 'selectedIcon'"
Expand Down

0 comments on commit fba074a

Please sign in to comment.