1
0
Fork 0
mirror of synced 2024-07-04 14:10:33 +12:00

moved formatting logic to relationship attribute model

This commit is contained in:
prateek banga 2023-08-08 03:34:13 +05:30
parent 52e800cc59
commit 7e185cd799
2 changed files with 20 additions and 15 deletions

View file

@ -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,

View file

@ -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;
}
}