trying to implement database backend
This commit is contained in:
parent
415a672af3
commit
4a6a3ac8ae
@ -23,8 +23,8 @@
|
||||
|
||||
return [
|
||||
'resources' => [
|
||||
'radio' => ['url' => '/radio'],
|
||||
'radio_api' => ['url' => '/api/0.1/radio']
|
||||
'station' => ['url' => '/stations'],
|
||||
'station_api' => ['url' => '/api/0.1/stations']
|
||||
],
|
||||
'routes' => [
|
||||
// Web page templates
|
||||
@ -40,7 +40,7 @@ return [
|
||||
['name' => 'settings#set_volume_state', 'url' => '/settings/volumeState', 'verb' => 'POST'],
|
||||
['name' => 'settings#get_volume_state', 'url' => '/settings/volumeState', 'verb' => 'GET'],
|
||||
// Api
|
||||
['name' => 'radio_api#preflighted_cors', 'url' => '/api/0.1/{path}',
|
||||
['name' => 'station_api#preflighted_cors', 'url' => '/api/0.1/{path}',
|
||||
'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']]
|
||||
]
|
||||
];
|
||||
|
@ -7,13 +7,13 @@ use Closure;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
|
||||
use OCA\Radio\Service\RadioNotFound;
|
||||
use OCA\Radio\Service\StationNotFound;
|
||||
|
||||
trait Errors {
|
||||
protected function handleNotFound(Closure $callback): DataResponse {
|
||||
try {
|
||||
return new DataResponse($callback());
|
||||
} catch (RadioNotFound $e) {
|
||||
} catch (StationNotFound $e) {
|
||||
$message = ['message' => $e->getMessage()];
|
||||
return new DataResponse($message, Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
@ -1,80 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\Radio\Controller;
|
||||
|
||||
use OCA\Radio\AppInfo\Application;
|
||||
use OCA\Radio\Service\RadioService;
|
||||
use OCP\AppFramework\ApiController;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\IRequest;
|
||||
|
||||
class RadioApiController extends ApiController {
|
||||
/** @var RadioService */
|
||||
private $service;
|
||||
|
||||
/** @var string */
|
||||
private $userId;
|
||||
|
||||
use Errors;
|
||||
|
||||
public function __construct(IRequest $request,
|
||||
RadioService $service,
|
||||
$userId) {
|
||||
parent::__construct(Application::APP_ID, $request);
|
||||
$this->service = $service;
|
||||
$this->userId = $userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @CORS
|
||||
* @NoCSRFRequired
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function index(): DataResponse {
|
||||
return new DataResponse($this->service->findAll($this->userId));
|
||||
}
|
||||
|
||||
/**
|
||||
* @CORS
|
||||
* @NoCSRFRequired
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function show(int $id): DataResponse {
|
||||
return $this->handleNotFound(function () use ($id) {
|
||||
return $this->service->find($id, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @CORS
|
||||
* @NoCSRFRequired
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function create(string $title, string $content): DataResponse {
|
||||
return new DataResponse($this->service->create($station,
|
||||
$this->userId));
|
||||
}
|
||||
|
||||
/**
|
||||
* @CORS
|
||||
* @NoCSRFRequired
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function update(int $id, string $title,
|
||||
string $content): DataResponse {
|
||||
return $this->handleNotFound(function () use ($id, $station) {
|
||||
return $this->service->update($id, $station, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @CORS
|
||||
* @NoCSRFRequired
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function destroy(int $id): DataResponse {
|
||||
return $this->handleNotFound(function () use ($id) {
|
||||
return $this->service->delete($id, $this->userId);
|
||||
});
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\Radio\Controller;
|
||||
|
||||
use OCA\Radio\AppInfo\Application;
|
||||
use OCA\Radio\Service\RadioService;
|
||||
use OCP\AppFramework\Controller;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\IRequest;
|
||||
|
||||
class RadioController extends Controller {
|
||||
/** @var RadioService */
|
||||
private $service;
|
||||
|
||||
/** @var string */
|
||||
private $userId;
|
||||
|
||||
use Errors;
|
||||
|
||||
public function __construct(IRequest $request,
|
||||
RadioService $service,
|
||||
$userId) {
|
||||
parent::__construct(Application::APP_ID, $request);
|
||||
$this->service = $service;
|
||||
$this->userId = $userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function index(): DataResponse {
|
||||
return new DataResponse($this->service->findAll($this->userId));
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function show(int $id): DataResponse {
|
||||
return $this->handleNotFound(function () use ($id) {
|
||||
return $this->service->find($id, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function create(string $title, string $content): DataResponse {
|
||||
return new DataResponse($this->service->create($station,
|
||||
$this->userId));
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function update(int $id, string $title,
|
||||
string $content): DataResponse {
|
||||
return $this->handleNotFound(function () use ($id, $station) {
|
||||
return $this->service->update($id, $station, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function destroy(int $id): DataResponse {
|
||||
return $this->handleNotFound(function () use ($id) {
|
||||
return $this->service->delete($id, $this->userId);
|
||||
});
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\Radio\Db;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
|
||||
class Note extends Entity implements JsonSerializable {
|
||||
protected $station
|
||||
protected $userId;
|
||||
|
||||
public function jsonSerialize(): array {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'station' => $this->station
|
||||
];
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\Radio\Db;
|
||||
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
use OCP\AppFramework\Db\QBMapper;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\IDBConnection;
|
||||
|
||||
class RadioMapper extends QBMapper {
|
||||
public function __construct(IDBConnection $db) {
|
||||
parent::__construct($db, 'radio', Radio::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param string $userId
|
||||
* @return Entity|Radio
|
||||
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
|
||||
* @throws DoesNotExistException
|
||||
*/
|
||||
public function find(int $id, string $userId): Radio {
|
||||
/* @var $qb IQueryBuilder */
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->select('*')
|
||||
->from('radio')
|
||||
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
|
||||
return $this->findEntity($qb);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $userId
|
||||
* @return array
|
||||
*/
|
||||
public function findAll(string $userId): array {
|
||||
/* @var $qb IQueryBuilder */
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->select('*')
|
||||
->from('radio')
|
||||
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
|
||||
return $this->findEntities($qb);
|
||||
}
|
||||
}
|
@ -27,7 +27,7 @@ class Version000000Date20181013124731 extends SimpleMigrationStep {
|
||||
'autoincrement' => true,
|
||||
'notnull' => true,
|
||||
]);
|
||||
$table->addColumn('station', 'string', [
|
||||
$table->addColumn('title', 'string', [
|
||||
'notnull' => true,
|
||||
'length' => 200
|
||||
]);
|
||||
@ -35,6 +35,10 @@ class Version000000Date20181013124731 extends SimpleMigrationStep {
|
||||
'notnull' => true,
|
||||
'length' => 200,
|
||||
]);
|
||||
$table->addColumn('content', 'text', [
|
||||
'notnull' => true,
|
||||
'default' => ''
|
||||
]);
|
||||
|
||||
$table->setPrimaryKey(['id']);
|
||||
$table->addIndex(['user_id'], 'radio_user_id_index');
|
||||
|
@ -1,6 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\Radio\Service;
|
||||
|
||||
class RadioNotFound extends \Exception {
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\Radio\Service;
|
||||
|
||||
use Exception;
|
||||
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
||||
|
||||
use OCA\Radio\Db\Radio;
|
||||
use OCA\Radio\Db\RadioMapper;
|
||||
|
||||
class RadioService {
|
||||
|
||||
/** @var RadioMapper */
|
||||
private $mapper;
|
||||
|
||||
public function __construct(RadioMapper $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 RadioNotFound($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($station, $userId) {
|
||||
$radio = new Radio();
|
||||
$radio->setStation($station);
|
||||
$radio->setUserId($userId);
|
||||
return $this->mapper->insert($radio);
|
||||
}
|
||||
|
||||
public function update($id, $station, $userId) {
|
||||
try {
|
||||
$radio = $this->mapper->find($id, $userId);
|
||||
$radio->setStation($station);
|
||||
return $this->mapper->update($radio);
|
||||
} catch (Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function delete($id, $userId) {
|
||||
try {
|
||||
$radio = $this->mapper->find($id, $userId);
|
||||
$this->mapper->delete($radio);
|
||||
return $radio;
|
||||
} catch (Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
}
|
@ -85,7 +85,6 @@ export default {
|
||||
this.offset = 0
|
||||
this.tableData = []
|
||||
const route = this.$route
|
||||
this.$store.dispatch('setMenuState', route.name)
|
||||
this.loadStations(route.name)
|
||||
},
|
||||
/**
|
||||
@ -94,7 +93,12 @@ export default {
|
||||
*/
|
||||
async doFavor(station) {
|
||||
try {
|
||||
const response = await axios.post(generateUrl('/apps/radio/radio'), station)
|
||||
station = {
|
||||
id: -1,
|
||||
title: 'test',
|
||||
content: 'testhallo',
|
||||
}
|
||||
const response = await axios.post(generateUrl('/apps/radio/station'), station)
|
||||
console.log(response)
|
||||
// const index = this.stations.findIndex((match) => match.id === this.currentStationId)
|
||||
// this.$set(this.stations, index, response.data)
|
||||
|
@ -4,6 +4,7 @@ import { generateUrl } from '@nextcloud/router'
|
||||
import axios from '@nextcloud/axios'
|
||||
|
||||
import Main from './components/Main'
|
||||
import store from './store.js'
|
||||
|
||||
Vue.use(Router)
|
||||
|
||||
@ -42,6 +43,7 @@ const router = new Router({
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
if (to.name) {
|
||||
store.dispatch('setMenuState', to.name)
|
||||
next()
|
||||
} else {
|
||||
axios
|
||||
|
Loading…
Reference in New Issue
Block a user