2020-10-30 20:16:27 +00:00
|
|
|
<?php
|
|
|
|
|
2020-10-31 12:14:24 +00:00
|
|
|
namespace OCA\Radio\Service;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
|
|
|
|
|
|
|
use OCA\Radio\Db\Station;
|
2020-11-13 12:17:05 +00:00
|
|
|
use OCA\Radio\Db\RecentMapper;
|
2020-10-31 12:14:24 +00:00
|
|
|
|
2020-11-13 12:17:05 +00:00
|
|
|
class RecentService {
|
2020-10-31 12:14:24 +00:00
|
|
|
|
2020-11-13 12:17:05 +00:00
|
|
|
/** @var RecentMapper */
|
2020-10-31 12:14:24 +00:00
|
|
|
private $mapper;
|
|
|
|
|
2020-11-13 12:17:05 +00:00
|
|
|
public function __construct(RecentMapper $mapper) {
|
2020-10-31 12:14:24 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 21:35:13 +00:00
|
|
|
public function create($stationuuid, $name, $favicon, $urlresolved,
|
|
|
|
$bitrate, $country, $language, $homepage, $codec, $tags, $userId) {
|
2020-10-31 12:14:24 +00:00
|
|
|
$station = new Station();
|
2020-11-06 09:13:50 +00:00
|
|
|
$station->setStationuuid($stationuuid);
|
2020-11-06 08:11:30 +00:00
|
|
|
$station->setName($name);
|
|
|
|
$station->setFavicon($favicon);
|
2020-11-07 09:26:41 +00:00
|
|
|
$station->setUrlresolved($urlresolved);
|
2020-11-19 21:35:13 +00:00
|
|
|
$station->setBitrate($bitrate);
|
|
|
|
$station->setCountry($country);
|
|
|
|
$station->setLanguage($language);
|
|
|
|
$station->setHomepage($homepage);
|
|
|
|
$station->setCodec($codec);
|
|
|
|
$station->setTags($tags);
|
2020-10-31 12:14:24 +00:00
|
|
|
$station->setUserId($userId);
|
|
|
|
return $this->mapper->insert($station);
|
|
|
|
}
|
|
|
|
|
2020-11-19 21:35:13 +00:00
|
|
|
public function update($id, $stationuuid, $name, $favicon, $urlresolved,
|
|
|
|
$bitrate, $country, $language, $homepage, $codec, $tags, $userId) {
|
2020-10-31 12:14:24 +00:00
|
|
|
try {
|
|
|
|
$station = $this->mapper->find($id, $userId);
|
2020-11-06 09:13:50 +00:00
|
|
|
$station->setStationuuid($stationuuid);
|
2020-11-06 08:11:30 +00:00
|
|
|
$station->setName($name);
|
|
|
|
$station->setFavicon($favicon);
|
2020-11-07 09:26:41 +00:00
|
|
|
$station->setUrlresolved($urlresolved);
|
2020-11-19 21:35:13 +00:00
|
|
|
$station->setBitrate($bitrate);
|
|
|
|
$station->setCountry($country);
|
|
|
|
$station->setLanguage($language);
|
|
|
|
$station->setHomepage($homepage);
|
|
|
|
$station->setCodec($codec);
|
|
|
|
$station->setTags($tags);
|
2020-10-31 12:14:24 +00:00
|
|
|
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);
|
2020-10-30 20:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|