From 7e185cd7991490c837825c79384038b1e12d4b43 Mon Sep 17 00:00:00 2001 From: prateek banga Date: Tue, 8 Aug 2023 03:34:13 +0530 Subject: [PATCH] moved formatting logic to relationship attribute model --- app/controllers/api/databases.php | 14 ------------- .../Response/Model/AttributeRelationship.php | 21 ++++++++++++++++++- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 9279d4d115..424d162f6f 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -1718,20 +1718,6 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/attributes') $filterQueries = Query::groupByType($queries)['filters']; $total = $dbForProject->count('attributes', $filterQueries, APP_LIMIT_COUNT); - //Add relationship data from options to attributes as it loses options during response setup - foreach ($attributes as $attribute) { - if ($attribute->getAttribute('type') === Database::VAR_RELATIONSHIP) { - $options = $attribute->getAttribute('options'); - if (!\is_null($options)) { - $attribute->setAttribute('relatedCollection', $options['relatedCollection']); - $attribute->setAttribute('relationType', $options['relationType']); - $attribute->setAttribute('twoWay', $options['twoWay']); - $attribute->setAttribute('twoWayKey', $options['twoWayKey']); - $attribute->setAttribute('side', $options['side']); - $attribute->setAttribute('onDelete', $options['onDelete']); - } - } - } $output = $response->output(new Document([ 'total' => $total, 'attributes' => $attributes, diff --git a/src/Appwrite/Utopia/Response/Model/AttributeRelationship.php b/src/Appwrite/Utopia/Response/Model/AttributeRelationship.php index dc3300c098..b6013e8ae1 100644 --- a/src/Appwrite/Utopia/Response/Model/AttributeRelationship.php +++ b/src/Appwrite/Utopia/Response/Model/AttributeRelationship.php @@ -3,7 +3,7 @@ namespace Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response; - +use Utopia\Database\Document; class AttributeRelationship extends Attribute { public function __construct() @@ -73,4 +73,23 @@ class AttributeRelationship extends Attribute { return Response::MODEL_ATTRIBUTE_RELATIONSHIP; } + + /** + * Process Document before returning it to the client + * + * @return Document + */ + public function filter(Document $document): Document + { + $options = $document->getAttribute('options'); + if (!\is_null($options)) { + $document->setAttribute('relatedCollection', $options['relatedCollection']); + $document->setAttribute('relationType', $options['relationType']); + $document->setAttribute('twoWay', $options['twoWay']); + $document->setAttribute('twoWayKey', $options['twoWayKey']); + $document->setAttribute('side', $options['side']); + $document->setAttribute('onDelete', $options['onDelete']); + } + return $document; + } }