68 lines
1.9 KiB
PHP
68 lines
1.9 KiB
PHP
<?php
|
|
namespace OCA\Agency\Service;
|
|
|
|
use Exception;
|
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
|
|
|
use OCA\Agency\Db\Agency;
|
|
use OCA\Agency\Db\AgencyMapper;
|
|
|
|
|
|
class AgencyService {
|
|
|
|
private $mapper;
|
|
|
|
public function __construct(AgencyMapper $mapper){
|
|
$this->mapper = $mapper;
|
|
}
|
|
|
|
private function handleException ($e) {
|
|
if ($e instanceof DoesNotExistException ||
|
|
$e instanceof MultipleObjectsReturnedException) {
|
|
throw new NotFoundException($e->getMessage());
|
|
} else {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
//Find an Agency
|
|
public function find(int $id) {
|
|
try {
|
|
return $this->mapper->find($id);
|
|
} catch(Exception $e) {
|
|
$this->handleException($e);
|
|
}
|
|
}
|
|
|
|
//Create an Agency
|
|
public function create(string $name, string $inhaber, string $street, string $plz, string $city, string $agencymail, string $phone) {
|
|
$agency = new Agency();
|
|
$agency->setName($name);
|
|
$agency->setInhaber($inhaber);
|
|
$agency->setStreet($street);
|
|
$agency->setPlz($plz);
|
|
$agency->setCity($city);
|
|
$agency->setAgencymail($agencymail);
|
|
$agency->setPhone($phone);
|
|
return $this->mapper->insert($agency);
|
|
}
|
|
//Update an Agency
|
|
public function update(int $id, string $name, string $inhaber, string $street, string $plz, string $city, string $agencymail, string $phone) {
|
|
try {
|
|
$agency = $this->mapper->find($id);
|
|
} catch(Exception $e) {
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
}
|
|
$agency->setName($name);
|
|
$agency->setInhaber($inhaber);
|
|
$agency->setStreet($street);
|
|
$agency->setPlz($plz);
|
|
$agency->setCity($city);
|
|
$agency->setAgencymail($agencymail);
|
|
$agency->setPhone($phone);
|
|
return $this->mapper->update($agency);
|
|
}
|
|
|
|
} |