From 432c3c9d8260daa4984af3b5dd682e8f994c85fc Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Sat, 17 Jan 2015 11:18:22 -0500 Subject: [PATCH] Add States MVC to API --- web/api/app/Config/routes.php | 1 + web/api/app/Controller/StatesController.php | 96 +++++++++++++++++++++ web/api/app/Model/State.php | 30 +++++++ 3 files changed, 127 insertions(+) create mode 100644 web/api/app/Controller/StatesController.php create mode 100644 web/api/app/Model/State.php diff --git a/web/api/app/Config/routes.php b/web/api/app/Config/routes.php index 69f567041..148f2a581 100755 --- a/web/api/app/Config/routes.php +++ b/web/api/app/Config/routes.php @@ -30,6 +30,7 @@ Router::mapResources('frames'); Router::mapResources('host'); Router::mapResources('logs'); + Router::mapResources('states'); Router::parseExtensions(); /** diff --git a/web/api/app/Controller/StatesController.php b/web/api/app/Controller/StatesController.php new file mode 100644 index 000000000..ad4599de7 --- /dev/null +++ b/web/api/app/Controller/StatesController.php @@ -0,0 +1,96 @@ +State->recursive = 0; + $states = $this->State->find('all'); + $this->set(array( + 'states' => $states, + '_serialize' => array('states') + )); + } + +/** + * view method + * + * @throws NotFoundException + * @param string $id + * @return void + */ + public function view($id = null) { + if (!$this->State->exists($id)) { + throw new NotFoundException(__('Invalid state')); + } + $options = array('conditions' => array('State.' . $this->State->primaryKey => $id)); + $this->set('state', $this->State->find('first', $options)); + } + +/** + * add method + * + * @return void + */ + public function add() { + if ($this->request->is('post')) { + $this->State->create(); + if ($this->State->save($this->request->data)) { + return $this->flash(__('The state has been saved.'), array('action' => 'index')); + } + } + } + +/** + * edit method + * + * @throws NotFoundException + * @param string $id + * @return void + */ + public function edit($id = null) { + if (!$this->State->exists($id)) { + throw new NotFoundException(__('Invalid state')); + } + if ($this->request->is(array('post', 'put'))) { + if ($this->State->save($this->request->data)) { + return $this->flash(__('The state has been saved.'), array('action' => 'index')); + } + } else { + $options = array('conditions' => array('State.' . $this->State->primaryKey => $id)); + $this->request->data = $this->State->find('first', $options); + } + } + +/** + * delete method + * + * @throws NotFoundException + * @param string $id + * @return void + */ + public function delete($id = null) { + $this->State->id = $id; + if (!$this->State->exists()) { + throw new NotFoundException(__('Invalid state')); + } + $this->request->allowMethod('post', 'delete'); + if ($this->State->delete()) { + return $this->flash(__('The state has been deleted.'), array('action' => 'index')); + } else { + return $this->flash(__('The state could not be deleted. Please, try again.'), array('action' => 'index')); + } + }} diff --git a/web/api/app/Model/State.php b/web/api/app/Model/State.php new file mode 100644 index 000000000..651a9cc09 --- /dev/null +++ b/web/api/app/Model/State.php @@ -0,0 +1,30 @@ +