Permalink
Browse files

Add beginnings of Controller_API_ORM .. Basically a really dumb idea,…

… but whatever!
  • Loading branch information...
1 parent cf3c987 commit fcf6c8c01185e8ef1526c8b4a530b5a463ace0b9 @kiall kiall committed Aug 24, 2011
Showing with 207 additions and 1 deletion.
  1. +3 −0 README.md
  2. +1 −1 classes/controller/api.php
  3. +203 −0 classes/controller/api/orm.php
View
@@ -0,0 +1,3 @@
+# Kohana API Module
+
+This is a work in progress.. don't use it! ;)
@@ -219,7 +219,7 @@ protected function _execute($action)
'code' => $e->getCode(),
);
- $this->_response_links = NULL;
+ $this->_response_links = array();
}
}
@@ -0,0 +1,203 @@
+<?php
+
+abstract class Controller_API_ORM extends Controller_API {
+
+ /**
+ * @var string Model Name
+ */
+ protected $_model_name = NULL;
+
+ /**
+ * @var integer Default number of results per request
+ */
+ protected $_limit_default = 25;
+
+ /**
+ * @var integer Max results per request
+ */
+ protected $_limit_max = 100;
+
+ public function before()
+ {
+ parent::before();
+
+ if ($this->_model_name === NULL)
+ throw new Kohana_Exception('A model name is required');
+ }
+
+ public function after()
+ {
+ $this->_response_metadata += array(
+ 'type' => $this->_model_name,
+ );
+
+ $this->_response_links += array(
+ 'create' => $this->_generate_link('POST', Route::url('api', array(
+ 'controller' => $this->request->controller(),
+ 'id' => NULL,
+ ))),
+ 'read' => $this->_generate_link('GET', Route::url('api', array(
+ 'controller' => $this->request->controller(),
+ 'id' => $this->request->param('id'),
+ )), array(
+ ':id' => 'id',
+ )),
+ 'update' => $this->_generate_link('PUT', Route::url('api', array(
+ 'controller' => $this->request->controller(),
+ 'id' => $this->request->param('id'),
+ )), array(
+ ':id' => 'id',
+ )),
+ 'delete' => $this->_generate_link('DELETE', Route::url('api', array(
+ 'controller' => $this->request->controller(),
+ 'id' => $this->request->param('id'),
+ )), array(
+ ':id' => 'id',
+ )),
+ );
+
+ parent::after();
+ }
+
+ protected function _execute($action) {
+ try
+ {
+ parent::_execute($action);
+ }
+ catch (ORM_Validation_Exception $e)
+ {
+ $this->response->status(400);
+
+ $this->_response_metadata = array(
+ 'error' => TRUE,
+ 'type' => 'error_validation',
+ );
+
+ $this->_response_links = array();
+
+ $this->_response_payload = array(
+ 'errors' => $e->errors(),
+ );
+ }
+ }
+
+ /**
+ * GET /api/:model_name_plural/:id
+ */
+ public function get() {
+ $id = $this->request->param('id', FALSE);
+
+ $object = ORM::factory($this->_model_name, $id);
+
+ $this->_response_payload = $object->as_array();
+
+ }
+
+ /**
+ * GET /api/:model_name_plural
+ */
+ public function get_collection() {
+ $model = ORM::factory($this->_model_name);
+
+ // Apply filters
+
+ $cmodel = clone $model;
+
+ // Apply Limit/Offset
+ $limit = $this->request->query('limit');
+ $offset = $this->request->query('offset');
+
+ if ($offset === NULL) {
+ $offset = 0;
+ }
+
+ if ($limit === NULL) {
+ $limit = $this->_limit_default;
+ } else if ((int) $limit > $this->_limit_max) {
+ $limit = $this->_limit_max;
+ }
+
+ $model->limit($limit)->offset($offset);
+
+ // Lets go ..
+ $objects_total_count = $cmodel->count_all();
+ $objects = $model->find_all();
+
+ // Prepare response
+ $this->_response_metadata += array(
+ 'total' => (int) $objects_total_count,
+ 'fetched' => (int) $objects->count(),
+ 'offset' => (int) $offset,
+ 'limit' => (int) $limit,
+ );
+
+ $this->_response_payload = $objects->map(function($val) {
+ return $val->as_array();
+ });
+ }
+
+ /**
+ * Create a new company
+ *
+ * POST /api/:model_name_plural
+ */
+ public function post_collection() {
+ $object = ORM::factory($this->_model_name);
+
+// $object->values($this->_request_payload, array(
+// 'name',
+// ))->save();
+
+ $object->values($this->_request_payload)->save();
+
+ $this->_response_payload = $object->as_array();
+ }
+
+ /**
+ * Update a company
+ *
+ * PUT /api/:model_name_plural/:id
+ */
+ public function put() {
+ $id = $this->request->param('id', FALSE);
+
+ $object = ORM::factory($this->_model_name, $id);
+
+// $object->values($this->_request_payload, array(
+// 'name',
+// ))->save();
+
+ $object->values($this->_request_payload)->save();
+
+ $this->_response_payload = $object->as_array();
+ }
+
+ /**
+ * Delete a company
+ *
+ * DELETE /api/:model_name_plural/:id
+ */
+ public function delete() {
+ $id = $this->request->param('id', FALSE);
+
+ $object = ORM::factory($this->_model_name, $id);
+
+ $object->values($this->_request_payload, array(
+ 'name',
+ ))->save();
+ }
+
+ /**
+ * Delete all companies
+ *
+ * DELETE /api/:model_name_plural
+ */
+ public function delete_collection() {
+ $objects = ORM::factory($this->_model_name)->find_all();
+
+ foreach ($objects as $object) {
+ $object->delete();
+ }
+ }
+
+}

0 comments on commit fcf6c8c

Please sign in to comment.