diff --git a/lib/Controller/StationApiController.php b/lib/Controller/StationApiController.php new file mode 100644 index 0000000..0dde685 --- /dev/null +++ b/lib/Controller/StationApiController.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($title, $content, + $this->userId)); + } + + /** + * @CORS + * @NoCSRFRequired + * @NoAdminRequired + */ + public function update(int $id, string $title, + string $content): DataResponse { + return $this->handleNotFound(function () use ($id, $title, $content) { + return $this->service->update($id, $title, $content, $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/StationController.php b/lib/Controller/StationController.php new file mode 100644 index 0000000..ed19d64 --- /dev/null +++ b/lib/Controller/StationController.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($title, $content, + $this->userId)); + } + + /** + * @NoAdminRequired + */ + public function update(int $id, string $title, + string $content): DataResponse { + return $this->handleNotFound(function () use ($id, $title, $content) { + return $this->service->update($id, $title, $content, $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/Station.php b/lib/Db/Station.php new file mode 100644 index 0000000..601f845 --- /dev/null +++ b/lib/Db/Station.php @@ -0,0 +1,21 @@ + $this->id, + 'title' => $this->title, + 'content' => $this->content + ]; + } +} diff --git a/lib/Db/StationMapper.php b/lib/Db/StationMapper.php new file mode 100644 index 0000000..8ca37b4 --- /dev/null +++ b/lib/Db/StationMapper.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/Service/StationNotFound.php b/lib/Service/StationNotFound.php new file mode 100644 index 0000000..eaaa652 --- /dev/null +++ b/lib/Service/StationNotFound.php @@ -0,0 +1,6 @@ +hasTable('radio')) { + $table = $schema->createTable('radio'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + ]); + $table->addColumn('title', 'string', [ + 'notnull' => true, + 'length' => 200 + ]); + $table->addColumn('user_id', 'string', [ + 'notnull' => true, + 'length' => 200, + ]); + $table->addColumn('content', 'text', [ + 'notnull' => true, + 'default' => '' + ]); + + $table->setPrimaryKey(['id']); + $table->addIndex(['user_id'], 'radio_user_id_index'); + } + return $schema; + } +}