Commit 664cb06e authored by onny's avatar onny

fix db backend

parent 7a809360
Copyright 2016 Jonas Heinrich
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
<?php
namespace OCA\Radio\Controller;
use Closure;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCA\Radio\Service\NotFoundException;
trait Errors {
protected function handleNotFound (Closure $callback) {
try {
return new DataResponse($callback());
} catch(NotFoundException $e) {
$message = ['message' => $e->getMessage()];
return new DataResponse($message, Http::STATUS_NOT_FOUND);
}
}
}
\ No newline at end of file
<?php
namespace OCA\Radio\Controller;
use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Controller;
class PageController extends Controller {
public function __construct($AppName, IRequest $request){
parent::__construct($AppName, $request);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index() {
return new TemplateResponse('radio', 'main');
}
}
<?php
namespace OCA\Radio\Controller;
use OCP\IRequest;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\ApiController;
use OCA\Radio\Service\StationService;
class StationApiController extends ApiController {
private $service;
private $userId;
use Errors;
public function __construct($AppName, IRequest $request,
StationService $service, $UserId){
parent::__construct($AppName, $request);
$this->service = $service;
$this->userId = $UserId;
}
/**
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
*/
public function index() {
return new DataResponse($this->service->findAll($this->userId));
}
/**
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
*
* @param int $id
*/
public function show($id) {
return $this->handleNotFound(function () use ($id) {
return $this->service->find($id, $this->userId);
});
}
/**
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
*
* @param string $stationid
*/
public function create($stationid) {
return $this->service->create($stationid, $this->userId);
}
/**
* @CORS
* @NoCSRFRequired
* @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);
});
}
/**
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
*
* @param int $id
*/
public function destroy($id) {
return $this->handleNotFound(function () use ($id) {
return $this->service->delete($id, $this->userId);
});
}
}
<?php
namespace OCA\Radio\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class Station extends Entity implements JsonSerializable {
protected $stationid;
protected $userId;
public function jsonSerialize() {
return [
'id' => $this->id,
'stationid' => $this->stationid,
];
}
}
<?php
namespace OCA\OwnNotes\Service;
class NotFoundException extends ServiceException {}
\ No newline at end of file
<?php
namespace OCA\OwnNotes\Service;
use Exception;
class ServiceException extends Exception {}
\ No newline at end of file
<?php
namespace OCA\Radio\Service;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCA\Radio\Db\Station;
use OCA\Radio\Db\StationMapper;
class StationService {
private $mapper;
public function __construct(StationMapper $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 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($stationid, $userId) {
$station = new Station();
$station->setStationid($stationid);
$station->setUserId($userId);
return $this->mapper->insert($station);
}
public function update($id, $stationid, $userId) {
try {
$station = $this->mapper->find($id, $userId);
$station->setStationid($stationid);
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);
}
}
}
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