aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/api/controllers/Links.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2021-01-20 14:45:59 +0100
committerArthurHoaro <arthur@hoa.ro>2021-01-20 15:01:29 +0100
commit9b8c0a4560fa1d87cab1529099b1b4677e92e265 (patch)
tree330a9b1a42ff7b7f24a76612b57fae63417ef483 /application/api/controllers/Links.php
parent055d97f9a9e67d8ee8ae81bbf59a4b846a145d9f (diff)
downloadShaarli-9b8c0a4560fa1d87cab1529099b1b4677e92e265.tar.gz
Shaarli-9b8c0a4560fa1d87cab1529099b1b4677e92e265.tar.zst
Shaarli-9b8c0a4560fa1d87cab1529099b1b4677e92e265.zip
Handle pagination through BookmarkService
Handle all search results through SearchResult object. This is a required step toward implementing a BookmarkService based on SQL database. Related to #953
Diffstat (limited to 'application/api/controllers/Links.php')
-rw-r--r--application/api/controllers/Links.php38
1 files changed, 19 insertions, 19 deletions
diff --git a/application/api/controllers/Links.php b/application/api/controllers/Links.php
index b83b2260..fe4bdc9f 100644
--- a/application/api/controllers/Links.php
+++ b/application/api/controllers/Links.php
@@ -36,13 +36,6 @@ class Links extends ApiController
36 public function getLinks($request, $response) 36 public function getLinks($request, $response)
37 { 37 {
38 $private = $request->getParam('visibility'); 38 $private = $request->getParam('visibility');
39 $bookmarks = $this->bookmarkService->search(
40 [
41 'searchtags' => $request->getParam('searchtags', ''),
42 'searchterm' => $request->getParam('searchterm', ''),
43 ],
44 $private
45 );
46 39
47 // Return bookmarks from the {offset}th link, starting from 0. 40 // Return bookmarks from the {offset}th link, starting from 0.
48 $offset = $request->getParam('offset'); 41 $offset = $request->getParam('offset');
@@ -50,9 +43,6 @@ class Links extends ApiController
50 throw new ApiBadParametersException('Invalid offset'); 43 throw new ApiBadParametersException('Invalid offset');
51 } 44 }
52 $offset = ! empty($offset) ? intval($offset) : 0; 45 $offset = ! empty($offset) ? intval($offset) : 0;
53 if ($offset > count($bookmarks)) {
54 return $response->withJson([], 200, $this->jsonStyle);
55 }
56 46
57 // limit parameter is either a number of bookmarks or 'all' for everything. 47 // limit parameter is either a number of bookmarks or 'all' for everything.
58 $limit = $request->getParam('limit'); 48 $limit = $request->getParam('limit');
@@ -61,23 +51,33 @@ class Links extends ApiController
61 } elseif (ctype_digit($limit)) { 51 } elseif (ctype_digit($limit)) {
62 $limit = intval($limit); 52 $limit = intval($limit);
63 } elseif ($limit === 'all') { 53 } elseif ($limit === 'all') {
64 $limit = count($bookmarks); 54 $limit = null;
65 } else { 55 } else {
66 throw new ApiBadParametersException('Invalid limit'); 56 throw new ApiBadParametersException('Invalid limit');
67 } 57 }
68 58
59 $searchResult = $this->bookmarkService->search(
60 [
61 'searchtags' => $request->getParam('searchtags', ''),
62 'searchterm' => $request->getParam('searchterm', ''),
63 ],
64 $private,
65 false,
66 false,
67 false,
68 [
69 'limit' => $limit,
70 'offset' => $offset,
71 'allowOutOfBounds' => true,
72 ]
73 );
74
69 // 'environment' is set by Slim and encapsulate $_SERVER. 75 // 'environment' is set by Slim and encapsulate $_SERVER.
70 $indexUrl = index_url($this->ci['environment']); 76 $indexUrl = index_url($this->ci['environment']);
71 77
72 $out = []; 78 $out = [];
73 $index = 0; 79 foreach ($searchResult->getBookmarks() as $bookmark) {
74 foreach ($bookmarks as $bookmark) { 80 $out[] = ApiUtils::formatLink($bookmark, $indexUrl);
75 if (count($out) >= $limit) {
76 break;
77 }
78 if ($index++ >= $offset) {
79 $out[] = ApiUtils::formatLink($bookmark, $indexUrl);
80 }
81 } 81 }
82 82
83 return $response->withJson($out, 200, $this->jsonStyle); 83 return $response->withJson($out, 200, $this->jsonStyle);