2020-10-30 20:16:27 +00:00
|
|
|
<?php
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-11-24 14:16:53 +00:00
|
|
|
/**
|
2024-11-19 19:41:46 +00:00
|
|
|
* Radio App.
|
2020-11-24 14:16:53 +00:00
|
|
|
*
|
|
|
|
* @author Jonas Heinrich
|
2021-01-15 10:21:53 +00:00
|
|
|
* @copyright 2021 Jonas Heinrich <onny@project-insanity.org>
|
2020-11-24 14:16:53 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-10-31 12:14:24 +00:00
|
|
|
namespace OCA\Radio\Service;
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
use OCA\Radio\Db\FavoriteMapper;
|
|
|
|
use OCA\Radio\Db\Station;
|
2020-10-31 12:14:24 +00:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
class FavoriteService
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
private readonly FavoriteMapper $mapper
|
|
|
|
) {
|
2020-10-31 12:14:24 +00:00
|
|
|
$this->mapper = $mapper;
|
|
|
|
}
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
/**
|
|
|
|
* @return Station[]
|
|
|
|
*/
|
2020-10-31 12:14:24 +00:00
|
|
|
public function findAll(string $userId): array {
|
|
|
|
return $this->mapper->findAll($userId);
|
|
|
|
}
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
public function find(int $id, string $userId): ?Station {
|
2020-10-31 12:14:24 +00:00
|
|
|
try {
|
|
|
|
return $this->mapper->find($id, $userId);
|
|
|
|
// in order to be able to plug in different storage backends like files
|
2024-11-19 19:41:46 +00:00
|
|
|
// 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 $exception) {
|
|
|
|
return $this->handleException($exception);
|
2020-10-31 12:14:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
public function create(
|
|
|
|
string $stationuuid,
|
|
|
|
string $name,
|
|
|
|
?string $favicon,
|
|
|
|
?string $urlresolved,
|
|
|
|
?string $bitrate,
|
|
|
|
?string $country,
|
|
|
|
?string $language,
|
|
|
|
?string $homepage,
|
|
|
|
?string $codec,
|
|
|
|
?string $tags,
|
|
|
|
string $userId
|
|
|
|
): Station {
|
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);
|
2024-11-19 19:41:46 +00:00
|
|
|
|
2020-10-31 12:14:24 +00:00
|
|
|
return $this->mapper->insert($station);
|
|
|
|
}
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
public function update(
|
|
|
|
int $id,
|
|
|
|
string $stationuuid,
|
|
|
|
string $name,
|
|
|
|
?string $favicon,
|
|
|
|
?string $urlresolved,
|
|
|
|
?string $bitrate,
|
|
|
|
?string $country,
|
|
|
|
?string $language,
|
|
|
|
?string $homepage,
|
|
|
|
?string $codec,
|
|
|
|
?string $tags,
|
|
|
|
string $userId
|
|
|
|
): ?Station {
|
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);
|
2024-11-19 19:41:46 +00:00
|
|
|
|
2020-10-31 12:14:24 +00:00
|
|
|
return $this->mapper->update($station);
|
2024-11-19 19:41:46 +00:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
return $this->handleException($exception);
|
2020-10-31 12:14:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
public function delete(int $id, string $userId): ?Station {
|
2020-10-31 12:14:24 +00:00
|
|
|
try {
|
|
|
|
$station = $this->mapper->find($id, $userId);
|
|
|
|
$this->mapper->delete($station);
|
2024-11-19 19:41:46 +00:00
|
|
|
|
2020-10-31 12:14:24 +00:00
|
|
|
return $station;
|
2024-11-19 19:41:46 +00:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
return $this->handleException($exception);
|
2020-10-30 20:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-19 19:41:46 +00:00
|
|
|
|
|
|
|
private function handleException(\Throwable $e): void {
|
|
|
|
if ($e instanceof DoesNotExistException
|
|
|
|
|| $e instanceof MultipleObjectsReturnedException) {
|
|
|
|
throw new StationNotFound($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
2020-10-30 20:16:27 +00:00
|
|
|
}
|