Skip to content

Commit

Permalink
Small code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoLouwerse committed Nov 14, 2024
1 parent 53daa49 commit cc823cf
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 82 deletions.
170 changes: 94 additions & 76 deletions lib/Controller/MappingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use OCP\IAppConfig;
use OCP\IRequest;
use OCP\IURLGenerator;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class MappingsController extends Controller
{
Expand Down Expand Up @@ -126,17 +128,17 @@ public function create(): JSONResponse
return new JSONResponse($this->mappingMapper->createFromArray(object: $data));
}

/**
* Updates an existing mapping
*
* This method updates an existing mapping based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the mapping to update
* @return JSONResponse A JSON response containing the updated mapping details
*/
/**
* Updates an existing mapping
*
* This method updates an existing mapping based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id The ID of the mapping to update
* @return JSONResponse A JSON response containing the updated mapping details
*/
public function update(int $id): JSONResponse
{
$data = $this->request->getParams();
Expand All @@ -152,60 +154,66 @@ public function update(int $id): JSONResponse
return new JSONResponse($this->mappingMapper->updateFromArray(id: (int) $id, object: $data));
}

/**
* Deletes a mapping
*
* This method deletes a mapping based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the mapping to delete
* @return JSONResponse An empty JSON response
*/
/**
* Deletes a mapping
*
* This method deletes a mapping based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id The ID of the mapping to delete
* @return JSONResponse An empty JSON response
* @throws \OCP\DB\Exception
*/
public function destroy(int $id): JSONResponse
{
$this->mappingMapper->delete($this->mappingMapper->find((int) $id));

return new JSONResponse([]);
}

/**
* Tests a mapping
*
* This method tests a mapping with provided input data and optional schema validation.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse A JSON response containing the test results
*
* @example
* Request:
* {
* "inputObject": "{\"name\":\"John Doe\",\"age\":30,\"email\":\"[email protected]\"}",
* "mapping": {
* "mapping": {
* "fullName":"{{name}}",
* "userAge":"{{age}}",
* "contactEmail":"{{email}}"
* }
* },
* "schema": "user_schema_id",
* "validation": true
* }
*
* Response:
* {
* "resultObject": {
* "fullName": "John Doe",
* "userAge": 30,
* "contactEmail": "[email protected]"
* },
* "isValid": true,
* "validationErrors": []
* }
*/
/**
* Tests a mapping
*
* This method tests a mapping with provided input data and optional schema validation.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param ObjectService $objectService
* @param IURLGenerator $urlGenerator
*
* @return JSONResponse A JSON response containing the test results
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*
* @example
* Request:
* {
* "inputObject": "{\"name\":\"John Doe\",\"age\":30,\"email\":\"[email protected]\"}",
* "mapping": {
* "mapping": {
* "fullName":"{{name}}",
* "userAge":"{{age}}",
* "contactEmail":"{{email}}"
* }
* },
* "schema": "user_schema_id",
* "validation": true
* }
*
* Response:
* {
* "resultObject": {
* "fullName": "John Doe",
* "userAge": 30,
* "contactEmail": "[email protected]"
* },
* "isValid": true,
* "validationErrors": []
* }
*/
public function test(ObjectService $objectService, IURLGenerator $urlGenerator): JSONResponse
{
$openRegisters = $objectService->getOpenRegisters();
Expand Down Expand Up @@ -293,32 +301,42 @@ public function test(ObjectService $objectService, IURLGenerator $urlGenerator):
]);
}

/**
* Saves a mapping object
*
* This method saves a mapping object based on POST data.
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function saveObject(): JSONResponse
/**
* Saves a mapping object
*
* This method saves a mapping object based on POST data.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse|null
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function saveObject(): ?JSONResponse
{
// Check if the OpenRegister service is available
$openRegisters = $this->objectService->getOpenRegisters();
if ($openRegisters !== null) {
$data = $this->request->getParams();
return new JSONResponse($openRegisters->saveObject($data['register'], $data['schema'], $data['object']));
}

return null;
}

/**
* Retrieves a list of objects to map to
*
* This method retrieves a list of objects to map to based on GET data.
*
* @NoAdminRequired
* @NoCSRFRequired
*/
/**
* Retrieves a list of objects to map to
*
* This method retrieves a list of objects to map to based on GET data.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getObjects(): JSONResponse
{
// Check if the OpenRegister service is available
Expand All @@ -333,6 +351,6 @@ public function getObjects(): JSONResponse
}

return new JSONResponse($data);

}
}
13 changes: 7 additions & 6 deletions lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use OCP\App\IAppManager;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -49,7 +50,7 @@ public function getClient(array $config): Client
* @param array $config The configuration that should be used by the call.
*
* @return array The resulting object.
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function saveObject(array $data, array $config): array
{
Expand Down Expand Up @@ -81,7 +82,7 @@ public function saveObject(array $data, array $config): array
*
* @return array The objects found for given filters.
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function findObjects(array $filters, array $config): array
{
Expand Down Expand Up @@ -115,7 +116,7 @@ public function findObjects(array $filters, array $config): array
*
* @return array The resulting object.
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function findObject(array $filters, array $config): array
{
Expand Down Expand Up @@ -149,7 +150,7 @@ public function findObject(array $filters, array $config): array
*
* @return array The updated object.
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function updateObject(array $filters, array $update, array $config): array
{
Expand Down Expand Up @@ -181,7 +182,7 @@ public function updateObject(array $filters, array $update, array $config): arra
*
* @return array An empty array.
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function deleteObject(array $filters, array $config): array
{
Expand All @@ -206,7 +207,7 @@ public function deleteObject(array $filters, array $config): array
* @param array $pipeline The pipeline to use.
* @param array $config The configuration to use in the call.
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function aggregateObjects(array $filters, array $pipeline, array $config):array
{
Expand Down

0 comments on commit cc823cf

Please sign in to comment.