Skip to content

Commit

Permalink
Merge branch 'feature/DIMOC-208/fileupload-fe' of https://github.com/…
Browse files Browse the repository at this point in the history
…ConductionNL/OpenCatalogApp into feature/DIMOC-208/fileupload-fe
  • Loading branch information
remko48 committed Aug 7, 2024
2 parents 631d158 + f099f35 commit d9cd2d2
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 65 deletions.
3 changes: 2 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
['name' => 'directory#page', 'url' => '/directory', 'verb' => 'GET'],
['name' => 'directory#add', 'url' => '/api/directory/add', 'verb' => 'POST'],
['name' => 'configuration#index', 'url' => '/configuration', 'verb' => 'GET'],
['name' => 'configuration#create', 'url' => '/configuration', 'verb' => 'POST']
['name' => 'configuration#create', 'url' => '/configuration', 'verb' => 'POST'],
['name' => 'search#preflighted_cors', 'url' => '/api/{path}', 'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']]
],
];
47 changes: 5 additions & 42 deletions lib/Controller/DirectoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,6 @@ public function page(?string $getParameter)
}


/**
* @PublicPage
* @NoCSRFRequired
*/
public function add(?string $url, DirectoryService $directoryService): JSONResponse
{
$directories = [];
$directoryService->registerToExternalDirectory(url: $url, externalDirectories: $directories);

return new JSONResponse(['listingsAdded' => $directories]);
}


/**
* @PublicPage
* @NoCSRFRequired
Expand Down Expand Up @@ -143,39 +130,15 @@ public function show(string|int $id, ObjectService $objectService, DirectoryServ


/**
* @NoAdminRequired
* @PublicPage
* @NoCSRFRequired
*/
public function create(ObjectService $objectService, DirectoryService $directoryService): JSONResponse
public function create(string $directory, DirectoryService $directoryService): JSONResponse
{
$directories = [];
$directoryService->registerToExternalDirectory(url: $directory, externalDirectories: $directories);

$data = $this->request->getParams();

foreach($data as $key => $value) {
if(str_starts_with($key, '_')) {
unset($data[$key]);
}
}

if($this->config->hasKey($this->appName, 'mongoStorage') === false
|| $this->config->getValueString($this->appName, 'mongoStorage') !== '1'
) {
return new JSONResponse($this->listingMapper->createFromArray(object: $data));
}

$dbConfig['base_uri'] = $this->config->getValueString(app: $this->appName, key: 'mongodbLocation');
$dbConfig['headers']['api-key'] = $this->config->getValueString(app: $this->appName, key: 'mongodbKey');
$dbConfig['mongodbCluster'] = $this->config->getValueString(app: $this->appName, key: 'mongodbCluster');

$data['_schema'] = 'directory';

$returnData = $objectService->saveObject(
data: $data,
config: $dbConfig
);

// get post from requests
return new JSONResponse($returnData);
return new JSONResponse(['results' => $directories]);
}

/**
Expand Down
59 changes: 42 additions & 17 deletions lib/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,60 @@
use OCA\OpenCatalogi\Service\ElasticSearchService;
use OCA\OpenCatalogi\Db\PublicationMapper;
use OCA\OpenCatalogi\Service\SearchService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;

class SearchController extends Controller
{
const TEST_ARRAY = [
"d9e1467e-fc55-44c8-bf5c-bf139ac10eda" => [
"id" => "d9e1467e-fc55-44c8-bf5c-bf139ac10eda",
"name" => "Search one",
"summary" => "summary for one"
],
"e9d0131b-06c4-4d20-aa17-3b2aaad186d7" => [
"id" => "e9d0131b-06c4-4d20-aa17-3b2aaad186d7",
"name" => "Search two",
"summary" => "summary for two"
]
];

public function __construct(
$appName,
IRequest $request,
private readonly PublicationMapper $publicationMapper,
private readonly IAppConfig $config)
{
parent::__construct($appName, $request);
private readonly IAppConfig $config,
$corsMethods = 'PUT, POST, GET, DELETE, PATCH',
$corsAllowedHeaders = 'Authorization, Content-Type, Accept',
$corsMaxAge = 1728000
) {
parent::__construct($appName, $request);
$this->corsMethods = $corsMethods;
$this->corsAllowedHeaders = $corsAllowedHeaders;
$this->corsMaxAge = $corsMaxAge;
}

/**
* This method implements a preflighted cors response for you that you can
* link to for the options request
*
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
* @since 7.0.0
*/
#[NoCSRFRequired]
#[PublicPage]
public function preflightedCors() {
if (isset($this->request->server['HTTP_ORIGIN'])) {
$origin = $this->request->server['HTTP_ORIGIN'];
} else {
$origin = '*';
}

$response = new Response();
$response->addHeader('Access-Control-Allow-Origin', $origin);
$response->addHeader('Access-Control-Allow-Methods', $this->corsMethods);
$response->addHeader('Access-Control-Max-Age', (string)$this->corsMaxAge);
$response->addHeader('Access-Control-Allow-Headers', $this->corsAllowedHeaders);
$response->addHeader('Access-Control-Allow-Credentials', 'false');
return $response;
}

/**
* @NoAdminRequired
* @NoCSRFRequired
Expand All @@ -51,10 +75,11 @@ public function page(?string $getParameter)
[]
);
}

/**
* @PublicPage
* @NoCSRFRequired
* @CORS
*/
public function index(SearchService $searchService): JSONResponse
{
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/Listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function jsonSerialize(): array
'metadata' => $this->metadata,
'catalogId' => $this->catalogId,
'status' => $this->status,
'lastSync' => $this->lastSync,
'lastSync' => $this->lastSync->format('c'),
'default' => $this->default,
'available' => $this->available,
];
Expand Down
7 changes: 5 additions & 2 deletions lib/Service/DirectoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public function listDirectory(array $filters = [], int $limit = 30, int $offset
if ($this->config->hasKey($this->appName, 'mongoStorage') === false
|| $this->config->getValueString($this->appName, 'mongoStorage') !== '1'
) {
$filters['catalog_id'] = $filters['catalogId'];
unset($filters['catalogId']);

return $this->listingMapper->findAll(limit: $limit, offset: $offset, filters: $filters);
}
$filters['_schema'] = 'directory';
Expand Down Expand Up @@ -232,9 +235,9 @@ public function listCatalog (array $catalog): array
'mongodbCluster' => $this->config->getValueString($this->appName, 'mongodbCluster')
];

$data['_schema'] = 'catalog';
$listing['_schema'] = 'directory';

$returnData = $this->objectService->saveObject($data, $dbConfig);
$returnData = $this->objectService->saveObject($listing, $dbConfig);
return $catalog;
} catch (\Exception $e) {
$catalog['listed'] = false;
Expand Down
2 changes: 2 additions & 0 deletions lib/Service/ElasticSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ public function searchObject(array $filters, array $config): array
$return = ['results' => array_map(callback: [$this, 'formatResults'], array: $result['hits']['hits'])];
if(isset($result['aggregations']) === true) {
$return['facets'] = array_map([$this, 'mapAggregationResults'], $result['aggregations']);
} else {
$return['facets'] = [];
}

return $return;
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function findObject(array $filters, array $config): array
associative: true
);

return ['document' => $result];
return $result['document'];
}


Expand Down
6 changes: 5 additions & 1 deletion lib/Service/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Promise\Utils;
use OCP\IURLGenerator;
use Symfony\Component\Uid\Uuid;

class SearchService
Expand All @@ -18,6 +19,7 @@ class SearchService
public function __construct(
private readonly ElasticSearchService $elasticService,
private readonly DirectoryService $directoryService,
private readonly IURLGenerator $urlGenerator,
) {
$this->client = new Client();
}
Expand Down Expand Up @@ -98,12 +100,14 @@ public function search(array $parameters, array $elasticConfig, array $dbConfig,

$searchEndpoints = [];


$promises = [];
foreach($directory as $instance) {
if(
$instance['default'] === false
&& isset($parameters['.catalogi']) === true
|| isset($parameters['.catalogi']) === true
&& in_array($instance['catalogId'], $parameters['.catalogi']) === false
|| $instance['search'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute(routeName:"opencatalogi.directory.index"))
) {
continue;
}
Expand Down

0 comments on commit d9cd2d2

Please sign in to comment.