Add pagination support to Events in the API

/api/events.json now accepts the ?page querystring, e.g.
/api/events.json?page=2 or /api/events.json
This commit is contained in:
Kyle Johnson 2014-12-29 12:10:05 -05:00
parent a7d32a2cd7
commit a2cd6e1434
2 changed files with 21 additions and 7 deletions

View File

@ -12,7 +12,7 @@ class EventsController extends AppController {
* *
* @var array * @var array
*/ */
public $components = array('RequestHandler', 'Scaler', 'Image'); public $components = array('RequestHandler', 'Scaler', 'Image', 'Paginator');
/** /**
* index method * index method
@ -22,18 +22,28 @@ class EventsController extends AppController {
*/ */
public function index() { public function index() {
$this->Event->recursive = -1; $this->Event->recursive = -1;
$events = $this->Event->find('all');
// How many events to return
$this->loadModel('Config');
$limit = $this->Config->find('list', array(
'conditions' => array('Name' => 'ZM_WEB_EVENTS_PER_PAGE'),
'fields' => array('Name', 'Value')
));
$this->Paginator->settings = array(
'limit' => $limit['ZM_WEB_EVENTS_PER_PAGE'],
'order' => array('StartTime', 'MaxScore'),
'paramType' => 'querystring'
);
$events = $this->Paginator->paginate('Event');
// For each event, get its thumbnail data (path, width, height)
foreach ($events as $key => $value) { foreach ($events as $key => $value) {
$thumbData = $this->createThumbnail($value['Event']['Id']); $thumbData = $this->createThumbnail($value['Event']['Id']);
$events[$key]['thumbData'] = $thumbData; $events[$key]['thumbData'] = $thumbData;
} }
$this->set(array( $this->set(compact('events'));
'events' => $events,
'_serialize' => array('events')
));
} }
/** /**

View File

@ -1 +1,5 @@
echo json_encode($events); <?php
$array['events'] = $events;
$array['pagination'] = $this->Paginator->params();
echo json_encode($array);
?>