From 3a92dbc0fc8b83934d1b2e40fe3e60bc11398d6c Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Sun, 18 Jun 2017 11:08:50 +0100 Subject: [PATCH] preliminary claims api implementation --- config/routes.php | 2 ++ src/Controller/AppController.php | 11 ++++++++ src/Controller/ClaimsController.php | 39 +++++++++++++++++++++++++++++ src/Controller/MainController.php | 11 -------- 4 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 src/Controller/ClaimsController.php diff --git a/config/routes.php b/config/routes.php index 5267863..8a47199 100644 --- a/config/routes.php +++ b/config/routes.php @@ -64,6 +64,8 @@ Router::scope('/', function (RouteBuilder $routes) { $routes->connect('/api/v1/status', ['controller' => 'Main', 'action' => 'apistatus']); //$routes->connect('/api/v1/recenttxs', ['controller' => 'Main', 'action' => 'apirecenttxs']); + $routes->connect('/api/v1/claims/recent', ['controller' => 'Claims', 'action' => 'apirecent']); + //$routes->fallbacks(DashedRoute::class); }); diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index afd65cd..5cbfeca 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -66,4 +66,15 @@ class AppController extends Controller $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); + } } diff --git a/src/Controller/ClaimsController.php b/src/Controller/ClaimsController.php new file mode 100644 index 0000000..9de7b48 --- /dev/null +++ b/src/Controller/ClaimsController.php @@ -0,0 +1,39 @@ +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]); + } +} + +?> \ No newline at end of file diff --git a/src/Controller/MainController.php b/src/Controller/MainController.php index 0daf26f..6c8e90e 100644 --- a/src/Controller/MainController.php +++ b/src/Controller/MainController.php @@ -799,17 +799,6 @@ class MainController extends AppController { 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 = []) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);