fix db backend

This commit is contained in:
Jonas Heinrich 2017-02-21 17:57:30 +01:00
parent 11fc84b0e9
commit 7a80936044
6 changed files with 59 additions and 106 deletions

View File

@ -1,20 +1,28 @@
<?php
/**
* Copyright (c) 2014, Lukas Reschke <lukas@owncloud.com>
* 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' => '.+']]
]
];

View File

@ -1,13 +1,13 @@
<?php
namespace OCA\OwnNotes\Controller;
namespace OCA\Radio\Controller;
use OCP\IRequest;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCA\OwnNotes\Service\NoteService;
use OCA\Radio\Service\StationService;
class NoteController extends Controller {
class StationController extends Controller {
private $service;
private $userId;
@ -15,7 +15,7 @@ class NoteController extends Controller {
use Errors;
public function __construct($AppName, IRequest $request,
NoteService $service, $UserId){
StationService $service, $UserId){
parent::__construct($AppName, $request);
$this->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);
});

View File

@ -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]);
}

View File

@ -1,17 +0,0 @@
<?php
namespace OCA\Radio\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class Stream extends Entity implements JsonSerializable {
protected $userId;
public function jsonSerialize() {
return [
'id' => $this->id
];
}
}

View File

@ -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"]
};

View File

@ -1,51 +0,0 @@
<?php
namespace OCA\OwnNotes\Service;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCA\OwnNotes\Db\Note;
use OCA\OwnNotes\Db\NoteMapper;
class NoteService {
private $mapper;
public function __construct(NoteMapper $mapper){
$this->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);
}
}
}