diff --git a/appinfo/routes.php b/appinfo/routes.php index eba0819..9db09fa 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -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' => '.+']] ] ]; diff --git a/lib/Controller/Errors.php b/lib/Controller/Errors.php new file mode 100644 index 0000000..389d819 --- /dev/null +++ b/lib/Controller/Errors.php @@ -0,0 +1,21 @@ + $e->getMessage()]; + return new DataResponse($message, Http::STATUS_NOT_FOUND); + } + } +} diff --git a/lib/Controller/RadioApiController.php b/lib/Controller/RadioApiController.php new file mode 100644 index 0000000..3a28eb1 --- /dev/null +++ b/lib/Controller/RadioApiController.php @@ -0,0 +1,80 @@ +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); + }); + } +} diff --git a/lib/Controller/RadioController.php b/lib/Controller/RadioController.php new file mode 100644 index 0000000..27d66d7 --- /dev/null +++ b/lib/Controller/RadioController.php @@ -0,0 +1,70 @@ +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); + }); + } +} diff --git a/lib/Db/Radio.php b/lib/Db/Radio.php new file mode 100644 index 0000000..9263c3f --- /dev/null +++ b/lib/Db/Radio.php @@ -0,0 +1,19 @@ + $this->id, + 'station' => $this->station + ]; + } +} diff --git a/lib/Db/RadioMapper.php b/lib/Db/RadioMapper.php new file mode 100644 index 0000000..548a4a3 --- /dev/null +++ b/lib/Db/RadioMapper.php @@ -0,0 +1,45 @@ +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); + } +} diff --git a/lib/Migration/Version000000Date20181013124731.php b/lib/Migration/Version000000Date20181013124731.php new file mode 100644 index 0000000..bf65679 --- /dev/null +++ b/lib/Migration/Version000000Date20181013124731.php @@ -0,0 +1,44 @@ +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; + } +} diff --git a/lib/Service/RadioNotFound.php b/lib/Service/RadioNotFound.php new file mode 100644 index 0000000..87de975 --- /dev/null +++ b/lib/Service/RadioNotFound.php @@ -0,0 +1,6 @@ +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); + } + } +}