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

Fix for the not found error #123

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions lib/Controller/ObjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function index(ObjectService $objectService, SearchService $searchService
public function show(string $id): JSONResponse
{
try {
return new JSONResponse($this->objectEntityMapper->find(idOrUuid: (int) $id)->getObjectArray());
return new JSONResponse($this->objectEntityMapper->find(identifier: (int) $id)->getObjectArray());
} catch (DoesNotExistException $exception) {
return new JSONResponse(data: ['error' => 'Not Found'], statusCode: 404);
}
Expand Down Expand Up @@ -235,7 +235,7 @@ public function destroy(int $id): JSONResponse
$oldObject = $this->objectEntityMapper->find($id);
$this->auditTrailMapper->createAuditTrail(old: $oldObject);

$this->objectEntityMapper->delete($this->objectEntityMapper->find($id));
$this->objectEntityMapper->delete($this->objectEntityMapper->find(identifier: $id));

return new JSONResponse([]);
}
Expand Down Expand Up @@ -272,7 +272,7 @@ public function auditTrails(int $id): JSONResponse
public function contracts(int $id): JSONResponse
{
// Create a log entry
$oldObject = $this->objectEntityMapper->find($id);
$oldObject = $this->objectEntityMapper->find(identifier: $id);
$this->auditTrailMapper->createAuditTrail(old: $oldObject);

return new JSONResponse(['error' => 'Not yet implemented'], 501);
Expand All @@ -294,7 +294,7 @@ public function relations(int $id): JSONResponse
{
try {
// Lets grap the object to stablish an uri
$object = $this->objectEntityMapper->find($id);
$object = $this->objectEntityMapper->find(identifier: $id);
$relations = $this->objectEntityMapper->findByRelationUri($object->getUri());

// We dont want to return the entity, but the object (and kant reley on the normal serilzier)
Expand Down
4 changes: 2 additions & 2 deletions lib/Db/FileMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public function __construct(IDBConnection $db)
* @throws DoesNotExistException If no file is found with the given ID.
* @throws MultipleObjectsReturnedException If multiple files are found with the given ID.
*/
public function find(int $id): File
public function find(int $identifier): File
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openregister_files')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
$qb->expr()->eq('id', $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_INT))
);

return $this->findEntity(query: $qb);
Expand Down
64 changes: 16 additions & 48 deletions lib/Db/ObjectEntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ public function __construct(IDBConnection $db, MySQLJsonService $mySQLJsonServic
}

/**
* Find an object by ID or UUID
* Find an object by ID, UUID or URI with optional register and schema filtering
*
* @param int|string $idOrUuid The ID or UUID of the object to find
* @return ObjectEntity The ObjectEntity
* @param int|string $identifier The ID, UUID or URI of the object to find
* @param Register|null $register Optional register to filter by
* @param Schema|null $schema Optional schema to filter by
* @return ObjectEntity|null The found object entity or null if not found
*/
public function find($identifier): ObjectEntity
public function find($identifier, ?Register $register = null, ?Schema $schema = null): ObjectEntity|null
{
$qb = $this->db->getQueryBuilder();

Expand All @@ -60,53 +62,19 @@ public function find($identifier): ObjectEntity
)
);

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

/**
* Find an object by UUID
*
* @param string $uuid The UUID of the object to find
* @return ObjectEntity The object
*/
public function findByUuid(Register $register, Schema $schema, string $uuid): ObjectEntity|null
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openregister_objects')
->where(
$qb->expr()->eq('uuid', $qb->createNamedParameter($uuid))
)
->andWhere(
// Add register filter if provided
if ($register !== null) {
$qb->andWhere(
$qb->expr()->eq('register', $qb->createNamedParameter($register->getId()))
)
->andWhere(
$qb->expr()->eq('schema', $qb->createNamedParameter($schema->getId()))
);

try {
return $this->findEntity($qb);
} catch (\OCP\AppFramework\Db\DoesNotExistException $e) {
return null;
}
}

/**
* Find an object by UUID only
*
* @param string $uuid The UUID of the object to find
* @return ObjectEntity The object
*/
public function findByUuidOnly(string $uuid): ObjectEntity|null
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openregister_objects')
->where(
$qb->expr()->eq('uuid', $qb->createNamedParameter($uuid))
);
// Add schema filter if provided
if ($schema !== null) {
$qb->andWhere(
$qb->expr()->eq('schema', $qb->createNamedParameter($schema->getId()))
);
}

try {
return $this->findEntity($qb);
Expand Down Expand Up @@ -263,7 +231,7 @@ public function createFromArray(array $object): ObjectEntity
*/
public function updateFromArray(int $id, array $object): ObjectEntity
{
$obj = $this->find($id);
$obj = $this->find(identifier: $id);
$obj->hydrate($object);

// Set or update the version
Expand Down
12 changes: 6 additions & 6 deletions lib/Db/RegisterMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public function __construct(IDBConnection $db, SchemaMapper $schemaMapper)
* @param int $id The ID of the register to find
* @return Register The found register
*/
public function find(int $id): Register
public function find(int $identifier): Register
{
$qb = $this->db->getQueryBuilder();

// Build the query
$qb->select('*')
->from('openregister_registers')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
$qb->expr()->eq('id', $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_INT))
);

// Execute the query and return the result
Expand Down Expand Up @@ -118,13 +118,13 @@ public function createFromArray(array $object): Register
/**
* Update an existing register from an array of data
*
* @param int $id The ID of the register to update
* @param int $identifier The ID of the register to update
* @param array $object The new data for the register
* @return Register The updated register
*/
public function updateFromArray(int $id, array $object): Register
public function updateFromArray(int $identifier, array $object): Register
{
$obj = $this->find($id);
$obj = $this->find(identifier: $identifier);
$obj->hydrate($object);

// Update the version
Expand Down Expand Up @@ -153,7 +153,7 @@ public function getSchemasByRegisterId(int $registerId): array

// Fetch each schema by its ID
foreach ($schemaIds as $schemaId) {
$schemas[] = $this->schemaMapper->find((int) $schemaId);
$schemas[] = $this->schemaMapper->find(identifier: (int) $schemaId);
}

return $schemas;
Expand Down
8 changes: 4 additions & 4 deletions lib/Db/SchemaMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public function __construct(IDBConnection $db)
* @param int $id The id of the schema
* @return Schema The schema
*/
public function find(int $id): Schema
public function find(int $identifier): Schema
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openregister_schemas')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
$qb->expr()->eq('id', $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_INT))
);

return $this->findEntity(query: $qb);
Expand All @@ -55,7 +55,7 @@ public function findMultiple(array $ids): array
{
$result = [];
foreach ($ids as $id) {
$result[] = $this->find($id);
$result[] = $this->find(identifier: $id);
}

return $result;
Expand Down Expand Up @@ -129,7 +129,7 @@ public function createFromArray(array $object): Schema
*/
public function updateFromArray(int $id, array $object): Schema
{
$obj = $this->find($id);
$obj = $this->find(identifier: $identifier);
$obj->hydrate($object);

// Set or update the version
Expand Down
8 changes: 4 additions & 4 deletions lib/Db/SourceMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public function __construct(IDBConnection $db)
* @param int $id The id of the source
* @return Source The source
*/
public function find(int $id): Source
public function find(int $identifier): Source
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openregister_sources')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
$qb->expr()->eq('id', $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_INT))
);

return $this->findEntity(query: $qb);
Expand Down Expand Up @@ -110,9 +110,9 @@ public function createFromArray(array $object): Source
* @param array $object The object to update
* @return Source The updated source
*/
public function updateFromArray(int $id, array $object): Source
public function updateFromArray(int $identifier, array $object): Source
{
$obj = $this->find($id);
$obj = $this->find(identifier: $identifier);
$obj->hydrate($object);

// Set or update the version
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/DownloadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function download(string $objectType, string|int $id, string $accept)
$mapper = $this->getMapper($objectType);

try {
$object = $mapper->find($id);
$object = $mapper->find(identifier: $id);
} catch (Exception $exception) {
return ['error' => "Could not find $objectType with id $id.", 'statusCode' => 404];
}
Expand Down
Loading
Loading