mirror of
https://github.com/appwrite/appwrite
synced 2026-05-22 08:28:42 +00:00
update: handle the context logic, use defaults.
This commit is contained in:
parent
0f94b800a5
commit
69cc4f0bb6
16 changed files with 22 additions and 60 deletions
|
|
@ -257,3 +257,13 @@ const RESOURCE_TYPE_PROVIDERS = 'providers';
|
|||
const RESOURCE_TYPE_TOPICS = 'topics';
|
||||
const RESOURCE_TYPE_SUBSCRIBERS = 'subscribers';
|
||||
const RESOURCE_TYPE_MESSAGES = 'messages';
|
||||
|
||||
// Context constants for database
|
||||
|
||||
const DATABASE_ROWS_CONTEXT = 'row';
|
||||
const DATABASE_TABLES_CONTEXT = 'table';
|
||||
const DATABASE_COLUMNS_CONTEXT = 'column';
|
||||
|
||||
const DATABASE_DOCUMENTS_CONTEXT = 'document';
|
||||
const DATABASE_ATTRIBUTES_CONTEXT = 'attribute';
|
||||
const DATABASE_COLLECTIONS_CONTEXT = 'collection';
|
||||
|
|
|
|||
|
|
@ -13,26 +13,21 @@ use Utopia\Platform\Action as UtopiaAction;
|
|||
*/
|
||||
abstract class Action extends UtopiaAction
|
||||
{
|
||||
/**
|
||||
* Valid context identifiers.
|
||||
*/
|
||||
public const TABLE = 'table';
|
||||
public const COLLECTION = 'collection';
|
||||
|
||||
/**
|
||||
* The current API context (either 'table' or 'collection').
|
||||
*/
|
||||
private ?string $context = null;
|
||||
private ?string $context = DATABASE_COLLECTIONS_CONTEXT;
|
||||
|
||||
/**
|
||||
* Set the current API context.
|
||||
*
|
||||
* @param string $context Must be either `self::TABLE` or `self::COLLECTION`.
|
||||
* @param string $context Must be either `DATABASE_TABLES_CONTEXT` or `DATABASE_COLLECTIONS_CONTEXT`.
|
||||
*/
|
||||
final protected function setContext(string $context): void
|
||||
{
|
||||
if (!\in_array($context, [self::TABLE, self::COLLECTION], true)) {
|
||||
throw new \InvalidArgumentException("Invalid context '$context'. Must be either `Action::TABLE` or `Action::COLLECTION`.");
|
||||
if (!\in_array($context, [DATABASE_TABLES_CONTEXT, DATABASE_COLLECTIONS_CONTEXT], true)) {
|
||||
throw new \InvalidArgumentException("Invalid context '$context'. Must be either `DATABASE_TABLES_CONTEXT` or `DATABASE_COLLECTIONS_CONTEXT`.");
|
||||
}
|
||||
|
||||
$this->context = $context;
|
||||
|
|
@ -45,10 +40,6 @@ abstract class Action extends UtopiaAction
|
|||
*/
|
||||
final protected function getContext(): string
|
||||
{
|
||||
if ($this->context === null) {
|
||||
throw new \Exception('Missing context: you must call setContext() with either `Action::TABLE` or `Action::COLLECTION` before using this method.');
|
||||
}
|
||||
|
||||
return $this->context;
|
||||
}
|
||||
|
||||
|
|
@ -70,7 +61,7 @@ abstract class Action extends UtopiaAction
|
|||
*/
|
||||
final protected function isCollectionsAPI(): bool
|
||||
{
|
||||
return $this->getContext() === self::COLLECTION;
|
||||
return $this->getContext() === DATABASE_COLLECTIONS_CONTEXT;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -110,15 +101,4 @@ abstract class Action extends UtopiaAction
|
|||
? Exception::COLLECTION_LIMIT_EXCEEDED
|
||||
: Exception::TABLE_LIMIT_EXCEEDED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that a valid context has been set.
|
||||
*
|
||||
* @throws \Exception if context is missing
|
||||
*/
|
||||
final protected function validateContext(): void
|
||||
{
|
||||
$this->getContext(); // Triggers exception if not set
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,8 +40,6 @@ class Create extends Action
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::COLLECTION);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_POST)
|
||||
->setHttpPath('/v1/databases/:databaseId/collections')
|
||||
|
|
@ -80,8 +78,6 @@ class Create extends Action
|
|||
|
||||
public function action(string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, UtopiaResponse $response, Database $dbForProject, Event $queueForEvents): void
|
||||
{
|
||||
$this->validateContext();
|
||||
|
||||
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
|
||||
|
||||
if ($database->isEmpty()) {
|
||||
|
|
|
|||
|
|
@ -32,8 +32,6 @@ class Delete extends Action
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::COLLECTION);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_DELETE)
|
||||
->setHttpPath('/v1/databases/:databaseId/collections/:collectionId')
|
||||
|
|
@ -69,8 +67,6 @@ class Delete extends Action
|
|||
|
||||
public function action(string $databaseId, string $collectionId, UtopiaResponse $response, Database $dbForProject, EventDatabase $queueForDatabase, Event $queueForEvents): void
|
||||
{
|
||||
$this->validateContext();
|
||||
|
||||
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
|
||||
if ($database->isEmpty()) {
|
||||
throw new Exception(Exception::DATABASE_NOT_FOUND);
|
||||
|
|
|
|||
|
|
@ -30,8 +30,6 @@ class Get extends Action
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::COLLECTION);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_GET)
|
||||
->setHttpPath('/v1/databases/:databaseId/collections/:collectionId')
|
||||
|
|
@ -62,8 +60,6 @@ class Get extends Action
|
|||
|
||||
public function action(string $databaseId, string $collectionId, UtopiaResponse $response, Database $dbForProject): void
|
||||
{
|
||||
$this->validateContext();
|
||||
|
||||
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
|
||||
|
||||
if ($database->isEmpty()) {
|
||||
|
|
|
|||
|
|
@ -42,8 +42,6 @@ class XList extends Action
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::COLLECTION);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_GET)
|
||||
->setHttpPath('/v1/databases/:databaseId/collections/:collectionId/logs')
|
||||
|
|
@ -77,8 +75,6 @@ class XList extends Action
|
|||
|
||||
public function action(string $databaseId, string $collectionId, array $queries, UtopiaResponse $response, Database $dbForProject, Locale $locale, Reader $geodb): void
|
||||
{
|
||||
$this->validateContext();
|
||||
|
||||
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
|
||||
|
||||
if ($database->isEmpty()) {
|
||||
|
|
|
|||
|
|
@ -35,8 +35,6 @@ class Update extends Action
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::COLLECTION);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_PUT)
|
||||
->setHttpPath('/v1/databases/:databaseId/collections/:collectionId')
|
||||
|
|
@ -75,8 +73,6 @@ class Update extends Action
|
|||
|
||||
public function action(string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, UtopiaResponse $response, Database $dbForProject, Event $queueForEvents): void
|
||||
{
|
||||
$this->validateContext();
|
||||
|
||||
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
|
||||
if ($database->isEmpty()) {
|
||||
throw new Exception(Exception::DATABASE_NOT_FOUND);
|
||||
|
|
|
|||
|
|
@ -35,8 +35,6 @@ class Get extends Action
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::COLLECTION);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_GET)
|
||||
->setHttpPath('/v1/databases/:databaseId/collections/:collectionId/usage')
|
||||
|
|
@ -68,8 +66,6 @@ class Get extends Action
|
|||
|
||||
public function action(string $databaseId, string $range, string $collectionId, UtopiaResponse $response, Database $dbForProject): void
|
||||
{
|
||||
$this->validateContext();
|
||||
|
||||
$database = $dbForProject->getDocument('databases', $databaseId);
|
||||
$collectionDocument = $dbForProject->getDocument('database_' . $database->getInternalId(), $collectionId);
|
||||
$collection = $dbForProject->getCollection('database_' . $database->getInternalId() . '_collection_' . $collectionDocument->getInternalId());
|
||||
|
|
|
|||
|
|
@ -36,8 +36,6 @@ class XList extends Action
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::COLLECTION);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_GET)
|
||||
->setHttpPath('/v1/databases/:databaseId/collections')
|
||||
|
|
@ -69,8 +67,6 @@ class XList extends Action
|
|||
|
||||
public function action(string $databaseId, array $queries, string $search, UtopiaResponse $response, Database $dbForProject): void
|
||||
{
|
||||
$this->validateContext();
|
||||
|
||||
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
|
||||
|
||||
if ($database->isEmpty()) {
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ class Create extends CollectionCreate
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::TABLE);
|
||||
$this->setContext(DATABASE_TABLES_CONTEXT);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_POST)
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ class Delete extends CollectionDelete
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::TABLE);
|
||||
$this->setContext(DATABASE_TABLES_CONTEXT);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_DELETE)
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class Get extends CollectionGet
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::TABLE);
|
||||
$this->setContext(DATABASE_TABLES_CONTEXT);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_GET)
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ class XList extends CollectionLogXList
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::TABLE);
|
||||
$this->setContext(DATABASE_TABLES_CONTEXT);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_GET)
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ class Update extends CollectionUpdate
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::TABLE);
|
||||
$this->setContext(DATABASE_TABLES_CONTEXT);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_PUT)
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ class Get extends CollectionUsageGet
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::TABLE);
|
||||
$this->setContext(DATABASE_TABLES_CONTEXT);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_GET)
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ class XList extends CollectionXList
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setContext(Action::TABLE);
|
||||
$this->setContext(DATABASE_TABLES_CONTEXT);
|
||||
|
||||
$this
|
||||
->setHttpMethod(self::HTTP_REQUEST_METHOD_GET)
|
||||
|
|
|
|||
Loading…
Reference in a new issue