From e9d93ce95e81f032f70cb247b33d7edb81472465 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 10 Jun 2021 09:15:00 -0400 Subject: [PATCH] Throw 404 when collection not found --- app/controllers/api/database.php | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 4c956d17fe..ad77af5a0d 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -317,6 +317,12 @@ App::post('/v1/database/collections/:collectionId/attributes') /** @var Utopia\Database\Database $dbForExternal*/ /** @var Appwrite\Event\Event $audits */ + $collection = $dbForExternal->getCollection($collectionId); + + if (empty($collection)) { + throw new Exception('Collection not found', 404); + } + // TODO@kodumbeats handle failed attribute creation $success = $dbForExternal->createAttribute($collectionId, $id, $type, $size, $required, $signed, $array, $filters); @@ -363,6 +369,10 @@ App::get('v1/database/collections/:collectionId/attributes') $collection = $dbForExternal->getCollection($collectionId); + if (empty($collection)) { + throw new Exception('Collection not found', 404); + } + // TODO@kodumbeats array_merge collectionId to each attribute $attributes = $collection->getAttributes(); @@ -393,6 +403,10 @@ App::get('v1/database/collections/:collectionId/attributes/:attributeId') $collection = $dbForExternal->getCollection($collectionId); + if (empty($collection)) { + throw new Exception('Collection not found', 404); + } + $attributes = $collection->getAttributes(); // Search for attribute @@ -435,6 +449,10 @@ App::delete('/v1/database/collections/:collectionId/attributes/:attributeId') $collection = $dbForExternal->getCollection($collectionId); + if (empty($collection)) { + throw new Exception('Collection not found', 404); + } + $attributes = $collection->getAttributes(); // Search for attribute @@ -496,6 +514,12 @@ App::post('/v1/database/collections/:collectionId/indexes') /** @var Utopia\Database\Database $dbForExternal */ /** @var Appwrite\Event\Event $audits */ + $collection = $dbForExternal->getCollection($collectionId); + + if (empty($collection)) { + throw new Exception('Collection not found', 404); + } + $success = $dbForExternal->createIndex($collectionId, $id, $type, $attributes, $lengths, $orders); // Database->createIndex() does not return a document @@ -540,6 +564,10 @@ App::get('v1/database/collections/:collectionId/indexes') $collection = $dbForExternal->getCollection($collectionId); + if (empty($collection)) { + throw new Exception('Collection not found', 404); + } + // TODO@kodumbeats decode index string and merge ['$collection' => $collectionId] $indexes = $collection->getAttribute('indexes'); @@ -570,6 +598,10 @@ App::get('v1/database/collections/:collectionId/indexes/:indexId') $collection = $dbForExternal->getCollection($collectionId); + if (empty($collection)) { + throw new Exception('Collection not found', 404); + } + // TODO@kodumbeats decode 'indexes' into array $indexes = $collection->getAttribute('indexes'); @@ -613,6 +645,10 @@ App::delete('/v1/database/collections/:collectionId/indexes/:indexId') $collection = $dbForExternal->getCollection($collectionId); + if (empty($collection)) { + throw new Exception('Collection not found', 404); + } + // TODO@kodumbeats decode 'indexes' into array $indexes = $collection->getAttribute('indexes');