1
0
Fork 0
mirror of synced 2024-05-20 04:32:37 +12:00

fix: database service

This commit is contained in:
Torsten Dittmann 2021-12-27 11:45:24 +01:00
parent 6aeb12f3a0
commit ffae301d5f
2 changed files with 51 additions and 2 deletions

View file

@ -1621,6 +1621,8 @@ App::post('/v1/database/collections/:collectionId/documents')
/**
* Skip Authorization to get the collection. Needed in case of empty permissions for document level permissions.
*
* @var Document $collection
*/
$collection = Authorization::skip(fn() => $dbForInternal->getDocument('collections', $collectionId));
@ -1666,6 +1668,7 @@ App::post('/v1/database/collections/:collectionId/documents')
} else {
$document = $dbForExternal->createDocument('collection_' . $collectionId, new Document($data));
}
$document->setAttribute('$collection', $collectionId);
}
catch (StructureException $exception) {
throw new Exception($exception->getMessage(), 400);
@ -1774,6 +1777,11 @@ App::get('/v1/database/collections/:collectionId/documents')
$sum = $dbForExternal->count('collection_' . $collectionId, $queries, APP_LIMIT_COUNT);
}
/**
* Reset $collection attribute to remove prefix.
*/
$documents = array_map(fn(Document $document) => $document->setAttribute('$collection', $collectionId), $documents);
$usage
->setParam('database.documents.read', 1)
->setParam('collectionId', $collectionId)
@ -1835,6 +1843,11 @@ App::get('/v1/database/collections/:collectionId/documents/:documentId')
$document = $dbForExternal->getDocument('collection_' . $collectionId, $documentId);
}
/**
* Reset $collection attribute to remove prefix.
*/
$document->setAttribute('$collection', $collectionId);
if ($document->isEmpty()) {
throw new Exception('No document found', 404);
}
@ -2042,6 +2055,10 @@ App::patch('/v1/database/collections/:collectionId/documents/:documentId')
} else {
$document = $dbForExternal->updateDocument('collection_' . $collection->getId(), $document->getId(), new Document($data));
}
/**
* Reset $collection attribute to remove prefix.
*/
$document->setAttribute('$collection', $collectionId);
}
catch (AuthorizationException $exception) {
throw new Exception('Unauthorized permissions', 401);
@ -2135,6 +2152,11 @@ App::delete('/v1/database/collections/:collectionId/documents/:documentId')
$dbForExternal->deleteCachedDocument('collection_' . $collectionId, $documentId);
/**
* Reset $collection attribute to remove prefix.
*/
$document->setAttribute('$collection', $collectionId);
$usage
->setParam('database.documents.delete', 1)
->setParam('collectionId', $collectionId)

View file

@ -827,6 +827,10 @@ trait DatabaseBase
$this->assertEquals(2019, $documents['body']['documents'][2]['releaseYear']);
$this->assertCount(3, $documents['body']['documents']);
foreach ($documents['body']['documents'] as $document) {
$this->assertEquals($data['moviesId'], $document['$collection']);
}
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
@ -841,7 +845,28 @@ trait DatabaseBase
$this->assertEquals(2019, $documents['body']['documents'][0]['releaseYear']);
$this->assertCount(3, $documents['body']['documents']);
return [];
return $documents['body']['documents'];
}
/**
* @depends testListDocuments
*/
public function testGetDocument(array $documents): void
{
foreach ($documents as $document) {
$response = $this->client->call(Client::METHOD_GET, '/database/collections/' . $document['$collection'] . '/documents/' . $document['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($response['headers']['status-code'], 200);
$this->assertEquals($response['body']['$id'], $document['$id']);
$this->assertEquals($response['body']['$collection'], $document['$collection']);
$this->assertEquals($response['body']['title'], $document['title']);
$this->assertEquals($response['body']['releaseYear'], $document['releaseYear']);
$this->assertEquals($response['body']['$read'], $document['$read']);
$this->assertEquals($response['body']['$write'], $document['$write']);
}
}
/**
@ -1218,6 +1243,8 @@ trait DatabaseBase
]);
$this->assertEquals($document['headers']['status-code'], 200);
$this->assertEquals($document['body']['$id'], $id);
$this->assertEquals($document['body']['$collection'], $data['moviesId']);
$this->assertEquals($document['body']['title'], 'Thor: Ragnarok');
$this->assertEquals($document['body']['releaseYear'], 2017);
$this->assertEquals('role:member', $document['body']['$read'][0]);
@ -1280,7 +1307,7 @@ trait DatabaseBase
], $this->getHeaders()));
$this->assertEquals($document['headers']['status-code'], 404);
return $data;
}