Add States MVC to API

This commit is contained in:
Kyle Johnson 2015-01-17 11:18:22 -05:00
parent c238b0c10a
commit 432c3c9d82
3 changed files with 127 additions and 0 deletions

View File

@ -30,6 +30,7 @@
Router::mapResources('frames'); Router::mapResources('frames');
Router::mapResources('host'); Router::mapResources('host');
Router::mapResources('logs'); Router::mapResources('logs');
Router::mapResources('states');
Router::parseExtensions(); Router::parseExtensions();
/** /**

View File

@ -0,0 +1,96 @@
<?php
App::uses('AppController', 'Controller');
/**
* States Controller
*
* @property State $State
* @property PaginatorComponent $Paginator
*/
class StatesController extends AppController {
public $components = array('RequestHandler');
/**
* index method
*
* @return void
*/
public function index() {
$this->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'));
}
}}

View File

@ -0,0 +1,30 @@
<?php
App::uses('AppModel', 'Model');
/**
* State Model
*
*/
class State extends AppModel {
/**
* Use table
*
* @var mixed False or table name
*/
public $useTable = 'States';
/**
* Primary key field
*
* @var string
*/
public $primaryKey = 'Name';
/**
* Display field
*
* @var string
*/
public $displayField = 'Name';
}