diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index efcc2162a5..a582d56914 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -1,15 +1,16 @@ setStatusCode(Response::STATUS_CODE_CREATED) - ->json($function->getArrayCopy()) - ; + $response->setStatusCode(Response::STATUS_CODE_CREATED); + $response->dynamic($function, Response::MODEL_FUNCTION); }, ['response', 'projectDB']); App::get('/v1/functions') @@ -90,7 +89,10 @@ App::get('/v1/functions') ], ]); - $response->json(['sum' => $projectDB->getSum(), 'functions' => $results]); + $response->dynamic(new Document([ + 'sum' => $projectDB->getSum(), + 'functions' => $results + ]), Response::MODEL_FUNCTION_LIST); }, ['response', 'projectDB']); App::get('/v1/functions/:functionId') @@ -109,7 +111,7 @@ App::get('/v1/functions/:functionId') throw new Exception('Function not found', 404); } - $response->json($function->getArrayCopy()); + $response->dynamic($function, Response::MODEL_FUNCTION); }, ['response', 'projectDB']); App::get('/v1/functions/:functionId/usage') @@ -264,7 +266,7 @@ App::put('/v1/functions/:functionId') throw new Exception('Failed saving function to DB', 500); } - $response->json($function->getArrayCopy()); + $response->dynamic($function, Response::MODEL_FUNCTION); }, ['response', 'projectDB']); App::patch('/v1/functions/:functionId/tag') @@ -302,7 +304,7 @@ App::patch('/v1/functions/:functionId/tag') throw new Exception('Failed saving function to DB', 500); } - $response->json($function->getArrayCopy()); + $response->dynamic($function, Response::MODEL_FUNCTION); }, ['response', 'projectDB']); App::delete('/v1/functions/:functionId') @@ -414,10 +416,8 @@ App::post('/v1/functions/:functionId/tags') ->setParam('storage', $tag->getAttribute('codeSize', 0)) ; - $response - ->setStatusCode(Response::STATUS_CODE_CREATED) - ->json($tag->getArrayCopy()) - ; + $response->setStatusCode(Response::STATUS_CODE_CREATED); + $response->dynamic($tag, Response::MODEL_TAG); }, ['request', 'response', 'projectDB', 'usage']); App::get('/v1/functions/:functionId/tags') @@ -453,7 +453,10 @@ App::get('/v1/functions/:functionId/tags') ], ]); - $response->json(['sum' => $projectDB->getSum(), 'tags' => $results]); + $response->dynamic(new Document([ + 'sum' => $projectDB->getSum(), + 'tags' => $results + ]), Response::MODEL_TAG_LIST); }, ['response', 'projectDB']); App::get('/v1/functions/:functionId/tags/:tagId') @@ -483,7 +486,7 @@ App::get('/v1/functions/:functionId/tags/:tagId') throw new Exception('Tag not found', 404); } - $response->json($tag->getArrayCopy()); + $response->dynamic($tag, Response::MODEL_TAG); }, ['response', 'projectDB']); App::delete('/v1/functions/:functionId/tags/:tagId') @@ -597,10 +600,8 @@ App::post('/v1/functions/:functionId/executions') 'trigger' => 'http', ]); - $response - ->setStatusCode(Response::STATUS_CODE_CREATED) - ->json($execution->getArrayCopy()) - ; + $response->setStatusCode(Response::STATUS_CODE_CREATED); + $response->dynamic($execution, Response::MODEL_EXECUTION); }, ['response', 'project', 'projectDB']); App::get('/v1/functions/:functionId/executions') @@ -636,7 +637,10 @@ App::get('/v1/functions/:functionId/executions') ], ]); - $response->json(['sum' => $projectDB->getSum(), 'executions' => $results]); + $response->dynamic(new Document([ + 'sum' => $projectDB->getSum(), + 'executions' => $results + ]), Response::MODEL_EXECUTION_LIST); }, ['response', 'projectDB']); App::get('/v1/functions/:functionId/executions/:executionId') @@ -666,5 +670,5 @@ App::get('/v1/functions/:functionId/executions/:executionId') throw new Exception('Execution not found', 404); } - $response->json($execution->getArrayCopy()); + $response->dynamic($execution, Response::MODEL_EXECUTION); }, ['response', 'projectDB']); \ No newline at end of file diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 53065df939..f3fb049b27 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -5,6 +5,7 @@ namespace Appwrite\Utopia; use Exception; use Appwrite\Database\Document; use Appwrite\Utopia\Response\Model; +use Appwrite\Utopia\Response\Model\BaseList; use Appwrite\Utopia\Response\Model\Error; use Appwrite\Utopia\Response\Model\ErrorDev; use Appwrite\Utopia\Response\Model\File; @@ -69,18 +70,24 @@ class Response extends UtopiaResponse public function __construct(int $time = 0) { $this + // General ->setModel(new Error()) ->setModel(new ErrorDev()) + // Lists + ->setModel(new BaseList('Files List', self::MODEL_FILE_LIST, 'files', self::MODEL_FILE)) + ->setModel(new BaseList('Teams List', self::MODEL_TEAM_LIST, 'teams', self::MODEL_TEAM)) + ->setModel(new BaseList('Memberships List', self::MODEL_MEMBERSHIP_LIST, 'memberships', self::MODEL_MEMBERSHIP)) + ->setModel(new BaseList('Functions List', self::MODEL_FUNCTION_LIST, 'functions', self::MODEL_FUNCTION)) + ->setModel(new BaseList('Tags List', self::MODEL_TAG_LIST, 'tags', self::MODEL_TAG)) + ->setModel(new BaseList('Executions List', self::MODEL_EXECUTION_LIST, 'executions', self::MODEL_EXECUTION)) + // Entities ->setModel(new User()) ->setModel(new Session()) ->setModel(new Locale()) ->setModel(new File()) - ->setModel(new FileList()) ->setModel(new Team()) - ->setModel(new TeamList()) ->setModel(new Membership()) - ->setModel(new MembershipList()) - ->setModel(new Functionx()) + ->setModel(new Func()) ; parent::__construct($time); diff --git a/src/Appwrite/Utopia/Response/Model/BaseList.php b/src/Appwrite/Utopia/Response/Model/BaseList.php index 24f0a76486..edf0545573 100644 --- a/src/Appwrite/Utopia/Response/Model/BaseList.php +++ b/src/Appwrite/Utopia/Response/Model/BaseList.php @@ -5,16 +5,35 @@ namespace Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model; -abstract class BaseList extends Model +class BaseList extends Model { - public function __construct() + /** + * @var string + */ + protected $name = ''; + + /** + * @var string + */ + protected $type = ''; + + public function __construct(string $name, string $type, string $key, string $model) { + $this->name = $name; + $this->type = $type; + $this ->addRule('sum', [ 'type' => 'integer', 'description' => 'Total sum of items in the list.', 'example' => '5', ]) + ->addRule($key, [ + 'type' => $model, + 'description' => 'List of '.$key.'.', + 'example' => [], + 'array' => true, + ]) ; } @@ -25,7 +44,7 @@ abstract class BaseList extends Model */ public function getName():string { - return 'Base List'; + return $this->name; } /** @@ -35,6 +54,6 @@ abstract class BaseList extends Model */ public function getType():string { - return Response::MODEL_BASE_LIST; + return $this->type; } } \ No newline at end of file diff --git a/src/Appwrite/Utopia/Response/Model/ExecutionList.php b/src/Appwrite/Utopia/Response/Model/ExecutionList.php deleted file mode 100644 index a36dab6505..0000000000 --- a/src/Appwrite/Utopia/Response/Model/ExecutionList.php +++ /dev/null @@ -1,41 +0,0 @@ -addRule('executions', [ - 'type' => Response::MODEL_EXECUTION, - 'description' => 'List of function execitions.', - 'example' => [], - 'array' => true, - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName():string - { - return 'Executions List'; - } - - /** - * Get Collection - * - * @return string - */ - public function getType():string - { - return Response::MODEL_EXECUTION_LIST; - } -} \ No newline at end of file diff --git a/src/Appwrite/Utopia/Response/Model/FileList.php b/src/Appwrite/Utopia/Response/Model/FileList.php deleted file mode 100644 index 731ca31628..0000000000 --- a/src/Appwrite/Utopia/Response/Model/FileList.php +++ /dev/null @@ -1,41 +0,0 @@ -addRule('files', [ - 'type' => Response::MODEL_FILE, - 'description' => 'List of files.', - 'example' => [], - 'array' => true, - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName():string - { - return 'Files List'; - } - - /** - * Get Collection - * - * @return string - */ - public function getType():string - { - return Response::MODEL_FILE_LIST; - } -} \ No newline at end of file diff --git a/src/Appwrite/Utopia/Response/Model/Functionx.php b/src/Appwrite/Utopia/Response/Model/Func.php similarity index 99% rename from src/Appwrite/Utopia/Response/Model/Functionx.php rename to src/Appwrite/Utopia/Response/Model/Func.php index 01e9de4e49..38596892fe 100644 --- a/src/Appwrite/Utopia/Response/Model/Functionx.php +++ b/src/Appwrite/Utopia/Response/Model/Func.php @@ -5,7 +5,7 @@ namespace Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model; -class Functionx extends Model +class Func extends Model { public function __construct() { diff --git a/src/Appwrite/Utopia/Response/Model/FunctionxList.php b/src/Appwrite/Utopia/Response/Model/FunctionxList.php deleted file mode 100644 index 237353ee48..0000000000 --- a/src/Appwrite/Utopia/Response/Model/FunctionxList.php +++ /dev/null @@ -1,41 +0,0 @@ -addRule('functions', [ - 'type' => Response::MODEL_MEMBERSHIP, - 'description' => 'List of functions.', - 'example' => [], - 'array' => true, - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName():string - { - return 'Functions List'; - } - - /** - * Get Collection - * - * @return string - */ - public function getType():string - { - return Response::MODEL_FUNCTION_LIST; - } -} \ No newline at end of file diff --git a/src/Appwrite/Utopia/Response/Model/MembershipList.php b/src/Appwrite/Utopia/Response/Model/MembershipList.php deleted file mode 100644 index 2b9934acf4..0000000000 --- a/src/Appwrite/Utopia/Response/Model/MembershipList.php +++ /dev/null @@ -1,41 +0,0 @@ -addRule('memberships', [ - 'type' => Response::MODEL_MEMBERSHIP, - 'description' => 'List of memberships.', - 'example' => [], - 'array' => true, - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName():string - { - return 'Memberships List'; - } - - /** - * Get Collection - * - * @return string - */ - public function getType():string - { - return Response::MODEL_MEMBERSHIP_LIST; - } -} \ No newline at end of file diff --git a/src/Appwrite/Utopia/Response/Model/TagList.php b/src/Appwrite/Utopia/Response/Model/TagList.php deleted file mode 100644 index 838452bef9..0000000000 --- a/src/Appwrite/Utopia/Response/Model/TagList.php +++ /dev/null @@ -1,41 +0,0 @@ -addRule('tags', [ - 'type' => Response::MODEL_TAG, - 'description' => 'List of tags.', - 'example' => [], - 'array' => true, - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName():string - { - return 'Tags List'; - } - - /** - * Get Collection - * - * @return string - */ - public function getType():string - { - return Response::MODEL_TAG_LIST; - } -} \ No newline at end of file diff --git a/src/Appwrite/Utopia/Response/Model/TeamList.php b/src/Appwrite/Utopia/Response/Model/TeamList.php deleted file mode 100644 index b5d35bc0a6..0000000000 --- a/src/Appwrite/Utopia/Response/Model/TeamList.php +++ /dev/null @@ -1,41 +0,0 @@ -addRule('teams', [ - 'type' => Response::MODEL_TEAM, - 'description' => 'List of teams.', - 'example' => [], - 'array' => true, - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName():string - { - return 'Teams List'; - } - - /** - * Get Collection - * - * @return string - */ - public function getType():string - { - return Response::MODEL_TEAM_LIST; - } -} \ No newline at end of file