Skip to content

Commit

Permalink
Fixes for updating known Listings after Catalog is added/updated
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoLouwerse committed Oct 15, 2024
1 parent e847d8d commit 31bcf0a
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 45 deletions.
79 changes: 50 additions & 29 deletions lib/Controller/CatalogiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

namespace OCA\OpenCatalogi\Controller;

use GuzzleHttp\Exception\GuzzleException;
use OCA\OpenCatalogi\Db\CatalogMapper;
use OCA\OpenCatalogi\Service\DirectoryService;
use OCA\OpenCatalogi\Service\ObjectService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
* Class CatalogiController
Expand All @@ -27,13 +30,15 @@ class CatalogiController extends Controller
* @param IAppConfig $config The app configuration
* @param CatalogMapper $catalogMapper The catalog mapper
* @param ObjectService $objectService The object service
* @param DirectoryService $directoryService The directory service
*/
public function __construct(
$appName,
IRequest $request,
private readonly IAppConfig $config,
private readonly CatalogMapper $catalogMapper,
private readonly ObjectService $objectService
private readonly ObjectService $objectService,
private readonly DirectoryService $directoryService
)
{
parent::__construct($appName, $request);
Expand All @@ -43,7 +48,9 @@ public function __construct(
* Retrieve a list of catalogs based on provided filters and parameters.
*
* @param ObjectService $objectService Service to handle object operations
*
* @return JSONResponse JSON response containing the list of catalogs and total count
* @throws DoesNotExistException|MultipleObjectsReturnedException|ContainerExceptionInterface|NotFoundExceptionInterface
*
* @NoAdminRequired
* @NoCSRFRequired
Expand All @@ -55,7 +62,7 @@ public function index(ObjectService $objectService): JSONResponse

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

// Return JSON response
return new JSONResponse($data);
}
Expand All @@ -65,7 +72,9 @@ public function index(ObjectService $objectService): JSONResponse
*
* @param string|int $id The ID of the catalog to retrieve
* @param ObjectService $objectService Service to handle object operations
*
* @return JSONResponse JSON response containing the requested catalog
* @throws DoesNotExistException|MultipleObjectsReturnedException|ContainerExceptionInterface|NotFoundExceptionInterface
*
* @NoAdminRequired
* @NoCSRFRequired
Expand All @@ -79,51 +88,61 @@ public function show(string|int $id, ObjectService $objectService): JSONResponse
return new JSONResponse($object);
}

/**
* Create a new catalog.
*
* @param ObjectService $objectService The service to handle object operations.
* @return JSONResponse The response containing the created catalog object.
*
* @NoAdminRequired
* @NoCSRFRequired
*/
/**
* Create a new catalog.
*
* @param ObjectService $objectService The service to handle object operations.
*
* @return JSONResponse The response containing the created catalog object.
* @throws DoesNotExistException|MultipleObjectsReturnedException|ContainerExceptionInterface|NotFoundExceptionInterface
* @throws GuzzleException
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function create(ObjectService $objectService): 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 catalog object
$object = $this->objectService->saveObject('catalog', $data);


$this->directoryService->updateAllExternalDirectories();

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

/**
* Update an existing catalog.
*
* @param string|int $id The ID of the catalog to update.
* @param ObjectService $objectService The service to handle object operations.
* @return JSONResponse The response containing the updated catalog object.
*
* @NoAdminRequired
* @NoCSRFRequired
*/
/**
* Update an existing catalog.
*
* @param string|int $id The ID of the catalog to update.
* @param ObjectService $objectService The service to handle object operations.
*
* @return JSONResponse The response containing the updated catalog object.
* @throws DoesNotExistException|MultipleObjectsReturnedException|ContainerExceptionInterface|NotFoundExceptionInterface
* @throws GuzzleException
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function update(string|int $id, ObjectService $objectService): 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 catalog object
$object = $this->objectService->saveObject('catalog', $data);


$this->directoryService->updateAllExternalDirectories();

// Return the updated object as a JSON response
return new JSONResponse($object);
}
Expand All @@ -133,16 +152,18 @@ public function update(string|int $id, ObjectService $objectService): JSONRespon
*
* @param string|int $id The ID of the catalog to delete.
* @param ObjectService $objectService The service to handle object operations.
*
* @return JSONResponse The response indicating the result of the deletion.
*
* @throws ContainerExceptionInterface|NotFoundExceptionInterface|\OCP\DB\Exception
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function destroy(string|int $id, ObjectService $objectService): JSONResponse
{
// Delete the catalog object
$result = $this->objectService->deleteObject('catalog', $id);

// Return the result as a JSON response
return new JSONResponse(['success' => $result]);
}
Expand Down
17 changes: 12 additions & 5 deletions lib/Controller/DirectoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
use OCA\OpenCatalogi\Service\DirectoryService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
* Controller for handling directory-related operations
Expand Down Expand Up @@ -40,9 +43,11 @@ public function __construct(
/**
* Retrieve all directories
*
* @return JSONResponse The JSON response containing all directories
* @throws DoesNotExistException|MultipleObjectsReturnedException|ContainerExceptionInterface|NotFoundExceptionInterface
*
* @PublicPage
* @NoCSRFRequired
* @return JSONResponse The JSON response containing all directories
*/
public function index(): JSONResponse
{
Expand All @@ -56,19 +61,21 @@ public function index(): JSONResponse
/**
* Update an external directory
*
* @PublicPage
* @NoCSRFRequired
* @return JSONResponse The JSON response containing the update result
* @throws DoesNotExistException|MultipleObjectsReturnedException|ContainerExceptionInterface|NotFoundExceptionInterface
* @throws GuzzleException
*
* @PublicPage
* @NoCSRFRequired
*/
public function update(): JSONResponse
{
// Get the URL from the request parameters
$url = $this->request->getParam('directory');
$url = $this->request->getParam('url');

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

// Sync the external directory with the provided URL
Expand Down
Loading

0 comments on commit 31bcf0a

Please sign in to comment.