claims retrieval api modifications
This commit is contained in:
parent
7853701a75
commit
74932d02a2
2 changed files with 32 additions and 15 deletions
|
@ -64,7 +64,7 @@ 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->connect('/api/v1/claims/browse', ['controller' => 'Claims', 'action' => 'apibrowse']);
|
||||||
|
|
||||||
//$routes->fallbacks(DashedRoute::class);
|
//$routes->fallbacks(DashedRoute::class);
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,33 +6,50 @@ use Cake\Datasource\ConnectionManager;
|
||||||
use Cake\Log\Log;
|
use Cake\Log\Log;
|
||||||
|
|
||||||
class ClaimsController extends AppController {
|
class ClaimsController extends AppController {
|
||||||
public function apirecent() {
|
public function apibrowse() {
|
||||||
$this->autoRender = false;
|
$this->autoRender = false;
|
||||||
$this->loadModel('Claims');
|
$this->loadModel('Claims');
|
||||||
|
|
||||||
$offset = 0;
|
|
||||||
$pageLimit = 48;
|
$pageLimit = 48;
|
||||||
$page = intval($this->request->query('page'));
|
$beforeId = intval($this->request->query('before'));
|
||||||
|
$afterId = intval($this->request->query('after'));
|
||||||
|
$sort = trim($this->request->query('sort'));
|
||||||
|
switch ($sort) {
|
||||||
|
case 'popular':
|
||||||
|
// TODO: sort by upvote/downvote score
|
||||||
|
break;
|
||||||
|
case 'random':
|
||||||
|
$order = ['RAND()' => 'ASC'];
|
||||||
|
break;
|
||||||
|
case 'oldest':
|
||||||
|
$order = ['Claims.Created' => 'ASC'];
|
||||||
|
break;
|
||||||
|
case 'newest':
|
||||||
|
default:
|
||||||
|
$order = ['Claims.Created' => 'DESC'];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
$conn = ConnectionManager::get('default');
|
$conn = ConnectionManager::get('default');
|
||||||
$stmt = $conn->execute('SELECT COUNT(Id) AS Total FROM Claims WHERE ThumbnailUrl IS NOT NULL AND LENGTH(TRIM(ThumbnailUrl)) > 0');
|
$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);
|
$count = $stmt->fetch(\PDO::FETCH_OBJ);
|
||||||
$numClaims = $count->Total;
|
$numClaims = $count->Total;
|
||||||
|
|
||||||
$numPages = ceil($numClaims / $pageLimit);
|
if ($beforeId < 0) {
|
||||||
if ($page < 1) {
|
$beforeId = 0;
|
||||||
$page = 1;
|
|
||||||
}
|
|
||||||
if ($page > $numPages) {
|
|
||||||
$page = $numPages;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$offset = ($page - 1) * $pageLimit;
|
$conditions = ['Claims.ThumbnailUrl IS NOT' => null, 'LENGTH(TRIM(Claims.ThumbnailUrl)) >' => 0];
|
||||||
$claims = $this->Claims->find()->contain(['Stream', 'Publisher' => ['fields' => ['Name']]])->where(
|
if ($afterId > 0) {
|
||||||
['Claims.ThumbnailUrl IS NOT' => null, 'LENGTH(TRIM(Claims.ThumbnailUrl)) >' => 0])->
|
$conditions['Claims.Id >'] = $afterId;
|
||||||
order(['Claims.Created' => 'DESC'])->offset($offset)->limit($pageLimit)->toArray();
|
} else if ($beforeId) {
|
||||||
|
$conditions['Claims.Id <'] = $beforeId;
|
||||||
|
}
|
||||||
|
|
||||||
return $this->_jsonResponse(['success' => true, 'claims' => $claims, 'num_pages' => $numPages, 'total' => (int) $numClaims]);
|
$claims = $this->Claims->find()->contain(['Stream', 'Publisher' => ['fields' => ['Name']]])->where($conditions)->
|
||||||
|
limit($pageLimit)->order($order)->toArray();
|
||||||
|
|
||||||
|
return $this->_jsonResponse(['success' => true, 'claims' => $claims, 'total' => (int) $numClaims]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue