75 lines
1.7 KiB
PHP
75 lines
1.7 KiB
PHP
|
<?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);
|
||
|
}
|
||
|
}
|
||
|
}
|