Skip to content
Snippets Groups Projects
Commit 0368ead2 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

Index results are paginated 100 by 100

parent 80c57987
Branches
No related merge requests found
......@@ -48,7 +48,7 @@ class IndexController extends Zend_Controller_Action {
if ($this->_request->isPost())
return $this->_forward('create');
$this->_helper->json($this->_model->index(), true);
$this->_helper->json($this->_model->index($this->_getParam('page', 1)), true);
}
......
......@@ -28,16 +28,16 @@ class Restful_Bootstrap {
Zend_Controller_Front::getInstance()
->setControllerDirectory(['default' => 'application/controllers'])
->throwExceptions(true)
->getRouter()
->removeDefaultRoutes()
->addRoute('restindex', new Zend_Controller_Router_Route(':model/*'), ['page' => 1])
->addRoute('restshow',
new Zend_Controller_Router_Route(':model/:id', ['action' => 'show']))
->addRoute('restnew',
new Zend_Controller_Router_Route(':model/new', ['action' => 'new']))
->addRoute('restedit',
new Zend_Controller_Router_Route(':model/:id/edit', ['action' => 'edit']))
->addRoute('restshow',
new Zend_Controller_Router_Route(':model/:id', ['action' => 'show']))
->addRoute('restindex',
new Zend_Controller_Router_Route(':model'))
->addRoute('restcatalog',
new Zend_Controller_Router_Route_Static('catalog', ['action' => 'catalog']))
......
......@@ -45,20 +45,23 @@ class Restful_Model {
}
public function index() {
public function index($page=1, $params=[]) {
if (!$this->canIndex())
return [];
$models = call_user_func_array([$this->_storm_model, 'findAllBy'], [[]]);
$models = call_user_func_array(
[$this->_storm_model, 'findAllBy'],
[['limitPage' => [$page, $this->_configuration->getIndexLimit()]]]);
$response = [];
foreach($models as $model)
$response[] = $this->toJson($model);
return $response;
}
public function show($id) {
xdebug_break();
if (!$this->canShow()
|| !$model = call_user_func_array([$this->_storm_model, 'find'], [$id]))
return new stdclass();
......
......@@ -24,6 +24,8 @@ THE SOFTWARE.
*/
class Restful_Model_Configuration {
const DEFAULT_INDEX_LIMIT = 100;
protected static $_loaded_instances = [];
protected static $_config;
......@@ -94,24 +96,29 @@ class Restful_Model_Configuration {
}
public function getIndexLimit() {
return self::DEFAULT_INDEX_LIMIT;
}
public function getMethods() {
$router = Zend_Controller_Front::getInstance()->getRouter();
$methods = [];
if ($this->canIndex())
$methods['index'] = 'GET ' . $router->assemble(['model' => $this->_model], 'restindex', true, false);
$methods['index'] = 'GET ' . $router->assemble(['model' => $this->_model], 'restindex', true, false) . '[/page/:page]';
if ($this->canShow())
$methods['show'] = 'GET ' . $router->assemble(['model' => $this->_model, 'id' => '[:id]'], 'restshow', true, false);
$methods['show'] = 'GET ' . $router->assemble(['model' => $this->_model, 'id' => ':id'], 'restshow', true, false);
if ($this->canCreate())
$methods['create'] = 'POST ' . $router->assemble(['model' => $this->_model], 'restindex', true, false);
if ($this->canUpdate())
$methods['update'] = 'PUT ' . $router->assemble(['model' => $this->_model, 'id' => '[:id]'], 'restshow', true, false);
$methods['update'] = 'PUT ' . $router->assemble(['model' => $this->_model, 'id' => ':id'], 'restshow', true, false);
if ($this->canDelete())
$methods['delete'] = 'DELETE ' . $router->assemble(['model' => $this->_model, 'id' => '[:id]'], 'restshow', true, false);
$methods['delete'] = 'DELETE ' . $router->assemble(['model' => $this->_model, 'id' => ':id'], 'restshow', true, false);
return $methods;
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment