1
0
Fork 0
mirror of synced 2024-07-01 20:50:49 +12:00
appwrite/app/controllers/api/platforms.php

146 lines
5.7 KiB
PHP
Raw Normal View History

2019-12-15 08:33:29 +13:00
<?php
2019-12-16 07:56:44 +13:00
global $utopia, $request, $response, $consoleDB, $project;
2019-12-15 08:33:29 +13:00
use Utopia\Exception;
use Utopia\Response;
use Utopia\Validator\Text;
use Utopia\Validator\WhiteList;
use Utopia\Validator\URL;
use Database\Database;
use Database\Document;
use Database\Validator\UID;
2019-12-17 08:35:33 +13:00
include_once __DIR__ . '/../shared/api.php';
2019-12-15 08:33:29 +13:00
2019-12-16 07:56:44 +13:00
$utopia->get('/v1/platforms')
2019-12-15 08:33:29 +13:00
->desc('List Platforms')
2019-12-16 17:55:05 +13:00
->label('scope', 'platforms.read')
->label('sdk.namespace', 'platforms')
2019-12-15 08:33:29 +13:00
->label('sdk.method', 'listPlatforms')
->action(
2019-12-16 07:56:44 +13:00
function () use ($request, $response, $consoleDB, $project) {
$response->json($project->getAttribute('platforms', []));
2019-12-15 08:33:29 +13:00
}
);
2019-12-16 07:56:44 +13:00
$utopia->get('/v1/platforms/:platformId')
2019-12-15 08:33:29 +13:00
->desc('Get Platform')
2019-12-16 17:55:05 +13:00
->label('scope', 'platforms.read')
->label('sdk.namespace', 'platforms')
2019-12-15 08:33:29 +13:00
->label('sdk.method', 'getPlatform')
->param('platformId', null, function () { return new UID(); }, 'Platform unique ID.')
->action(
2019-12-16 07:56:44 +13:00
function ($platformId) use ($request, $response, $consoleDB, $project) {
2019-12-15 08:33:29 +13:00
$platform = $project->search('$uid', $platformId, $project->getAttribute('platforms', []));
if (empty($platform) && $platform instanceof Document) {
throw new Exception('Platform not found', 404);
}
$response->json($platform->getArrayCopy());
}
);
2019-12-16 07:56:44 +13:00
$utopia->post('/v1/platforms')
2019-12-15 08:33:29 +13:00
->desc('Create Platform')
2019-12-16 17:55:05 +13:00
->label('scope', 'platforms.write')
->label('sdk.namespace', 'platforms')
2019-12-15 08:33:29 +13:00
->label('sdk.method', 'createPlatform')
->param('type', null, function () { return new WhiteList(['web', 'ios', 'android', 'unity']); }, 'Platform name')
->param('name', null, function () { return new Text(256); }, 'Platform name')
->param('key', '', function () { return new Text(256); }, 'Package name for android or bundle ID for iOS', true)
->param('store', '', function () { return new Text(256); }, 'App store or Google Play store ID', true)
->param('url', '', function () { return new URL(); }, 'Platform client URL', true)
->action(
2019-12-16 07:56:44 +13:00
function ($type, $name, $key, $store, $url) use ($response, $consoleDB, $project) {
2019-12-15 08:33:29 +13:00
$platform = $consoleDB->createDocument([
'$collection' => Database::SYSTEM_COLLECTION_PLATFORMS,
'$permissions' => [
'read' => ['team:'.$project->getAttribute('teamId', null)],
'write' => ['team:'.$project->getAttribute('teamId', null).'/owner', 'team:'.$project->getAttribute('teamId', null).'/developer'],
],
'type' => $type,
'name' => $name,
'key' => $key,
'store' => $store,
'url' => $url,
'dateCreated' => time(),
'dateUpdated' => time(),
]);
if (false === $platform) {
throw new Exception('Failed saving platform to DB', 500);
}
$project->setAttribute('platforms', $platform, Document::SET_TYPE_APPEND);
$project = $consoleDB->updateDocument($project->getArrayCopy());
if (false === $project) {
throw new Exception('Failed saving project to DB', 500);
}
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->json($platform->getArrayCopy())
;
}
);
2019-12-16 07:56:44 +13:00
$utopia->put('/v1/platforms/:platformId')
2019-12-15 08:33:29 +13:00
->desc('Update Platform')
2019-12-16 17:55:05 +13:00
->label('scope', 'platforms.write')
->label('sdk.namespace', 'platforms')
2019-12-15 08:33:29 +13:00
->label('sdk.method', 'updatePlatform')
->param('platformId', null, function () { return new UID(); }, 'Platform unique ID.')
->param('name', null, function () { return new Text(256); }, 'Platform name')
->param('key', '', function () { return new Text(256); }, 'Package name for android or bundle ID for iOS', true)
->param('store', '', function () { return new Text(256); }, 'App store or Google Play store ID', true)
->param('url', '', function () { return new URL(); }, 'Platform client URL', true)
->action(
2019-12-16 07:56:44 +13:00
function ($platformId, $name, $key, $store, $url) use ($response, $consoleDB, $project) {
2019-12-15 08:33:29 +13:00
$platform = $project->search('$uid', $platformId, $project->getAttribute('platforms', []));
if (empty($platform) && $platform instanceof Document) {
throw new Exception('Platform not found', 404);
}
$platform
->setAttribute('name', $name)
->setAttribute('dateUpdated', time())
->setAttribute('key', $key)
->setAttribute('store', $store)
->setAttribute('url', $url)
;
if (false === $consoleDB->updateDocument($platform->getArrayCopy())) {
throw new Exception('Failed saving platform to DB', 500);
}
$response->json($platform->getArrayCopy());
}
);
2019-12-16 07:56:44 +13:00
$utopia->delete('/v1/platforms/:platformId')
2019-12-15 08:33:29 +13:00
->desc('Delete Platform')
2019-12-16 17:55:05 +13:00
->label('scope', 'platforms.write')
->label('sdk.namespace', 'platforms')
2019-12-15 08:33:29 +13:00
->label('sdk.method', 'deletePlatform')
->param('platformId', null, function () { return new UID(); }, 'Platform unique ID.')
->action(
2019-12-16 07:56:44 +13:00
function ($platformId) use ($response, $consoleDB, $project) {
2019-12-15 08:33:29 +13:00
$platform = $project->search('$uid', $platformId, $project->getAttribute('platforms', []));
if (empty($platform) && $platform instanceof Document) {
throw new Exception('Platform not found', 404);
}
if (!$consoleDB->deleteDocument($platform->getUid())) {
throw new Exception('Failed to remove platform from DB', 500);
}
$response->noContent();
}
);