diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 864a06dab..abc935803 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -24,6 +24,8 @@ use Utopia\Database\Document as Document2; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization as Authorization2; +use function Amp\Promise\wait; + App::post('/v1/database/collections') ->desc('Create Collection') ->groups(['api', 'database']) @@ -98,11 +100,11 @@ App::get('/v1/database/collections/:collectionId') ->label('sdk.response.model', Response::MODEL_COLLECTION) ->param('collectionId', '', new UID(), 'Collection unique ID.') ->inject('response') - ->inject('projectDB') + ->inject('dbForExternal') ->action(function ($collectionId, $response, $dbForExternal) { /** @var Appwrite\Utopia\Response $response */ /** @var Utopia\Database\Database $dbForExternal */ - + $collection = $dbForExternal->getCollection($collectionId); if ($collection->isEmpty()) { @@ -257,10 +259,10 @@ App::post('/v1/database/collections/:collectionId/attributes') // TODO@kodumbeats add units to description ->param('size', null, new Numeric(), 'Attribute size.') ->param('required', null, new Boolean(), 'Is attribute required?') - ->param('signed', null, new Boolean(), 'Is attribute signed?') - ->param('array', null, new Boolean(), 'Is attribute an array?') + ->param('signed', true, new Boolean(), 'Is attribute signed?', true) + ->param('array', false, new Boolean(), 'Is attribute an array?', true) // TODO@kodumbeats "We should have here a whitelist of allowed values. We should add a link to a reference in the future documentation. We might be able to hide this option from the public API for now." - ->param('filters', [], new ArrayList(new Text(256)), 'Array of filters.') + ->param('filters', [], new ArrayList(new Text(256)), 'Array of filters.', true) ->inject('response') ->inject('dbForExternal') ->inject('audits') @@ -597,7 +599,7 @@ App::delete('/v1/database/collections/:collectionId/indexes/:indexId') $collection = $dbForExternal->getCollection($collectionId); - if (empty($collection)) { + if ($collection->isEmpty()) { throw new Exception('Collection not found', 404); } @@ -677,7 +679,7 @@ App::post('/v1/database/collections/:collectionId/documents') $collection = $dbForExternal->getCollection($collectionId); - if (empty($collection)) { + if ($collection->isEmpty()) { throw new Exception('Collection not found', 404); } @@ -793,7 +795,7 @@ App::get('/v1/database/collections/:collectionId/documents/:documentId') $collection = $dbForExternal->getCollection($collectionId); - if (empty($collection)) { + if ($collection->isEmpty()) { throw new Exception('Collection not found', 404); } @@ -903,7 +905,7 @@ App::delete('/v1/database/collections/:collectionId/documents/:documentId') $collection = $dbForExternal->getCollection($collectionId); - if (empty($collection)) { + if ($collection->isEmpty()) { throw new Exception('Collection not found', 404); } diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 65ec5230a..bbae1c3e9 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -182,6 +182,8 @@ class Response extends SwooleResponse // Entities ->setModel(new Permissions()) ->setModel(new Collection()) + ->setModel(new Attribute()) + ->setModel(new Index()) ->setModel(new ModelDocument()) ->setModel(new Rule()) ->setModel(new Log()) diff --git a/tests/e2e/Services/Database/DatabaseCustomServerTest.php b/tests/e2e/Services/Database/DatabaseCustomServerTest.php index 559072875..607b31ba1 100644 --- a/tests/e2e/Services/Database/DatabaseCustomServerTest.php +++ b/tests/e2e/Services/Database/DatabaseCustomServerTest.php @@ -25,36 +25,39 @@ class DatabaseCustomServerTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), [ - 'name' => 'Actors', - 'read' => ['*'], - 'write' => ['role:member', 'role:admin'], - 'rules' => [ - [ - 'label' => 'First Name', - 'key' => 'firstName', - 'type' => 'text', - 'default' => '', - 'required' => true, - 'array' => false - ], - [ - 'label' => 'Last Name', - 'key' => 'lastName', - 'type' => 'text', - 'default' => '', - 'required' => true, - 'array' => false - ], - ], + 'id' => 'Actors', ]); $this->assertEquals($actors['headers']['status-code'], 201); $this->assertEquals($actors['body']['name'], 'Actors'); - $this->assertIsArray($actors['body']['$permissions']); - $this->assertIsArray($actors['body']['$permissions']['read']); - $this->assertIsArray($actors['body']['$permissions']['write']); - $this->assertCount(1, $actors['body']['$permissions']['read']); - $this->assertCount(2, $actors['body']['$permissions']['write']); + + $firstName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'id' => 'firstName', + 'type' => 'string', + 'size' => 256, + 'required' => true, + ]); + + $lastName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'id' => 'lastName', + 'type' => 'string', + 'size' => 256, + 'required' => true, + ]); + + $collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $actors['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), []); // Add Documents to the collection $document1 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/documents', array_merge([ @@ -80,24 +83,22 @@ class DatabaseCustomServerTest extends Scope 'read' => ['user:'.$this->getUser()['$id']], 'write' => ['user:'.$this->getUser()['$id']], ]); - + $this->assertEquals($document1['headers']['status-code'], 201); $this->assertEquals($document1['body']['$collection'], $actors['body']['$id']); - $this->assertIsArray($document1['body']['$permissions']); - $this->assertIsArray($document1['body']['$permissions']['read']); - $this->assertIsArray($document1['body']['$permissions']['write']); - $this->assertCount(1, $document1['body']['$permissions']['read']); - $this->assertCount(1, $document1['body']['$permissions']['write']); + $this->assertIsArray($document1['body']['$read']); + $this->assertIsArray($document1['body']['$write']); + $this->assertCount(1, $document1['body']['$read']); + $this->assertCount(1, $document1['body']['$write']); $this->assertEquals($document1['body']['firstName'], 'Tom'); $this->assertEquals($document1['body']['lastName'], 'Holland'); $this->assertEquals($document2['headers']['status-code'], 201); $this->assertEquals($document2['body']['$collection'], $actors['body']['$id']); - $this->assertIsArray($document2['body']['$permissions']); - $this->assertIsArray($document2['body']['$permissions']['read']); - $this->assertIsArray($document2['body']['$permissions']['write']); - $this->assertCount(1, $document2['body']['$permissions']['read']); - $this->assertCount(1, $document2['body']['$permissions']['write']); + $this->assertIsArray($document2['body']['$read']); + $this->assertIsArray($document2['body']['$write']); + $this->assertCount(1, $document2['body']['$read']); + $this->assertCount(1, $document2['body']['$write']); $this->assertEquals($document2['body']['firstName'], 'Samuel'); $this->assertEquals($document2['body']['lastName'], 'Jackson');