add database management

This commit is contained in:
Jonas Heinrich 2020-10-19 20:41:09 +02:00
parent 97d39c0406
commit e41e359865
9 changed files with 365 additions and 1 deletions

View File

@ -22,8 +22,13 @@
*/
return [
'resources' => [
'radio' => ['url' => '/radio'],
'radio_api' => ['url' => '/api/0.1/radio']
],
'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'page#mail', 'url' => '/mail', 'verb' => 'GET'],
['name' => 'radio_api#preflighted_cors', 'url' => '/api/0.1/{path}',
'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']]
]
];

21
lib/Controller/Errors.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace OCA\Radio\Controller;
use Closure;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCA\Radio\Service\RadioNotFound;
trait Errors {
protected function handleNotFound(Closure $callback): DataResponse {
try {
return new DataResponse($callback());
} catch (RadioNotFound $e) {
$message = ['message' => $e->getMessage()];
return new DataResponse($message, Http::STATUS_NOT_FOUND);
}
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace OCA\Radio\Controller;
use OCA\Radio\AppInfo\Application;
use OCA\Radio\Service\RadioService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
class RadioApiController extends ApiController {
/** @var RadioService */
private $service;
/** @var string */
private $userId;
use Errors;
public function __construct(IRequest $request,
RadioService $service,
$userId) {
parent::__construct(Application::APP_ID, $request);
$this->service = $service;
$this->userId = $userId;
}
/**
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
*/
public function index(): DataResponse {
return new DataResponse($this->service->findAll($this->userId));
}
/**
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
*/
public function show(int $id): DataResponse {
return $this->handleNotFound(function () use ($id) {
return $this->service->find($id, $this->userId);
});
}
/**
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
*/
public function create(string $title, string $content): DataResponse {
return new DataResponse($this->service->create($station,
$this->userId));
}
/**
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
*/
public function update(int $id, string $title,
string $content): DataResponse {
return $this->handleNotFound(function () use ($id, $station) {
return $this->service->update($id, $station, $this->userId);
});
}
/**
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
*/
public function destroy(int $id): DataResponse {
return $this->handleNotFound(function () use ($id) {
return $this->service->delete($id, $this->userId);
});
}
}

View File

@ -0,0 +1,70 @@
<?php
namespace OCA\Radio\Controller;
use OCA\Radio\AppInfo\Application;
use OCA\Radio\Service\RadioService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
class RadioController extends Controller {
/** @var RadioService */
private $service;
/** @var string */
private $userId;
use Errors;
public function __construct(IRequest $request,
RadioService $service,
$userId) {
parent::__construct(Application::APP_ID, $request);
$this->service = $service;
$this->userId = $userId;
}
/**
* @NoAdminRequired
*/
public function index(): DataResponse {
return new DataResponse($this->service->findAll($this->userId));
}
/**
* @NoAdminRequired
*/
public function show(int $id): DataResponse {
return $this->handleNotFound(function () use ($id) {
return $this->service->find($id, $this->userId);
});
}
/**
* @NoAdminRequired
*/
public function create(string $title, string $content): DataResponse {
return new DataResponse($this->service->create($station,
$this->userId));
}
/**
* @NoAdminRequired
*/
public function update(int $id, string $title,
string $content): DataResponse {
return $this->handleNotFound(function () use ($id, $station) {
return $this->service->update($id, $station, $this->userId);
});
}
/**
* @NoAdminRequired
*/
public function destroy(int $id): DataResponse {
return $this->handleNotFound(function () use ($id) {
return $this->service->delete($id, $this->userId);
});
}
}

19
lib/Db/Radio.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace OCA\Radio\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class Note extends Entity implements JsonSerializable {
protected $station
protected $userId;
public function jsonSerialize(): array {
return [
'id' => $this->id,
'station' => $this->station
];
}
}

45
lib/Db/RadioMapper.php Normal file
View File

@ -0,0 +1,45 @@
<?php
namespace OCA\Radio\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
class RadioMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'radio', Radio::class);
}
/**
* @param int $id
* @param string $userId
* @return Entity|Radio
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws DoesNotExistException
*/
public function find(int $id, string $userId): Radio {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('radio')
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
return $this->findEntity($qb);
}
/**
* @param string $userId
* @return array
*/
public function findAll(string $userId): array {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('radio')
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
return $this->findEntities($qb);
}
}

View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace OCA\Radio\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
class Version000000Date20181013124731 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
if (!$schema->hasTable('radio')) {
$table = $schema->createTable('radio');
$table->addColumn('id', 'integer', [
'autoincrement' => true,
'notnull' => true,
]);
$table->addColumn('station', 'string', [
'notnull' => true,
'length' => 200
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 200,
]);
$table->setPrimaryKey(['id']);
$table->addIndex(['user_id'], 'radio_user_id_index');
}
return $schema;
}
}

View File

@ -0,0 +1,6 @@
<?php
namespace OCA\Radio\Service;
class RadioNotFound extends \Exception {
}

View File

@ -0,0 +1,74 @@
<?php
namespace OCA\Radio\Service;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCA\Radio\Db\Radio;
use OCA\Radio\Db\RadioMapper;
class RadioService {
/** @var RadioMapper */
private $mapper;
public function __construct(RadioMapper $mapper) {
$this->mapper = $mapper;
}
public function findAll(string $userId): array {
return $this->mapper->findAll($userId);
}
private function handleException(Exception $e): void {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new RadioNotFound($e->getMessage());
} else {
throw $e;
}
}
public function find($id, $userId) {
try {
return $this->mapper->find($id, $userId);
// in order to be able to plug in different storage backends like files
// for instance it is a good idea to turn storage related exceptions
// into service related exceptions so controllers and service users
// have to deal with only one type of exception
} catch (Exception $e) {
$this->handleException($e);
}
}
public function create($station, $userId) {
$radio = new Radio();
$radio->setStation($station);
$radio->setUserId($userId);
return $this->mapper->insert($radio);
}
public function update($id, $station, $userId) {
try {
$radio = $this->mapper->find($id, $userId);
$radio->setStation($station);
return $this->mapper->update($radio);
} catch (Exception $e) {
$this->handleException($e);
}
}
public function delete($id, $userId) {
try {
$radio = $this->mapper->find($id, $userId);
$this->mapper->delete($radio);
return $radio;
} catch (Exception $e) {
$this->handleException($e);
}
}
}