From 4a6a3ac8ae0aac649daf5a7a4da4d5263c282982 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Fri, 30 Oct 2020 21:16:02 +0100 Subject: [PATCH] trying to implement database backend --- appinfo/routes.php | 6 +- lib/Controller/Errors.php | 4 +- lib/Controller/RadioApiController.php | 80 ------------------- lib/Controller/RadioController.php | 70 ---------------- lib/Db/Radio.php | 19 ----- lib/Db/RadioMapper.php | 45 ----------- .../Version000000Date20181013124731.php | 6 +- lib/Service/RadioNotFound.php | 6 -- lib/Service/RadioService.php | 74 ----------------- src/components/Main.vue | 8 +- src/router.js | 2 + 11 files changed, 18 insertions(+), 302 deletions(-) delete mode 100644 lib/Controller/RadioApiController.php delete mode 100644 lib/Controller/RadioController.php delete mode 100644 lib/Db/Radio.php delete mode 100644 lib/Db/RadioMapper.php delete mode 100644 lib/Service/RadioNotFound.php delete mode 100644 lib/Service/RadioService.php diff --git a/appinfo/routes.php b/appinfo/routes.php index d6f1963..6affc74 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -23,8 +23,8 @@ return [ 'resources' => [ - 'radio' => ['url' => '/radio'], - 'radio_api' => ['url' => '/api/0.1/radio'] + 'station' => ['url' => '/stations'], + 'station_api' => ['url' => '/api/0.1/stations'] ], 'routes' => [ // Web page templates @@ -40,7 +40,7 @@ return [ ['name' => 'settings#set_volume_state', 'url' => '/settings/volumeState', 'verb' => 'POST'], ['name' => 'settings#get_volume_state', 'url' => '/settings/volumeState', 'verb' => 'GET'], // Api - ['name' => 'radio_api#preflighted_cors', 'url' => '/api/0.1/{path}', + ['name' => 'station_api#preflighted_cors', 'url' => '/api/0.1/{path}', 'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']] ] ]; diff --git a/lib/Controller/Errors.php b/lib/Controller/Errors.php index 389d819..253d549 100644 --- a/lib/Controller/Errors.php +++ b/lib/Controller/Errors.php @@ -7,13 +7,13 @@ use Closure; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; -use OCA\Radio\Service\RadioNotFound; +use OCA\Radio\Service\StationNotFound; trait Errors { protected function handleNotFound(Closure $callback): DataResponse { try { return new DataResponse($callback()); - } catch (RadioNotFound $e) { + } catch (StationNotFound $e) { $message = ['message' => $e->getMessage()]; return new DataResponse($message, Http::STATUS_NOT_FOUND); } diff --git a/lib/Controller/RadioApiController.php b/lib/Controller/RadioApiController.php deleted file mode 100644 index 3a28eb1..0000000 --- a/lib/Controller/RadioApiController.php +++ /dev/null @@ -1,80 +0,0 @@ -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 deleted file mode 100644 index 27d66d7..0000000 --- a/lib/Controller/RadioController.php +++ /dev/null @@ -1,70 +0,0 @@ -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 deleted file mode 100644 index 9263c3f..0000000 --- a/lib/Db/Radio.php +++ /dev/null @@ -1,19 +0,0 @@ - $this->id, - 'station' => $this->station - ]; - } -} diff --git a/lib/Db/RadioMapper.php b/lib/Db/RadioMapper.php deleted file mode 100644 index 548a4a3..0000000 --- a/lib/Db/RadioMapper.php +++ /dev/null @@ -1,45 +0,0 @@ -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 index bf65679..28d2b4a 100644 --- a/lib/Migration/Version000000Date20181013124731.php +++ b/lib/Migration/Version000000Date20181013124731.php @@ -27,7 +27,7 @@ class Version000000Date20181013124731 extends SimpleMigrationStep { 'autoincrement' => true, 'notnull' => true, ]); - $table->addColumn('station', 'string', [ + $table->addColumn('title', 'string', [ 'notnull' => true, 'length' => 200 ]); @@ -35,6 +35,10 @@ class Version000000Date20181013124731 extends SimpleMigrationStep { 'notnull' => true, 'length' => 200, ]); + $table->addColumn('content', 'text', [ + 'notnull' => true, + 'default' => '' + ]); $table->setPrimaryKey(['id']); $table->addIndex(['user_id'], 'radio_user_id_index'); diff --git a/lib/Service/RadioNotFound.php b/lib/Service/RadioNotFound.php deleted file mode 100644 index 87de975..0000000 --- a/lib/Service/RadioNotFound.php +++ /dev/null @@ -1,6 +0,0 @@ -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); - } - } -} diff --git a/src/components/Main.vue b/src/components/Main.vue index 35bffbb..00c97cf 100644 --- a/src/components/Main.vue +++ b/src/components/Main.vue @@ -85,7 +85,6 @@ export default { this.offset = 0 this.tableData = [] const route = this.$route - this.$store.dispatch('setMenuState', route.name) this.loadStations(route.name) }, /** @@ -94,7 +93,12 @@ export default { */ async doFavor(station) { try { - const response = await axios.post(generateUrl('/apps/radio/radio'), station) + station = { + id: -1, + title: 'test', + content: 'testhallo', + } + const response = await axios.post(generateUrl('/apps/radio/station'), station) console.log(response) // const index = this.stations.findIndex((match) => match.id === this.currentStationId) // this.$set(this.stations, index, response.data) diff --git a/src/router.js b/src/router.js index 4c6d0a9..2cd79be 100644 --- a/src/router.js +++ b/src/router.js @@ -4,6 +4,7 @@ import { generateUrl } from '@nextcloud/router' import axios from '@nextcloud/axios' import Main from './components/Main' +import store from './store.js' Vue.use(Router) @@ -42,6 +43,7 @@ const router = new Router({ router.beforeEach((to, from, next) => { if (to.name) { + store.dispatch('setMenuState', to.name) next() } else { axios