From 5546a3f3bde2060d2837e0514168b3071a655b8d Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sat, 31 Oct 2020 13:14:24 +0100 Subject: [PATCH] trying to implement database backend --- Dockerfile | 2 - README.md | 2 +- appinfo/info.xml | 2 +- lib/Command/Search.php | 51 -------------- lib/Controller/StationController.php | 2 - lib/Service/SearchService.php | 59 ---------------- lib/Service/StationService.php | 102 +++++++++++++++++---------- src/components/Main.vue | 2 +- 8 files changed, 68 insertions(+), 154 deletions(-) delete mode 100644 Dockerfile delete mode 100644 lib/Command/Search.php delete mode 100644 lib/Service/SearchService.php diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index a07635f..0000000 --- a/Dockerfile +++ /dev/null @@ -1,2 +0,0 @@ -ARG NEXTCLOUD_VERSION=20.0.0 -FROM rootlogin/nextcloud diff --git a/README.md b/README.md index 4261ca2..756cebb 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Mount or move the ``radio`` folder into your Nextcloud ``apps/`` directory. Go t Can be easily tested using Docker: ``` -docker build -t nextcloud . +docker build -t nextcloud https://git.project-insanity.org/onny/docker-nextcloud.git docker run -v /tmp/nextcloud-app-radio:/opt/nextcloud/apps/radio -d --name nextcloud-app-radio -p 80:80 nextcloud ``` First part of -v is the path to the cloned and compiled or downloaded Nextcloud Radio app. Debug running container it with: diff --git a/appinfo/info.xml b/appinfo/info.xml index 6a8902c..886d778 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -14,7 +14,7 @@ https://git.project-insanity.org/onny/nextcloud-app-radio/issues https://git.project-insanity.org/onny/nextcloud-app-radio/raw/master/screenshot.png - + OCA\Radio\Command\Search diff --git a/lib/Command/Search.php b/lib/Command/Search.php deleted file mode 100644 index c33f776..0000000 --- a/lib/Command/Search.php +++ /dev/null @@ -1,51 +0,0 @@ -searchService = $searchService; - } - - /** - * @return void - */ - protected function configure() { - $this->setName('radio:search'); - $this->setDescription('Search for radio stations'); - - $this->addArgument("term", InputArgument::OPTIONAL); - } - - protected function execute(InputInterface $input, OutputInterface $output): int { - $radioStations = $this->searchService->findRadioStations( - $input->getArgument("term") - ); - - $output->writeln("Found radio stations"); - foreach ($radioStations as $radioStation) { - $output->writeln("* $radioStation"); - } - - return 0; - } -} diff --git a/lib/Controller/StationController.php b/lib/Controller/StationController.php index e51166d..5e61506 100644 --- a/lib/Controller/StationController.php +++ b/lib/Controller/StationController.php @@ -15,8 +15,6 @@ class StationController extends Controller { /** @var string */ private $userId; - use Errors; - public function __construct(IRequest $request, StationService $service, $userId) { diff --git a/lib/Service/SearchService.php b/lib/Service/SearchService.php deleted file mode 100644 index 21c7745..0000000 --- a/lib/Service/SearchService.php +++ /dev/null @@ -1,59 +0,0 @@ -clientService = $clientService; - $this->logger = $logger; - } - - public function findRadioStations(?string $term = null): array { - $this->logger->debug('searching radio stations'); - - $client = $this->clientService->newClient(); - - try { - $response = $client->get("https://cat-fact.herokuapp.com/facts?animal_type=cat"); - } catch (Exception $e) { - $this->logger->error("Could not search for radio stations. Please check connection to radio-browser API."); - - throw $e; - } - - $body = $response->getBody(); - $parsed = json_decode($body, true); - - $mapped = array_map(function(array $radioStation) { - return $radioStation['text']; - }, $parsed['all']); - - if (empty($term)) { - return $mapped; - } - - return array_filter($mapped, function(string $radioStation) use ($term) { - return mb_strpos(mb_strtolower($radioStation), mb_strtolower($term)); - }); - } - -} diff --git a/lib/Service/StationService.php b/lib/Service/StationService.php index 28d2b4a..fa3ede9 100644 --- a/lib/Service/StationService.php +++ b/lib/Service/StationService.php @@ -1,48 +1,76 @@ 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' => '' - ]); + /** @var StationMapper */ + private $mapper; - $table->setPrimaryKey(['id']); - $table->addIndex(['user_id'], 'radio_user_id_index'); + public function __construct(StationMapper $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 StationNotFound($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($title, $content, $userId) { + $station = new Station(); + $station->setTitle($title); + $station->setContent($content); + $station->setUserId($userId); + return $this->mapper->insert($station); + } + + public function update($id, $title, $content, $userId) { + try { + $station = $this->mapper->find($id, $userId); + $station->setTitle($title); + $station->setContent($content); + return $this->mapper->update($station); + } catch (Exception $e) { + $this->handleException($e); + } + } + + public function delete($id, $userId) { + try { + $station = $this->mapper->find($id, $userId); + $this->mapper->delete($station); + return $station; + } catch (Exception $e) { + $this->handleException($e); } - return $schema; } } diff --git a/src/components/Main.vue b/src/components/Main.vue index 00c97cf..0e626df 100644 --- a/src/components/Main.vue +++ b/src/components/Main.vue @@ -98,7 +98,7 @@ export default { title: 'test', content: 'testhallo', } - const response = await axios.post(generateUrl('/apps/radio/station'), station) + const response = await axios.post(generateUrl('/apps/radio/stations'), station) console.log(response) // const index = this.stations.findIndex((match) => match.id === this.currentStationId) // this.$set(this.stations, index, response.data)