* * 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 . */ namespace OCA\Radio\Service; use OCA\Radio\Db\FavoriteMapper; use OCA\Radio\Db\Station; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; class FavoriteService { public function __construct( private readonly FavoriteMapper $mapper ) { $this->mapper = $mapper; } /** * @return Station[] */ public function findAll(string $userId): array { return $this->mapper->findAll($userId); } public function find(int $id, string $userId): ?Station { 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 $exception) { return $this->handleException($exception); } } 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 { $station = new Station(); $station->setStationuuid($stationuuid); $station->setName($name); $station->setFavicon($favicon); $station->setUrlresolved($urlresolved); $station->setBitrate($bitrate); $station->setCountry($country); $station->setLanguage($language); $station->setHomepage($homepage); $station->setCodec($codec); $station->setTags($tags); $station->setUserId($userId); return $this->mapper->insert($station); } 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 { try { $station = $this->mapper->find($id, $userId); $station->setStationuuid($stationuuid); $station->setName($name); $station->setFavicon($favicon); $station->setUrlresolved($urlresolved); $station->setBitrate($bitrate); $station->setCountry($country); $station->setLanguage($language); $station->setHomepage($homepage); $station->setCodec($codec); $station->setTags($tags); return $this->mapper->update($station); } catch (\Exception $exception) { return $this->handleException($exception); } } public function delete(int $id, string $userId): ?Station { try { $station = $this->mapper->find($id, $userId); $this->mapper->delete($station); return $station; } catch (\Exception $exception) { return $this->handleException($exception); } } private function handleException(\Throwable $e): void { if ($e instanceof DoesNotExistException || $e instanceof MultipleObjectsReturnedException) { throw new StationNotFound($e->getMessage()); } throw $e; } }