From e41e35986522139baee48c9f6e03230b568fa1d0 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Mon, 19 Oct 2020 20:41:09 +0200 Subject: [PATCH] add database management --- appinfo/routes.php | 7 +- lib/Controller/Errors.php | 21 +++++ lib/Controller/RadioApiController.php | 80 +++++++++++++++++++ lib/Controller/RadioController.php | 70 ++++++++++++++++ lib/Db/Radio.php | 19 +++++ lib/Db/RadioMapper.php | 45 +++++++++++ .../Version000000Date20181013124731.php | 44 ++++++++++ lib/Service/RadioNotFound.php | 6 ++ lib/Service/RadioService.php | 74 +++++++++++++++++ 9 files changed, 365 insertions(+), 1 deletion(-) create mode 100644 lib/Controller/Errors.php create mode 100644 lib/Controller/RadioApiController.php create mode 100644 lib/Controller/RadioController.php create mode 100644 lib/Db/Radio.php create mode 100644 lib/Db/RadioMapper.php create mode 100644 lib/Migration/Version000000Date20181013124731.php create mode 100644 lib/Service/RadioNotFound.php create mode 100644 lib/Service/RadioService.php 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); + } + } +}