Skip to content
This repository was archived by the owner on May 29, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
'Registers' => ['url' => 'api/registers'],
'Schemas' => ['url' => 'api/schemas'],
'Sources' => ['url' => 'api/sources'],
'Objects' => ['url' => 'api/objects'],
],
'routes' => [
['name' => 'dashboard#page', 'url' => '/', 'verb' => 'GET'],
['name' => 'dashboard#page', 'url' => '/', 'verb' => 'GET'],
['name' => 'registers#objects', 'url' => '/api/registers-objects/{register}/{schema}', 'verb' => 'GET'],
],
];
165 changes: 165 additions & 0 deletions lib/Controller/ObjectsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace OCA\OpenRegister\Controller;

use OCA\OpenRegister\Service\ObjectService;
use OCA\OpenRegister\Service\SearchService;
use OCA\OpenRegister\Db\ObjectEntity;
use OCA\OpenRegister\Db\ObjectEntityMapper;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;

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

/**
* Returns the template of the main app's page
*
* This method renders the main page of the application, adding any necessary data to the template.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return TemplateResponse The rendered template response
*/
public function page(): TemplateResponse
{
return new TemplateResponse(
'openconnector',
'index',
[]
);
}

/**
* Retrieves a list of all objects
*
* This method returns a JSON response containing an array of all objects in the system.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse A JSON response containing the list of objects
*/
public function index(ObjectService $objectService, SearchService $searchService): JSONResponse
{
$filters = $this->request->getParams();
$fieldsToSearch = ['uuid', 'register', 'schema'];

$searchParams = $searchService->createMySQLSearchParams(filters: $filters);
$searchConditions = $searchService->createMySQLSearchConditions(filters: $filters, fieldsToSearch: $fieldsToSearch);
$filters = $searchService->unsetSpecialQueryParams(filters: $filters);

return new JSONResponse(['results' => $this->objectEntityMapper->findAll(limit: null, offset: null, filters: $filters, searchConditions: $searchConditions, searchParams: $searchParams)]);
}

/**
* Retrieves a single object by its ID
*
* This method returns a JSON response containing the details of a specific object.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the object to retrieve
* @return JSONResponse A JSON response containing the object details
*/
public function show(string $id): JSONResponse
{
try {
return new JSONResponse($this->objectEntityMapper->find(id: (int) $id));
} catch (DoesNotExistException $exception) {
return new JSONResponse(data: ['error' => 'Not Found'], statusCode: 404);
}
}

/**
* Creates a new object
*
* This method creates a new object based on POST data.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse A JSON response containing the created object
*/
public function create(): JSONResponse
{
$data = $this->request->getParams();

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

if (isset($data['id'])) {
unset($data['id']);
}

return new JSONResponse($this->objectEntityMapper->createFromArray(object: $data));
}

/**
* Updates an existing object
*
* This method updates an existing object based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the object to update
* @return JSONResponse A JSON response containing the updated object details
*/
public function update(int $id): JSONResponse
{
$data = $this->request->getParams();

foreach ($data as $key => $value) {
if (str_starts_with($key, '_')) {
unset($data[$key]);
}
}
if (isset($data['id'])) {
unset($data['id']);
}
return new JSONResponse($this->objectEntityMapper->updateFromArray(id: (int) $id, object: $data));
}

/**
* Deletes an object
*
* This method deletes an object based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the object to delete
* @return JSONResponse An empty JSON response
*/
public function destroy(int $id): JSONResponse
{
$this->objectEntityMapper->delete($this->objectEntityMapper->find((int) $id));

return new JSONResponse([]);
}
}
22 changes: 21 additions & 1 deletion lib/Controller/RegistersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use OCA\OpenRegister\Service\SearchService;
use OCA\OpenRegister\Db\Register;
use OCA\OpenRegister\Db\RegisterMapper;
use OCA\OpenRegister\Db\ObjectEntityMapper;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
Expand All @@ -25,7 +26,8 @@ public function __construct(
$appName,
IRequest $request,
private readonly IAppConfig $config,
private readonly RegisterMapper $registerMapper
private readonly RegisterMapper $registerMapper,
private readonly ObjectEntityMapper $objectEntityMapper
)
{
parent::__construct($appName, $request);
Expand Down Expand Up @@ -162,4 +164,22 @@ public function destroy(int $id): JSONResponse

return new JSONResponse([]);
}

/**
* Get objects
*
* Get all the objects for a register and schema
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $register The ID of the register
* @param string $schema The ID of the schema
*
* @return JSONResponse An empty JSON response
*/
public function objects(int $register, int $schema): JSONResponse
{
return new JSONResponse($this->objectEntityMapper->findByRegisterAndSchema(register: $register, schema: $schema));
}
}
73 changes: 73 additions & 0 deletions lib/Db/ObjectEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace OCA\OpenRegister\Db;

use DateTime;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;

class ObjectEntity extends Entity implements JsonSerializable
{
protected ?string $uuid = null;
protected ?string $register = null;
protected ?string $schema = null;
protected ?array $object = [];
protected ?DateTime $updated = null;
protected ?DateTime $created = null;

public function __construct() {
$this->addType(fieldName:'uuid', type: 'string');
$this->addType(fieldName: 'register', type: 'string');
$this->addType(fieldName: 'schema', type: 'string');
$this->addType(fieldName: 'object', type: 'json');
$this->addType(fieldName:'updated', type: 'datetime');
$this->addType(fieldName:'created', type: 'datetime');
}

public function getJsonFields(): array
{
return array_keys(
array_filter($this->getFieldTypes(), function ($field) {
return $field === 'json';
})
);
}

public function hydrate(array $object): self
{
$jsonFields = $this->getJsonFields();

if(isset($object['metadata']) === false) {
$object['metadata'] = [];
}

foreach($object as $key => $value) {
if (in_array($key, $jsonFields) === true && $value === []) {
$value = null;
}

$method = 'set'.ucfirst($key);

try {
$this->$method($value);
} catch (\Exception $exception) {
}
}

return $this;
}


public function jsonSerialize(): array
{
return [
'id' => $this->id,
'uuid' => $this->uuid,
'register' => $this->register,
'schema' => $this->schema,
'object' => $this->object,
'updated' => isset($this->updated) ? $this->updated->format('c') : null,
'created' => isset($this->created) ? $this->created->format('c') : null
];
}
}
Loading