1
0
Fork 0
mirror of synced 2024-10-01 09:47:43 +13:00

Updated response models

This commit is contained in:
Eldad Fux 2020-08-07 09:33:49 +03:00
parent 7ebc6a5de9
commit 5b34a1ea7e
10 changed files with 60 additions and 276 deletions

View file

@ -1,15 +1,16 @@
<?php <?php
use Appwrite\Database\Database; use Appwrite\Database\Database;
use Appwrite\Database\Document;
use Appwrite\Database\Validator\UID; use Appwrite\Database\Validator\UID;
use Appwrite\Storage\Storage; use Appwrite\Storage\Storage;
use Appwrite\Storage\Validator\File; use Appwrite\Storage\Validator\File;
use Appwrite\Storage\Validator\FileSize; use Appwrite\Storage\Validator\FileSize;
use Appwrite\Storage\Validator\FileType; use Appwrite\Storage\Validator\FileType;
use Appwrite\Storage\Validator\Upload; use Appwrite\Storage\Validator\Upload;
use Appwrite\Swoole\Response;
use Appwrite\Task\Validator\Cron; use Appwrite\Task\Validator\Cron;
use Utopia\App; use Utopia\App;
use Utopia\Response;
use Utopia\Validator\ArrayList; use Utopia\Validator\ArrayList;
use Utopia\Validator\Assoc; use Utopia\Validator\Assoc;
use Utopia\Validator\Text; use Utopia\Validator\Text;
@ -59,10 +60,8 @@ App::post('/v1/functions')
throw new Exception('Failed saving function to DB', 500); throw new Exception('Failed saving function to DB', 500);
} }
$response $response->setStatusCode(Response::STATUS_CODE_CREATED);
->setStatusCode(Response::STATUS_CODE_CREATED) $response->dynamic($function, Response::MODEL_FUNCTION);
->json($function->getArrayCopy())
;
}, ['response', 'projectDB']); }, ['response', 'projectDB']);
App::get('/v1/functions') 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']); }, ['response', 'projectDB']);
App::get('/v1/functions/:functionId') App::get('/v1/functions/:functionId')
@ -109,7 +111,7 @@ App::get('/v1/functions/:functionId')
throw new Exception('Function not found', 404); throw new Exception('Function not found', 404);
} }
$response->json($function->getArrayCopy()); $response->dynamic($function, Response::MODEL_FUNCTION);
}, ['response', 'projectDB']); }, ['response', 'projectDB']);
App::get('/v1/functions/:functionId/usage') App::get('/v1/functions/:functionId/usage')
@ -264,7 +266,7 @@ App::put('/v1/functions/:functionId')
throw new Exception('Failed saving function to DB', 500); throw new Exception('Failed saving function to DB', 500);
} }
$response->json($function->getArrayCopy()); $response->dynamic($function, Response::MODEL_FUNCTION);
}, ['response', 'projectDB']); }, ['response', 'projectDB']);
App::patch('/v1/functions/:functionId/tag') 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); throw new Exception('Failed saving function to DB', 500);
} }
$response->json($function->getArrayCopy()); $response->dynamic($function, Response::MODEL_FUNCTION);
}, ['response', 'projectDB']); }, ['response', 'projectDB']);
App::delete('/v1/functions/:functionId') App::delete('/v1/functions/:functionId')
@ -414,10 +416,8 @@ App::post('/v1/functions/:functionId/tags')
->setParam('storage', $tag->getAttribute('codeSize', 0)) ->setParam('storage', $tag->getAttribute('codeSize', 0))
; ;
$response $response->setStatusCode(Response::STATUS_CODE_CREATED);
->setStatusCode(Response::STATUS_CODE_CREATED) $response->dynamic($tag, Response::MODEL_TAG);
->json($tag->getArrayCopy())
;
}, ['request', 'response', 'projectDB', 'usage']); }, ['request', 'response', 'projectDB', 'usage']);
App::get('/v1/functions/:functionId/tags') 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']); }, ['response', 'projectDB']);
App::get('/v1/functions/:functionId/tags/:tagId') 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); throw new Exception('Tag not found', 404);
} }
$response->json($tag->getArrayCopy()); $response->dynamic($tag, Response::MODEL_TAG);
}, ['response', 'projectDB']); }, ['response', 'projectDB']);
App::delete('/v1/functions/:functionId/tags/:tagId') App::delete('/v1/functions/:functionId/tags/:tagId')
@ -597,10 +600,8 @@ App::post('/v1/functions/:functionId/executions')
'trigger' => 'http', 'trigger' => 'http',
]); ]);
$response $response->setStatusCode(Response::STATUS_CODE_CREATED);
->setStatusCode(Response::STATUS_CODE_CREATED) $response->dynamic($execution, Response::MODEL_EXECUTION);
->json($execution->getArrayCopy())
;
}, ['response', 'project', 'projectDB']); }, ['response', 'project', 'projectDB']);
App::get('/v1/functions/:functionId/executions') 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']); }, ['response', 'projectDB']);
App::get('/v1/functions/:functionId/executions/:executionId') 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); throw new Exception('Execution not found', 404);
} }
$response->json($execution->getArrayCopy()); $response->dynamic($execution, Response::MODEL_EXECUTION);
}, ['response', 'projectDB']); }, ['response', 'projectDB']);

View file

@ -5,6 +5,7 @@ namespace Appwrite\Utopia;
use Exception; use Exception;
use Appwrite\Database\Document; use Appwrite\Database\Document;
use Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response\Model\BaseList;
use Appwrite\Utopia\Response\Model\Error; use Appwrite\Utopia\Response\Model\Error;
use Appwrite\Utopia\Response\Model\ErrorDev; use Appwrite\Utopia\Response\Model\ErrorDev;
use Appwrite\Utopia\Response\Model\File; use Appwrite\Utopia\Response\Model\File;
@ -69,18 +70,24 @@ class Response extends UtopiaResponse
public function __construct(int $time = 0) public function __construct(int $time = 0)
{ {
$this $this
// General
->setModel(new Error()) ->setModel(new Error())
->setModel(new ErrorDev()) ->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 User())
->setModel(new Session()) ->setModel(new Session())
->setModel(new Locale()) ->setModel(new Locale())
->setModel(new File()) ->setModel(new File())
->setModel(new FileList())
->setModel(new Team()) ->setModel(new Team())
->setModel(new TeamList())
->setModel(new Membership()) ->setModel(new Membership())
->setModel(new MembershipList()) ->setModel(new Func())
->setModel(new Functionx())
; ;
parent::__construct($time); parent::__construct($time);

View file

@ -5,16 +5,35 @@ namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response; use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model; 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 $this
->addRule('sum', [ ->addRule('sum', [
'type' => 'integer', 'type' => 'integer',
'description' => 'Total sum of items in the list.', 'description' => 'Total sum of items in the list.',
'example' => '5', '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 public function getName():string
{ {
return 'Base List'; return $this->name;
} }
/** /**
@ -35,6 +54,6 @@ abstract class BaseList extends Model
*/ */
public function getType():string public function getType():string
{ {
return Response::MODEL_BASE_LIST; return $this->type;
} }
} }

View file

@ -1,41 +0,0 @@
<?php
namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response;
class ExecutionList extends BaseList
{
public function __construct()
{
parent::__construct();
$this
->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;
}
}

View file

@ -1,41 +0,0 @@
<?php
namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response;
class FileList extends BaseList
{
public function __construct()
{
parent::__construct();
$this
->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;
}
}

View file

@ -5,7 +5,7 @@ namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response; use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response\Model;
class Functionx extends Model class Func extends Model
{ {
public function __construct() public function __construct()
{ {

View file

@ -1,41 +0,0 @@
<?php
namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response;
class FunctionxList extends BaseList
{
public function __construct()
{
parent::__construct();
$this
->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;
}
}

View file

@ -1,41 +0,0 @@
<?php
namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response;
class MembershipList extends BaseList
{
public function __construct()
{
parent::__construct();
$this
->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;
}
}

View file

@ -1,41 +0,0 @@
<?php
namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response;
class TagList extends BaseList
{
public function __construct()
{
parent::__construct();
$this
->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;
}
}

View file

@ -1,41 +0,0 @@
<?php
namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response;
class TeamList extends BaseList
{
public function __construct()
{
parent::__construct();
$this
->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;
}
}