aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV.Krishn <vkrishn4@gmail.com>2016-06-24 14:58:49 +0530
committerV.Krishn <vkrishn4@gmail.com>2016-06-24 14:58:49 +0530
commit3e96ea458b2d6f6360b664e24f0a83c4c01a6910 (patch)
tree76455a0be5d44852fa02180c7dc75ba291754c09
parent8d4c887848a47a7141cccb026941759e5c061092 (diff)
downloadaport-api-3e96ea458b2d6f6360b664e24f0a83c4c01a6910.tar.bz2
add search option row=limit-offset
-rw-r--r--README.md5
-rw-r--r--docs/api-json.md2
-rw-r--r--index.php67
3 files changed, 50 insertions, 24 deletions
diff --git a/README.md b/README.md
index d5de694..6b9235b 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,4 @@
-aports-api
-==========
+# aports-api
-Jsonapi for alpinelinux aports.
+**Jsonapi** for alpinelinux aports.
diff --git a/docs/api-json.md b/docs/api-json.md
index c1f5af1..a0fa8bd 100644
--- a/docs/api-json.md
+++ b/docs/api-json.md
@@ -3,7 +3,7 @@
# **JSONAPI** implementations for aports packages
Api is build around packages data and its relationships with other tables in database,
-using, simple one-to-one relationships (based on *foreign-key/pid/id*).
+using, simple to-one or to-many relationships (based on *foreign-key/pid/id*).
Currently, any JOINS in database is being avoided and maybe used if needed.
diff --git a/index.php b/index.php
index fdeb190..4fdebbd 100644
--- a/index.php
+++ b/index.php
@@ -199,6 +199,7 @@ $app->get('/search/{where:[a-z0-9\_]+}{filters:.*}', function($where, $filters)
$filter = set_search_category($filter);
$filter = set_search_flagged($filter);
$filter = set_search_maint($filter);
+ $filter = set_search_row($filter, $app);
if(isset($filter['page'])) $app->myapi->reqPage = (int)$filter['page'];
@@ -215,16 +216,10 @@ $app->get('/search/{where:[a-z0-9\_]+}{filters:.*}', function($where, $filters)
});
-function get2filter($f) {
- $_k = array('category', 'name', 'maintainer', 'flagged', 'sort');
- //$_k = array('name', 'branch', 'repo', 'arch', 'maintainer', 'flagged', 'sort');
- foreach($_k as $v) { if(array_key_exists($v, $_GET)) $f[$v] = mb_substr($_GET[$v], 0, 56); }
- return $f;
-}
-
/*
- Advance Search by POST (expects simple key/value pairs in post data)
+ Advance/Complex Search by POST (expects simple key/value pairs in post data)
eg. '{"name":"bas_","category":"edge:main:x86"}'
+ More complex keywords like 'include' and 'filter for columns' maybe implemented
*/
$app->post('/search/{where:[a-z0-9\_]+}', function($where) use ($app) {
$data = initJapiData($app, 'search');
@@ -271,6 +266,24 @@ function sanitize_filters($filters='', $where='', $app) {
return $filter;
}
+function set_search_row($f=array(), $app) {
+ if( ! array_key_exists('row', $f) ) return $f;
+ $rec = explode('-', $f['row']);
+ $f['limit'] = ((int)$rec[0] >= 1) ? (int)substr($rec[0], 0, 2) : 0;
+ $f['offset'] = ((int)$rec[1] >= 1) ? (int)substr($rec[1], 0, 10) : 0;
+
+ $app->myapi->pglimit = $f['limit']>=51 ? $app->myapi->pglimit : $f['limit'];
+ $f['filter']['limit'] = implode(',', array($f['limit'], $f['offset']));
+ return $f;
+}
+
+function get2filter($f=array()) {
+ $_k = array('category', 'name', 'maintainer', 'flagged', 'sort');
+ //$_k = array('name', 'branch', 'repo', 'arch', 'maintainer', 'flagged', 'sort');
+ foreach($_k as $v) { if(array_key_exists($v, $_GET)) $f[$v] = mb_substr($_GET[$v], 0, 56); }
+ return $f;
+}
+
function set_search_category($f) {
$t = array('branch', 'repo', 'arch');
$tdef = array('edge', 'main', 'x86_64'); #defaults
@@ -368,17 +381,22 @@ function get_package($filter=array(), $data=array(), $app) {
$sort = isset($filter['filter']['sort']) ? $filter['filter']['sort'] : "id DESC";
# get Packages count
- $params = array( 'conditions' => "$condt" );
+ $params = array(
+ 'conditions' => "$condt"
+ );
$res = Packages::find( $params );
$tnum = count($res);
+ $tnum = (isset($filter['offset']) && $filter['offset']<=$tnum)
+ ? $tnum-$filter['offset'] : $tnum;
setPageLinks('page', $tnum, $data, $app);
+ $_ofs = isset($filter['offset']) ? $filter['offset'] : $app->myapi->offset;
$params = array(
- "conditions" => "$condt",
- "order" => "$sort",
- "limit" => $app->myapi->pglimit,
- "offset" => $app->myapi->offset
+ 'conditions' => "$condt",
+ 'order' => "$sort",
+ 'limit' => $app->myapi->pglimit,
+ 'offset' => $_ofs
);
$res = Packages::find( $params );
@@ -670,8 +688,9 @@ $app->get('/204', function() use ($app) {
# Error Response 401
$app->get('/401', function() use ($app) {
$app->response->setStatusCode(401, "Unauthorized")->sendHeaders();
- $data = initJapiErrData($app,
- array( '401', 'Access is not authorized', '' ));
+ $data = initJapiErrData($app, array(
+ '401', 'Access is not authorized', ''
+ ));
json_api_encode($data, $app);
});
@@ -682,22 +701,29 @@ $app->get('/403', function() use ($app) {
# Error Response 404
$app->notFound(function() use ($app) {
$app->response->setStatusCode(404, "Not Found")->sendHeaders();
- $data = initJapiErrData($app,
- array( '404', '404 - Page Not Found', 'This is crazy, but this page was not found!' ));
+ $data = initJapiErrData($app, array(
+ '404',
+ '404 - Page Not Found',
+ 'This is crazy, but this page was not found!'
+ ));
json_api_encode($data, $app);
});
# Error Response 406
$app->get('/406', function() use ($app) {
$app->response->setStatusCode(406, "Not Acceptable")->sendHeaders();
- $data = initJapiErrData($app, array( '406', 'Not Acceptable', '' ));
+ $data = initJapiErrData($app, array(
+ '406', 'Not Acceptable', ''
+ ));
json_api_encode($data, $app);
});
# Error Response 415
$app->get('/415', function() use ($app) {
$app->response->setStatusCode(415, "Unsupported Media Type")->sendHeaders();
- $data = initJapiErrData($app, array( '415', 'Unsupported Media Type', '' ));
+ $data = initJapiErrData($app, array(
+ '415', 'Unsupported Media Type', ''
+ ));
json_api_encode($data, $app);
});
@@ -738,7 +764,6 @@ function isJapiReqHeader($app) { # TODO
}
function cleanUri($_reqUrl) {
- //$_reqUrl = $app->request->get('_url');
$pat = array('#\/{2}+#', '#\/{1}+$#');
$rep = array('/', '');
return preg_replace($pat, $rep, $_reqUrl);
@@ -920,6 +945,8 @@ function fmtData($res, $type, $app) {
$rels = array();
}
+ if('none' === $subtype) $rels = array();
+
foreach ($res as $item) {
$obj = (object)array();
$obj->type = $type;