Commit 7a809360 authored by onny's avatar onny

fix db backend

parent 11fc84b0
<?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 */
/** @var $this \OCP\Route\IRouter */
$this->create('radio_index', '/')
->actionInclude('radio/index.php');
/** 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
<?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,18 +24,40 @@ 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
* @param int $id
*/
public function show($id) {
return $this->handleNotFound(function () use ($id) {
return $this->service->find($id, $this->userId);
});
}
/**
* @NoAdminRequired
*
* @param string $stationid
*/
public function fav($id) {
return $this->service->create($id, $this->userId);
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);
});
}
/**
......@@ -43,7 +65,7 @@ class NoteController extends Controller {
*
* @param int $id
*/
public function unfav($id) {
public function destroy($id) {
return $this->handleNotFound(function () use ($id) {
return $this->service->delete($id, $this->userId);
});
......
......@@ -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]);
}
......
<?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
];
}
}
......@@ -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"]
};
......
<?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);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment