Skip to content

Commit

Permalink
Merge pull request #147 from ConductionNL/master
Browse files Browse the repository at this point in the history
Main to dev
  • Loading branch information
remko48 authored Dec 3, 2024
2 parents 52bd649 + a10d8b9 commit c9e37b3
Show file tree
Hide file tree
Showing 38 changed files with 2,042 additions and 49 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Create a [bug report](https://github.com/OpenCatalogi/.github/issues/new/choose)
Create a [feature request](https://github.com/OpenCatalogi/.github/issues/new/choose)
]]></description>
<version>0.6.42</version>
<version>0.6.48</version>
<licence>agpl</licence>
<author mail="[email protected]" homepage="https://www.conduction.nl/">Conduction</author>
<author mail="[email protected]" homepage="https://acato.nl/">Acato</author>
Expand Down
5 changes: 4 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'publications' => ['url' => '/api/publications'],
'organizations' => ['url' => '/api/organizations'],
'themes' => ['url' => '/api/themes'],
'pages' => ['url' => '/api/pages'],
'attachments' => ['url' => '/api/attachments'],
'catalogi' => ['url' => '/api/catalogi'],
'listings' => ['url' => '/api/listings'],
Expand Down Expand Up @@ -37,6 +38,8 @@
['name' => 'search#publication', 'url' => '/api/search/publications/{publicationId}', 'verb' => 'GET', 'requirements' => ['publicationId' => '[^/]+']],
['name' => 'search#attachments', 'url' => '/api/search/publications/{publicationId}/attachments', 'verb' => 'GET', 'requirements' => ['publicationId' => '[^/]+']],
['name' => 'search#themes', 'url' => '/api/search/themes', 'verb' => 'GET'],
['name' => 'search#theme', 'url' => '/api/search/themes/{themeId}', 'verb' => 'GET', 'requirements' => ['themeId' => '\d+']]
['name' => 'search#theme', 'url' => '/api/search/themes/{themeId}', 'verb' => 'GET', 'requirements' => ['themeId' => '\d+']],
['name' => 'search#pages', 'url' => '/api/public/pages', 'verb' => 'GET'],
['name' => 'search#page', 'url' => '/api/public/pages/{pageSlug}', 'verb' => 'GET', 'requirements' => ['pageId' => '.+']]
]
];
27 changes: 10 additions & 17 deletions lib/Controller/DirectoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use OCA\OpenCatalogi\Db\ListingMapper;
use OCA\OpenCatalogi\Service\DirectoryService;
use OCA\OpenCatalogi\Service\ObjectService;
use OCA\OpenCatalogi\Exception\DirectoryUrlException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
Expand Down Expand Up @@ -75,25 +76,17 @@ public function update(): JSONResponse
{
// Get the URL from the request parameters
$url = $this->request->getParam('directory');

// http://directory.opencatalogi.nl
// Check if the URL parameter is provided
if (empty($url) === true) {
return new JSONResponse(['error' => 'directory parameter is required'], 400);
}

// Check if URL contains 'local' and throw exception if it does
if (str_contains(strtolower($url), 'local')) {
return new JSONResponse(['error' => 'Local URLs are not allowed'], 400);
}

// Validate the URL
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return new JSONResponse(['error' => 'Invalid URL provided'], 400);
}

// Sync the external directory with the provided URL
$data = $this->directoryService->syncExternalDirectory($url);
try {
$data = $this->directoryService->syncExternalDirectory($url);
} catch (DirectoryUrlException $exception) {
if($exception->getMessage() === 'URL is required') {
$exception->setMessage('Property "directory" is required');
}

return new JSONResponse(data: ['message' => $exception->getMessage()], statusCode: 400);
}

// Return JSON response with the sync result
return new JSONResponse($data);
Expand Down
16 changes: 10 additions & 6 deletions lib/Controller/ListingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use OCA\OpenCatalogi\Db\ListingMapper;
use OCA\OpenCatalogi\Service\ObjectService;
use OCA\OpenCatalogi\Service\DirectoryService;
use OCA\OpenCatalogi\Exception\DirectoryUrlException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
Expand Down Expand Up @@ -186,13 +187,16 @@ public function add(): JSONResponse
// Get the URL parameter from the request
$url = $this->request->getParam('url');

// Check if the URL parameter is provided
if (empty($url) === true) {
return new JSONResponse(['error' => 'URL parameter is required'], 400);
}

// Add the new listing using the provided URL
$result = $this->directoryService->syncExternalDirectory($url);
try {
$result = $this->directoryService->syncExternalDirectory($url);
} catch (DirectoryUrlException $exception) {
if($exception->getMessage() === 'URL is required') {
$exception->setMessage('Property "url" is required');
}

return new JSONResponse(data: ['message' => $exception->getMessage()], statusCode: 400);
}

// Return the result as a JSON response
return new JSONResponse(['success' => $result]);
Expand Down
145 changes: 145 additions & 0 deletions lib/Controller/PagesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace OCA\OpenCatalogi\Controller;

use GuzzleHttp\Exception\GuzzleException;
use OCA\OpenCatalogi\Db\PageMapper;
use OCA\OpenCatalogi\Service\ObjectService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;

/**
* Class PagesController
*
* This controller handles CRUD operations for pages in the OpenCatalogi app.
*/
class PagesController extends Controller
{
/**
* Constructor for PagesController
*
* @param string $appName The name of the app
* @param IRequest $request The request object
* @param PageMapper $pageMapper The page mapper for database operations
* @param IAppConfig $config The app configuration
* @param ObjectService $objectService The service for handling object operations
*/
public function __construct
(
$appName,
IRequest $request,
private readonly PageMapper $pageMapper,
private readonly IAppConfig $config,
private readonly ObjectService $objectService
)
{
parent::__construct($appName, $request);
}

/**
* Retrieve a list of pages based on provided filters and parameters.
*
* @return JSONResponse JSON response containing the list of pages and total count
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index(): JSONResponse
{
// Retrieve all request parameters
$requestParams = $this->request->getParams();

// Fetch page objects based on filters and order
$data = $this->objectService->getResultArrayForRequest('page', $requestParams);

// Return JSON response
return new JSONResponse($data);
}

/**
* Retrieve a specific page by its ID.
*
* @param string|int $id The ID of the page to retrieve
* @return JSONResponse JSON response containing the requested page
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function show(string|int $id): JSONResponse
{
// Fetch the page object by its ID
$object = $this->objectService->getObject('page', $id);

// Return the page as a JSON response
return new JSONResponse($object);
}

/**
* Create a new page.
*
* @return JSONResponse The response containing the created page object.
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function create(): JSONResponse
{
// Get all parameters from the request
$data = $this->request->getParams();

// Remove the 'id' field if it exists, as we're creating a new object
unset($data['id']);

// Save the new page object
$object = $this->objectService->saveObject('page', $data);

// Return the created object as a JSON response
return new JSONResponse($object);
}

/**
* Update an existing page.
*
* @param string|int $id The ID of the page to update.
* @return JSONResponse The response containing the updated page object.
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function update(string|int $id): JSONResponse
{
// Get all parameters from the request
$data = $this->request->getParams();

// Ensure the ID in the data matches the ID in the URL
$data['id'] = $id;

// Save the updated page object
$object = $this->objectService->saveObject('page', $data);

// Return the updated object as a JSON response
return new JSONResponse($object);
}

/**
* Delete a page.
*
* @param string|int $id The ID of the page to delete.
* @return JSONResponse The response indicating the result of the deletion.
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function destroy(string|int $id): JSONResponse
{
// Delete the page object
$result = $this->objectService->deleteObject('page', $id);

// Return the result as a JSON response
return new JSONResponse(['success' => $result], $result === true ? '200' : '404');
}
}
69 changes: 69 additions & 0 deletions lib/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,73 @@ public function theme(string|int $themeId): JSONResponse
return new JSONResponse($object);
}

/**
* Return all pages.
*
* @CORS
* @PublicPage
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse The Response containing all pages.
*/
public function pages(): JSONResponse
{
// Get all page objects with request parameters
$objects = $this->objectService->getResultArrayForRequest(objectType: 'page', requestParams: $this->request->getParams());

// Format dates for each result
$formattedResults = array_map(function($object) {
// Format created_at if it exists
if (isset($object['created_at'])) {
$created = new \DateTime($object['created_at']);
$object['created_at'] = $created->format('Y-m-d\TH:i:s.u\Z');
}
// Format updated_at if it exists
if (isset($object['updated_at'])) {
$updated = new \DateTime($object['updated_at']);
$object['updated_at'] = $updated->format('Y-m-d\TH:i:s.u\Z');
}
return $object;
}, $objects['results']);

// Prepare the response data with formatted dates
$data = [
'data' => $formattedResults
];

return new JSONResponse($data);
}

/**
* Return a specific page by slug.
*
* @CORS
* @PublicPage
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $pageSlug The slug of the page
* @return JSONResponse The Response containing the requested page
* @throws GuzzleException
*/
public function page(string $pageSlug): JSONResponse
{
// Get the page object by slug
$object = $this->objectService->getObject('page', $pageSlug);

// Format the date fields to match required format
if (isset($object['created_at'])) {
$created = new \DateTime($object['created_at']);
$object['created_at'] = $created->format('Y-m-d\TH:i:s.u\Z');
}
if (isset($object['updated_at'])) {
$updated = new \DateTime($object['updated_at']);
$object['updated_at'] = $updated->format('Y-m-d\TH:i:s.u\Z');
}

return new JSONResponse($object);
}


}
7 changes: 5 additions & 2 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function index(): JSONResponse
{
// Initialize the data array
$data = [];
$data['objectTypes'] = ['attachment', 'catalog', 'listing', 'publicationtype', 'organization', 'publication', 'theme'];
$data['objectTypes'] = ['attachment', 'catalog', 'listing', 'publicationtype', 'organization', 'publication', 'theme', 'page'];
$data['openRegisters'] = false;
$data['availableRegisters'] = [];

Expand Down Expand Up @@ -78,7 +78,10 @@ public function index(): JSONResponse
'publication_register' => '',
'theme_source' => 'internal',
'theme_schema' => '',
'theme_register' => ''
'theme_register' => '',
'page_source' => 'internal',
'page_schema' => '',
'page_register' => '',
];

// Get the current values for the object types from the configuration
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/AttachmentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function createFromArray(array $object): Attachment
* @throws DoesNotExistException If the entity is not found
* @throws MultipleObjectsReturnedException|\OCP\DB\Exception If multiple entities are found
*/
public function updateFromArray(int $id, array $object, bool $updateVersion = true): Attachment
public function updateFromArray(int $id, array $object, bool $updateVersion = true, bool $patch = false): Attachment
{
$attachment = $this->find($id);
// Fallback to create if the attachment does not exist
Expand Down
4 changes: 2 additions & 2 deletions lib/Db/CatalogMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public function createFromArray(array $object): Catalog
*
* @return Catalog The updated Catalog entity
*/
public function updateFromArray(int $id, array $object, bool $updateVersion = true): Catalog
public function updateFromArray(int $id, array $object, bool $updateVersion = true, bool $patch = false): Catalog
{
$catalog = $this->find($id);
$catalog = $this->find($id);
// Fallback to create if the catalog does not exist
if ($catalog === null) {
$object['uuid'] = $id;
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/ListingMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function createFromArray(array $object): Listing
* @return Listing The updated Listing entity
* @throws Exception
*/
public function updateFromArray(int|string $id, array $object, bool $updateVersion = true): Listing
public function updateFromArray(int|string $id, array $object, bool $updateVersion = true, bool $patch = false): Listing
{
$listing = $this->find($id);
// Fallback to create if the listing does not exist
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/OrganizationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function createFromArray(array $object): Organization
*
* @return Organization The updated Organization entity
*/
public function updateFromArray(int $id, array $object, bool $updateVersion = true): Organization
public function updateFromArray(int $id, array $object, bool $updateVersion = true, bool $patch = false): Organization
{
$organization = $this->find($id);
// Fallback to create if the organization does not exist
Expand Down
Loading

0 comments on commit c9e37b3

Please sign in to comment.