nextcloud-app-radio/service/stationservice.php
2017-02-21 17:57:35 +01:00

75 lines
2.0 KiB
PHP

<?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\StationMapper;
class StationService {
private $mapper;
public function __construct(StationMapper $mapper){
$this->mapper = $mapper;
}
public function findAll($userId) {
return $this->mapper->findAll($userId);
}
private function handleException ($e) {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new NotFoundException($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($stationid, $userId) {
$station = new Station();
$station->setStationid($stationid);
$station->setUserId($userId);
return $this->mapper->insert($station);
}
public function update($id, $stationid, $userId) {
try {
$station = $this->mapper->find($id, $userId);
$station->setStationid($stationid);
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);
}
}
}