Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/CONNECTOR-172/export-of-json-files #163

Merged
merged 12 commits into from
Jan 10, 2025
12 changes: 8 additions & 4 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@
['name' => 'mappings#saveObject', 'url' => '/api/mappings/objects', 'verb' => 'POST'],
['name' => 'mappings#getObjects', 'url' => '/api/mappings/objects', 'verb' => 'GET'],
// Running endpoints - allow any path after /api/endpoints/
['name' => 'endpoints#handlePath', 'postfix' => 'read', 'url' => '/api/endpoint/{_path}', 'verb' => 'GET', 'requirements' => ['_path' => '.+']],
['name' => 'endpoints#handlePath', 'postfix' => 'update', 'url' => '/api/endpoint/{_path}', 'verb' => 'PUT', 'requirements' => ['_path' => '.+']],
['name' => 'endpoints#handlePath', 'postfix' => 'create', 'url' => '/api/endpoint/{_path}', 'verb' => 'POST', 'requirements' => ['_path' => '.+']],
['name' => 'endpoints#handlePath', 'postfix' => 'destroy', 'url' => '/api/endpoint/{_path}', 'verb' => 'DELETE', 'requirements' => ['_path' => '.+']],
['name' => 'endpoints#handlePath', 'url' => '/api/endpoint/{_path}', 'verb' => 'GET', 'requirements' => ['_path' => '.+']],
// ['name' => 'endpoints#handlePath', 'url' => '/api/endpoint/{path}', 'verb' => 'PUT', 'requirements' => ['path' => '.+']],
// ['name' => 'endpoints#handlePath', 'url' => '/api/endpoint/{path}', 'verb' => 'POST', 'requirements' => ['path' => '.+']],
// ['name' => 'endpoints#handlePath', 'url' => '/api/endpoint/{path}', 'verb' => 'DELETE', 'requirements' => ['path' => '.+']],
// Upload & Download
['name' => 'import#import', 'url' => '/api/import', 'verb' => 'POST'],
// ['name' => 'import#importUpdate', 'url' => '/api/import/{id}', 'verb' => 'PUT'],
['name' => 'export#export', 'url' => '/api/export/{type}/{id}', 'verb' => 'GET'],
],
];
52 changes: 52 additions & 0 deletions lib/Controller/ExportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace OCA\OpenConnector\Controller;

use OCA\OpenConnector\Service\ExportService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;

class ExportController extends Controller
{
/**
* Constructor for the ExportController
*
* @param string $appName The name of the app
* @param IRequest $request The request object
* @param IAppConfig $config The app configuration object
* @param ExportService $exportService The Export Service.
*/
public function __construct(
$appName,
IRequest $request,
private IAppConfig $config,
private readonly ExportService $exportService
)
{
parent::__construct($appName, $request);
}

/**
* Creates and return a json file for a specific object.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $type The object type we want to export an object for.
* @param string $id The id used to find an existing object to export.
*
* @return JSONResponse
*/
public function export(string $type, string $id): JSONResponse
{
$accept = $this->request->getHeader(name: 'Accept');

if (empty($accept) === true) {
return new JSONResponse(data: ['error' => 'Request is missing header Accept'], statusCode: 400);
}

return $this->exportService->export(objectType: $type, id: $id, accept: $accept);
}
}
70 changes: 70 additions & 0 deletions lib/Controller/ImportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace OCA\OpenConnector\Controller;

use GuzzleHttp\Exception\GuzzleException;
use OCA\OpenConnector\Service\ImportService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;

class ImportController extends Controller
{
/**
* Constructor for the ImportController
*
* @param string $appName The name of the app
* @param IRequest $request The request object
* @param IAppConfig $config The app configuration object
* @param ImportService $importService The Import Service.
*/
public function __construct(
$appName,
IRequest $request,
private IAppConfig $config,
private readonly ImportService $importService
)
{
parent::__construct($appName, $request);
}

/**
* Creates a new object or updates an existing one using a json text/string as input.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse
* @throws GuzzleException
*/
public function import(): JSONResponse
{
$data = $this->request->getParams();
$uploadedFiles = [];

// Check if multiple files have been uploaded.
$files = $_FILES['files'] ?? null;

if (empty($files) === false) {
// Loop through each file using the count of 'name'
for ($i = 0; $i < count($files['name']); $i++) {
$uploadedFiles[] = [
'name' => $files['name'][$i],
'type' => $files['type'][$i],
'tmp_name' => $files['tmp_name'][$i],
'error' => $files['error'][$i],
'size' => $files['size'][$i]
];
}
}

// Get the uploaded file from the request if a single file hase been uploaded.
$uploadedFile = $this->request->getUploadedFile(key: 'file');
if (empty($uploadedFile) === false) {
$uploadedFiles[] = $uploadedFile;
}

return $this->importService->import(data: $data, uploadedFiles: $uploadedFiles);
}
}
37 changes: 29 additions & 8 deletions lib/Db/ConsumerMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(IDBConnection $db)
}

/**
* Find a Consumer by its ID
* Find a Consumer by its ID.
*
* @param int $id The ID of the Consumer
* @return Consumer The found Consumer entity
Expand All @@ -49,7 +49,26 @@ public function find(int $id): Consumer
}

/**
* Find all Consumers with optional filtering and pagination
* Find a Consumer by its Reference.
*
* @param string $reference
* @return Endpoint
*/
public function findByRef(string $reference): Consumer
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openconnector_consumers')
->where(
$qb->expr()->eq('reference', $qb->createNamedParameter($reference))
);

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

/**
* Find all Consumers with optional filtering and pagination.
*
* @param int|null $limit Maximum number of results to return
* @param int|null $offset Number of results to skip
Expand Down Expand Up @@ -88,7 +107,7 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
}

/**
* Create a new Consumer from an array of data
* Create a new Consumer from an array of data.
*
* @param array $object An array of Consumer data
* @return Consumer The newly created Consumer entity
Expand All @@ -105,7 +124,7 @@ public function createFromArray(array $object): Consumer
}

/**
* Update an existing Consumer from an array of data
* Update an existing Consumer from an array of data.
*
* @param int $id The ID of the Consumer to update
* @param array $object An array of updated Consumer data
Expand All @@ -116,10 +135,12 @@ public function updateFromArray(int $id, array $object): Consumer
$obj = $this->find($id);
$obj->hydrate($object);

// Set or update the version
// $version = explode('.', $obj->getVersion());
// $version[2] = (int)$version[2] + 1;
// $obj->setVersion(implode('.', $version));
if (isset($object['version']) === false) {
// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
}

return $this->update($obj);
}
Expand Down
23 changes: 19 additions & 4 deletions lib/Db/EndpointMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ public function find(int $id): Endpoint
return $this->findEntity(query: $qb);
}

public function findByRef(string $reference): Endpoint
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openconnector_endpoints')
->where(
$qb->expr()->eq('reference', $qb->createNamedParameter($reference))
);

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();
Expand Down Expand Up @@ -85,10 +98,12 @@ public function updateFromArray(int $id, array $object): Endpoint
$obj = $this->find($id);
$obj->hydrate($object);

// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
if (isset($object['version']) === false) {
// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
}

$obj->setEndpointRegex($this->createEndpointRegex($obj->getEndpoint()));
$obj->setEndpointArray(explode('/', $obj->getEndpoint()));
Expand Down
5 changes: 4 additions & 1 deletion lib/Db/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Job extends Entity implements JsonSerializable
protected ?string $uuid = null;
protected ?string $name = null;
protected ?string $description = null;
protected ?string $version = '0.0.0'; // The version of the endpoint
protected ?string $reference = null; // The reference of the Job
protected ?string $version = '0.0.0'; // The version of the Job
protected ?string $jobClass = 'OCA\OpenConnector\Action\PingAction';
protected ?array $arguments = null;
protected ?int $interval = 3600; // seconds in an hour
Expand All @@ -34,6 +35,7 @@ public function __construct() {
$this->addType('uuid', 'string');
$this->addType('name', 'string');
$this->addType('description', 'string');
$this->addType(fieldName:'reference', type: 'string');
$this->addType('version', 'string');
$this->addType('jobClass', 'string');
$this->addType('arguments', 'json');
Expand Down Expand Up @@ -91,6 +93,7 @@ public function jsonSerialize(): array
'uuid' => $this->uuid,
'name' => $this->name,
'description' => $this->description,
'reference' => $this->reference,
'version' => $this->version,
'jobClass' => $this->jobClass,
'arguments' => $this->arguments,
Expand Down
23 changes: 19 additions & 4 deletions lib/Db/JobMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public function find(int $id): Job
return $this->findEntity(query: $qb);
}

public function findByRef(string $reference): Job
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openconnector_endpoints')
->where(
$qb->expr()->eq('reference', $qb->createNamedParameter($reference))
);

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();
Expand Down Expand Up @@ -74,10 +87,12 @@ public function updateFromArray(int $id, array $object): Job
$obj = $this->find($id);
$obj->hydrate($object);

// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
if (isset($object['version']) === false) {
// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
}

return $this->update($obj);
}
Expand Down
19 changes: 16 additions & 3 deletions lib/Db/MappingMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public function find(int $id): Mapping
return $this->findEntity(query: $qb);
}

public function findByRef(string $reference): Mapping
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openconnector_mappings')
->where(
$qb->expr()->eq('reference', $qb->createNamedParameter($reference))
);

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();
Expand Down Expand Up @@ -73,9 +86,9 @@ public function updateFromArray(int $id, array $object): Mapping
{
$obj = $this->find($id);
$obj->hydrate($object);
// Set or update the version
if ($obj->getVersion() !== null) {

if ($obj->getVersion() !== null && isset($object['version']) === false) {
// Set or update the version
$version = explode('.', $obj->getVersion());
if (isset($version[2]) === true) {
$version[2] = (int) $version[2] + 1;
Expand Down
23 changes: 19 additions & 4 deletions lib/Db/SourceMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public function find(int $id): Source
return $this->findEntity(query: $qb);
}

public function findByRef(string $reference): Source
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openconnector_sources')
->where(
$qb->expr()->eq('reference', $qb->createNamedParameter($reference))
);

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();
Expand Down Expand Up @@ -74,10 +87,12 @@ public function updateFromArray(int $id, array $object): Source
$obj = $this->find($id);
$obj->hydrate($object);

// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
if (isset($object['version']) === false) {
// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
}

return $this->update($obj);
}
Expand Down
Loading
Loading