diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..003e69a --- /dev/null +++ b/COPYING @@ -0,0 +1,7 @@ +Copyright 2016 Jonas Heinrich + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/controller/errors.php b/controller/errors.php new file mode 100644 index 0000000..82472f9 --- /dev/null +++ b/controller/errors.php @@ -0,0 +1,24 @@ + $e->getMessage()]; + return new DataResponse($message, Http::STATUS_NOT_FOUND); + } + } + +} \ No newline at end of file diff --git a/controller/pagecontroller.php b/controller/pagecontroller.php new file mode 100644 index 0000000..0def552 --- /dev/null +++ b/controller/pagecontroller.php @@ -0,0 +1,22 @@ +service = $service; + $this->userId = $UserId; + } + + /** + * @CORS + * @NoCSRFRequired + * @NoAdminRequired + */ + public function index() { + return new DataResponse($this->service->findAll($this->userId)); + } + + /** + * @CORS + * @NoCSRFRequired + * @NoAdminRequired + * + * @param int $id + */ + public function show($id) { + return $this->handleNotFound(function () use ($id) { + return $this->service->find($id, $this->userId); + }); + } + + /** + * @CORS + * @NoCSRFRequired + * @NoAdminRequired + * + * @param string $stationid + */ + public function create($stationid) { + return $this->service->create($stationid, $this->userId); + } + + /** + * @CORS + * @NoCSRFRequired + * @NoAdminRequired + * + * @param int $id + * @param string $stationid + */ + public function update($id, $stationid) { + return $this->handleNotFound(function () use ($id, $stationid) { + return $this->service->update($id, $stationid, $this->userId); + }); + } + + /** + * @CORS + * @NoCSRFRequired + * @NoAdminRequired + * + * @param int $id + */ + public function destroy($id) { + return $this->handleNotFound(function () use ($id) { + return $this->service->delete($id, $this->userId); + }); + } + +} diff --git a/db/station.php b/db/station.php new file mode 100644 index 0000000..101b691 --- /dev/null +++ b/db/station.php @@ -0,0 +1,20 @@ + $this->id, + 'stationid' => $this->stationid, + + ]; + } +} diff --git a/service/notfoundexception.php b/service/notfoundexception.php new file mode 100644 index 0000000..91c05e9 --- /dev/null +++ b/service/notfoundexception.php @@ -0,0 +1,6 @@ +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); + } + } + +}