implementing recent page

This commit is contained in:
Jonas Heinrich 2020-11-13 13:17:05 +01:00
parent 67f4bebefc
commit 3752d93e38
9 changed files with 236 additions and 10 deletions

View File

@ -24,7 +24,7 @@
return [ return [
'resources' => [ 'resources' => [
'favorite' => ['url' => '/api/favorites'], 'favorite' => ['url' => '/api/favorites'],
'recent' => ['url' => '/api/recents'], 'recent' => ['url' => '/api/recent'],
], ],
'routes' => [ 'routes' => [
@ -99,6 +99,12 @@ return [
'url' => '/api/0.1/{path}', 'url' => '/api/0.1/{path}',
'verb' => 'OPTIONS', 'verb' => 'OPTIONS',
'requirements' => ['path' => '.+'] 'requirements' => ['path' => '.+']
],
[
'name' => 'recent_api#preflighted_cors',
'url' => '/api/0.1/{path}',
'verb' => 'OPTIONS',
'requirements' => ['path' => '.+']
] ]
] ]

7
css/dashboard.css Normal file
View File

@ -0,0 +1,7 @@
.icon-mail {
background-image: url(./../img/radio-trans.svg);
}
body.theme--dark .icon-mail {
background-image: url(./../img/radio.svg);
}

View File

@ -3,13 +3,13 @@
namespace OCA\Radio\Controller; namespace OCA\Radio\Controller;
use OCA\Radio\AppInfo\Application; use OCA\Radio\AppInfo\Application;
use OCA\Radio\Service\StationService; use OCA\Radio\Service\FavoriteService;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest; use OCP\IRequest;
class FavoriteController extends Controller { class FavoriteController extends Controller {
/** @var StationService */ /** @var FavoriteService */
private $service; private $service;
/** @var string */ /** @var string */
@ -18,7 +18,7 @@ class FavoriteController extends Controller {
use Errors; use Errors;
public function __construct(IRequest $request, public function __construct(IRequest $request,
StationService $service, FavoriteService $service,
$userId) { $userId) {
parent::__construct(Application::APP_ID, $request); parent::__construct(Application::APP_ID, $request);
$this->service = $service; $this->service = $service;

View File

@ -0,0 +1,70 @@
<?php
namespace OCA\Radio\Controller;
use OCA\Radio\AppInfo\Application;
use OCA\Radio\Service\RecentService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
class RecentController extends Controller {
/** @var RecentService */
private $service;
/** @var string */
private $userId;
use Errors;
public function __construct(IRequest $request,
RecentService $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 $stationuuid, string $name, string $favicon, string $urlresolved): DataResponse {
return new DataResponse($this->service->create($stationuuid, $name,
$favicon, $urlresolved, $this->userId));
}
/**
* @NoAdminRequired
*/
public function update(int $id, string $stationuuid,
string $name, string $favicon, string $urlresolved): DataResponse {
return $this->handleNotFound(function () use ($id, $stationuuid, $name, $favicon, $urlresolved) {
return $this->service->update($id, $stationuuid, $name, $favicon, $urlresolved, $this->userId);
});
}
/**
* @NoAdminRequired
*/
public function destroy(int $id): DataResponse {
return $this->handleNotFound(function () use ($id) {
return $this->service->delete($id, $this->userId);
});
}
}

View File

@ -8,7 +8,7 @@ use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection; use OCP\IDBConnection;
class StationMapper extends QBMapper { class FavoriteMapper extends QBMapper {
public function __construct(IDBConnection $db) { public function __construct(IDBConnection $db) {
parent::__construct($db, 'favorites', Station::class); parent::__construct($db, 'favorites', Station::class);
} }

45
lib/Db/RecentMapper.php Normal file
View File

@ -0,0 +1,45 @@
<?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 RecentMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'recent', Station::class);
}
/**
* @param int $id
* @param string $userId
* @return Entity|Station
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws DoesNotExistException
*/
public function find(int $id, string $userId): Station {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('recent')
->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('recent')
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
return $this->findEntities($qb);
}
}

View File

@ -0,0 +1,80 @@
<?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\FavoriteMapper;
class FavoriteService {
/** @var FavoriteMapper */
private $mapper;
public function __construct(FavoriteMapper $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 StationNotFound($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($stationuuid, $name, $favicon, $urlresolved, $userId) {
$station = new Station();
$station->setStationuuid($stationuuid);
$station->setName($name);
$station->setFavicon($favicon);
$station->setUrlresolved($urlresolved);
$station->setUserId($userId);
return $this->mapper->insert($station);
}
public function update($id, $stationuuid, $name, $favicon, $urlresolved, $userId) {
try {
$station = $this->mapper->find($id, $userId);
$station->setStationuuid($stationuuid);
$station->setName($name);
$station->setFavicon($favicon);
$station->setUrlresolved($urlresolved);
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);
}
}
}

View File

@ -8,14 +8,14 @@ use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCA\Radio\Db\Station; use OCA\Radio\Db\Station;
use OCA\Radio\Db\StationMapper; use OCA\Radio\Db\RecentMapper;
class StationService { class RecentService {
/** @var StationMapper */ /** @var RecentMapper */
private $mapper; private $mapper;
public function __construct(StationMapper $mapper) { public function __construct(RecentMapper $mapper) {
$this->mapper = $mapper; $this->mapper = $mapper;
} }

View File

@ -157,7 +157,7 @@ export default {
* Start playing a radio station and counting the playback * Start playing a radio station and counting the playback
* @param {Object} station Station object * @param {Object} station Station object
*/ */
doPlay(station) { async doPlay(station) {
const vm = this const vm = this
if (audioPlayer !== null) { if (audioPlayer !== null) {
@ -201,6 +201,22 @@ export default {
/* Count click */ /* Count click */
axios.get(this.$apiUrl + '/json/url/' + station.stationuuid) axios.get(this.$apiUrl + '/json/url/' + station.stationuuid)
/* Put into recent stations */
try {
const stationMap = {
id: -1,
name: station.name,
urlresolved: station.url_resolved,
favicon: station.favicon,
stationuuid: station.stationuuid,
}
await axios
.post(generateUrl('/apps/radio/api/recent'), stationMap)
} catch (error) {
showError(t('radio', 'Could not add station to recent list'))
}
}, },
/** /**
@ -257,6 +273,8 @@ export default {
queryURI = queryBase + '/byname/' + searchQuery queryURI = queryBase + '/byname/' + searchQuery
} else if (menuState === 'FAVORITES') { } else if (menuState === 'FAVORITES') {
queryURI = generateUrl('/apps/radio/api/favorites') queryURI = generateUrl('/apps/radio/api/favorites')
} else if (menuState === 'RECENT') {
queryURI = generateUrl('/apps/radio/api/recent')
} }
await axios.get(queryURI, { await axios.get(queryURI, {