From 82085df58a839a47b910a1f511196492aeccf558 Mon Sep 17 00:00:00 2001 From: Matej Baco Date: Mon, 30 Aug 2021 09:33:45 +0200 Subject: [PATCH] Migrated project platforms to it's own collection --- app/config/collections2.php | 107 ++++++++++++++++++++++++++++++- app/controllers/api/projects.php | 36 ++++++----- app/init.php | 12 ++++ 3 files changed, 138 insertions(+), 17 deletions(-) diff --git a/app/config/collections2.php b/app/config/collections2.php index 8079bdd7c..1e4e4fb30 100644 --- a/app/config/collections2.php +++ b/app/config/collections2.php @@ -511,7 +511,7 @@ $collections = [ 'required' => false, 'default' => null, 'array' => true, - 'filters' => ['json'], + 'filters' => ['subQueryProjectPlatforms'], ], [ '$id' => 'webhooks', @@ -558,6 +558,111 @@ $collections = [ ], ], + 'projectsPlatforms' => [ + '$collection' => Database::METADATA, + '$id' => 'projectsPlatforms', + 'name' => 'projectsPlatforms', + 'attributes' => [ + [ + '$id' => 'projectId', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'type', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'name', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 256, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'key', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'store', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 256, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'hostname', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 256, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'dateCreated', + 'type' => Database::VAR_INTEGER, + 'format' => '', + 'size' => 0, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'dateUpdated', + 'type' => Database::VAR_INTEGER, + 'format' => '', + 'size' => 0, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + ], + 'indexes' => [ + [ + '$id' => '_key_project', + 'type' => Database::INDEX_KEY, + 'attributes' => ['projectId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + ], + ], + 'users' => [ '$collection' => Database::METADATA, '$id' => 'users', diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index b83dff6d9..2dc5ce9ac 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -1047,8 +1047,13 @@ App::post('/v1/projects/:projectId/platforms') throw new Exception('Project not found', 404); } + $teamId = $project->getAttribute("teamId"); + $platform = new Document([ + 'projectId' => $project->getId(), '$id' => $dbForConsole->getId(), + '$read' => ['team:' . $teamId], + '$write' => ['team:' . $teamId . '/owner', 'team:' . $teamId . '/developer'], 'type' => $type, 'name' => $name, 'key' => $key, @@ -1058,9 +1063,8 @@ App::post('/v1/projects/:projectId/platforms') 'dateUpdated' => \time(), ]); - $project = $dbForConsole->updateDocument('projects', $project->getId(), $project - ->setAttribute('platforms', $platform, Document::SET_TYPE_APPEND) - ); + $dbForConsole->createDocument("projectsPlatforms", $platform); + $dbForConsole->purgeDocument('projects', $project->getId()); $response->setStatusCode(Response::STATUS_CODE_CREATED); $response->dynamic($platform, Response::MODEL_PLATFORM); @@ -1083,13 +1087,17 @@ App::get('/v1/projects/:projectId/platforms') /** @var Appwrite\Utopia\Response $response */ /** @var Utopia\Database\Database $dbForConsole */ + // TODO: Implement pagination + $project = $dbForConsole->getDocument('projects', $projectId); if ($project->isEmpty()) { throw new Exception('Project not found', 404); } - $platforms = $project->getAttribute('platforms', []); + $platforms = $dbForConsole->find('projectsPlatforms', [ + new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) + ]); $response->dynamic(new Document([ 'platforms' => $platforms, @@ -1121,7 +1129,7 @@ App::get('/v1/projects/:projectId/platforms/:platformId') throw new Exception('Project not found', 404); } - $platform = $project->find('$id', $platformId, 'platforms'); + $platform = $dbForConsole->getDocument("projectsPlatforms", $platformId); if (empty($platform) || !$platform instanceof Document) { throw new Exception('Platform not found', 404); @@ -1158,7 +1166,7 @@ App::put('/v1/projects/:projectId/platforms/:platformId') throw new Exception('Project not found', 404); } - $platform = $project->find('$id', $platformId, 'platforms'); + $platform = $dbForConsole->getDocument("projectsPlatforms", $platformId); if (empty($platform) || !$platform instanceof Document) { throw new Exception('Platform not found', 404); @@ -1172,15 +1180,9 @@ App::put('/v1/projects/:projectId/platforms/:platformId') ->setAttribute('hostname', $hostname) ; - $project->findAndReplace('$id', $platform->getId(), $platform - ->setAttribute('name', $name) - ->setAttribute('dateUpdated', \time()) - ->setAttribute('key', $key) - ->setAttribute('store', $store) - ->setAttribute('hostname', $hostname) - , 'platforms'); + $dbForConsole->updateDocument('projectsPlatforms', $platform->getId(), $platform); - $dbForConsole->updateDocument('projects', $project->getId(), $project); + $dbForConsole->purgeDocument('projects', $project->getId()); $response->dynamic($platform, Response::MODEL_PLATFORM); }); @@ -1208,11 +1210,13 @@ App::delete('/v1/projects/:projectId/platforms/:platformId') throw new Exception('Project not found', 404); } - if (!$project->findAndRemove('$id', $platformId, 'platforms')) { + $deleteRecord = $dbForConsole->deleteDocument("projectsPlatforms", $platformId); + + if($deleteRecord == false) { throw new Exception('Platform not found', 404); } - $dbForConsole->updateDocument('projects', $project->getId(), $project); + $dbForConsole->purgeDocument('projects', $project->getId()); $response->noContent(); }); diff --git a/app/init.php b/app/init.php index 57f2dc713..4da737ebb 100644 --- a/app/init.php +++ b/app/init.php @@ -200,6 +200,18 @@ Database::addFilter('subQueryIndexes', } ); +Database::addFilter('subQueryProjectPlatforms', + function($value) { + return null; + }, + function($value, Document $document, Database $database) { + return $database + ->find('projectsPlatforms', [ + new Query('collectionId', Query::TYPE_EQUAL, [$document->getId()]) + ], 100, 0, []); + } +); + Database::addFilter('encrypt', function($value) { $key = App::getEnv('_APP_OPENSSL_KEY_V1');