From 7a80936044968be4f39062a81fadcbb05330f9b3 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Tue, 21 Feb 2017 17:57:30 +0100 Subject: [PATCH] fix db backend --- appinfo/routes.php | 24 +++++++++----- controller/stationcontroller.php | 54 ++++++++++++++++++++++---------- db/stationmapper.php | 4 +-- db/stream.php | 17 ---------- js/main.js | 15 ++------- service/streamservice.php | 51 ------------------------------ 6 files changed, 59 insertions(+), 106 deletions(-) delete mode 100644 db/stream.php delete mode 100644 service/streamservice.php diff --git a/appinfo/routes.php b/appinfo/routes.php index 6a2f53c..1b0c0a5 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -1,20 +1,28 @@ - * This file is licensed under the Affero General Public License version 3 or later. - * See the COPYING-README file. - */ + /** @var $this \OCP\Route\IRouter */ - $this->create('radio_index', '/') ->actionInclude('radio/index.php'); -return [ +/** return [ 'resources' => [ 'station' => ['url' => '/stations'], ], 'routes' => [ ['name' => 'page#index', 'url' => '/', 'verb' => 'GET'], ] -]; +]; **/ + + +return [ + 'resources' => [ + 'station' => ['url' => '/stations'], + 'station_api' => ['url' => '/api/0.1/stations'] + ], + 'routes' => [ + ['name' => 'page#index', 'url' => '/', 'verb' => 'GET'], + ['name' => 'station_api#preflighted_cors', 'url' => '/api/0.1/{path}', + 'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']] + ] +]; \ No newline at end of file diff --git a/controller/stationcontroller.php b/controller/stationcontroller.php index d2037d0..dfe8334 100644 --- a/controller/stationcontroller.php +++ b/controller/stationcontroller.php @@ -1,13 +1,13 @@ service = $service; $this->userId = $UserId; @@ -24,26 +24,48 @@ class NoteController extends Controller { /** * @NoAdminRequired */ - public function favorites() { + public function index() { return new DataResponse($this->service->findAll($this->userId)); } - /** - * @NoAdminRequired - * - * @param string $title - * @param string $content - */ - public function fav($id) { - return $this->service->create($id, $this->userId); - } - /** * @NoAdminRequired * * @param int $id */ - public function unfav($id) { + public function show($id) { + return $this->handleNotFound(function () use ($id) { + return $this->service->find($id, $this->userId); + }); + } + + /** + * @NoAdminRequired + * + * @param string $stationid + */ + public function create($stationid) { + return $this->service->create($stationid, $this->userId); + } + + /** + * @NoAdminRequired + * + * @param int $id + * @param string $stationid + */ + public function update($id, $stationid) { + return $this->handleNotFound(function () use ($id, $stationid) { + return $this->service->update($id, $stationid, $this->userId); + }); + } + + /** + * @NoAdminRequired + * + * @param int $id + */ + public function destroy($id) { return $this->handleNotFound(function () use ($id) { return $this->service->delete($id, $this->userId); }); diff --git a/db/stationmapper.php b/db/stationmapper.php index b9214cb..164b0c0 100644 --- a/db/stationmapper.php +++ b/db/stationmapper.php @@ -11,12 +11,12 @@ class StationMapper extends Mapper { } public function find($id, $userId) { - $sql = 'SELECT * FROM *PREFIX*ownnotes_notes WHERE id = ? AND user_id = ?'; + $sql = 'SELECT * FROM *PREFIX*radio_stations WHERE id = ? AND user_id = ?'; return $this->findEntity($sql, [$id, $userId]); } public function findAll($userId) { - $sql = 'SELECT * FROM *PREFIX*ownnotes_notes WHERE user_id = ?'; + $sql = 'SELECT * FROM *PREFIX*radio_stations WHERE user_id = ?'; return $this->findEntities($sql, [$userId]); } diff --git a/db/stream.php b/db/stream.php deleted file mode 100644 index 067cd88..0000000 --- a/db/stream.php +++ /dev/null @@ -1,17 +0,0 @@ - $this->id - ]; - } -} diff --git a/js/main.js b/js/main.js index d783d05..f943feb 100644 --- a/js/main.js +++ b/js/main.js @@ -5,10 +5,6 @@ $(function(){ play_station(stationid); }); - function station_fav(stationid){ - alert("faving"); - }; - function play_station(stationid){ $.ajax({ method: "GET", @@ -43,14 +39,9 @@ $(function(){ } function get_station_ids(){ - $.ajax({ - method: "GET", - url: "http://127.0.0.1/index.php/apps/radio/stations", - dataType: 'json', - success: function(data) { - alert(data); - return true; - } + var baseUrl = OC.generateUrl('/apps/radio'); + $.get(baseUrl + '/stations', function ( data ) { + alert(JSON.stringify(data)); }); return ["89920","44707","91101","85755","45281","78011","91102"] }; diff --git a/service/streamservice.php b/service/streamservice.php deleted file mode 100644 index dae6435..0000000 --- a/service/streamservice.php +++ /dev/null @@ -1,51 +0,0 @@ -mapper = $mapper; - } - - public function findAll($userId) { - return $this->mapper->findAll($userId); - } - - private function handleException ($e) { - if ($e instanceof DoesNotExistException || - $e instanceof MultipleObjectsReturnedException) { - throw new NotFoundException($e->getMessage()); - } else { - throw $e; - } - } - - public function fav($id, $userId) { - $station = new Station(); - $station->setId($id); - $station->setUserId($userId); - return $this->mapper->insert($station); - } - - public function unfav($id, $userId) { - try { - $station = $this->mapper->find($id, $userId); - $this->mapper->delete($station); - return $station; - } catch(Exception $e) { - $this->handleException($e); - } - } - -}