preliminary claims api implementation

This commit is contained in:
Akinwale Ariwodola 2017-06-18 11:08:50 +01:00
parent 2d59ca30f1
commit 3a92dbc0fc
4 changed files with 52 additions and 11 deletions

View file

@ -64,6 +64,8 @@ Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/api/v1/status', ['controller' => 'Main', 'action' => 'apistatus']); $routes->connect('/api/v1/status', ['controller' => 'Main', 'action' => 'apistatus']);
//$routes->connect('/api/v1/recenttxs', ['controller' => 'Main', 'action' => 'apirecenttxs']); //$routes->connect('/api/v1/recenttxs', ['controller' => 'Main', 'action' => 'apirecenttxs']);
$routes->connect('/api/v1/claims/recent', ['controller' => 'Claims', 'action' => 'apirecent']);
//$routes->fallbacks(DashedRoute::class); //$routes->fallbacks(DashedRoute::class);
}); });

View file

@ -66,4 +66,15 @@ class AppController extends Controller
$this->set('_serialize', true); $this->set('_serialize', true);
} }
} }
protected function _jsonResponse($object = [], $statusCode = null)
{
$this->response->statusCode($statusCode);
$this->response->type('json');
$this->response->body(json_encode($object));
}
protected function _jsonError($message, $statusCode = null) {
return $this->_jsonResponse(['error' => true, 'message' => $message], $statusCode);
}
} }

View file

@ -0,0 +1,39 @@
<?php
namespace App\Controller;
use Cake\Datasource\ConnectionManager;
use Cake\Log\Log;
class ClaimsController extends AppController {
public function apirecent() {
$this->autoRender = false;
$this->loadModel('Claims');
$offset = 0;
$pageLimit = 24;
$page = intval($this->request->query('page'));
$conn = ConnectionManager::get('default');
$stmt = $conn->execute('SELECT COUNT(Id) AS Total FROM Claims WHERE ThumbnailUrl IS NOT NULL AND LENGTH(TRIM(ThumbnailUrl)) > 0');
$count = $stmt->fetch(\PDO::FETCH_OBJ);
$numClaims = $count->Total;
$numPages = ceil($numClaims / $pageLimit);
if ($page < 1) {
$page = 1;
}
if ($page > $numPages) {
$page = $numPages;
}
$offset = ($page - 1) * $pageLimit;
$claims = $this->Claims->find()->contain(['Stream', 'Publisher' => ['fields' => ['Name']]])->where(
['Claims.ThumbnailUrl IS NOT' => null, 'LENGTH(TRIM(Claims.ThumbnailUrl)) >' => 0])->
order(['Claims.Created' => 'DESC'])->offset($offset)->limit($pageLimit)->toArray();
return $this->_jsonResponse(['success' => true, 'claims' => $claims, 'num_pages' => $numPages, 'total' => (int) $numClaims]);
}
}
?>

View file

@ -799,17 +799,6 @@ class MainController extends AppController {
return $this->_jsonResponse(['success' => true, 'utxo' => $utxo]); return $this->_jsonResponse(['success' => true, 'utxo' => $utxo]);
} }
protected function _jsonResponse($object = [], $statusCode = null)
{
$this->response->statusCode($statusCode);
$this->response->type('json');
$this->response->body(json_encode($object));
}
protected function _jsonError($message, $statusCode = null) {
return $this->_jsonResponse(['error' => true, 'message' => $message], $statusCode);
}
private static function curl_json_post($url, $data, $headers = []) { private static function curl_json_post($url, $data, $headers = []) {
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);