Skip to content

Commit

Permalink
add api route: pages/query
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-peugnet committed Dec 5, 2022
1 parent 6b123f2 commit a0d2d21
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
46 changes: 46 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ In case of success, you will get a `200` HTTP CODE and recieve the full JSON pag

possible error codes:

- `400` if the POST datas are not recieved or in case of JSON decoding error
- `401` if user does'nt have the rights to update the page
- `404` if page is not found
- `406` in case of invalid ID
Expand Down Expand Up @@ -138,6 +139,39 @@ possibles error codes:
- `401` if user does'nt have the rights to view list of page


### query

List pages IDs using a given filters and sorting options

GET /api/v1/pages/filteredlist

You need to provide a JSON storing search options.

```json
{
sortby: "datemodif",
order: 1,
tagfilter: ["cc", "dille"],
tagcompare: "AND",
tagnot: false,
authorfilter: ["vincent", "audrey"],
authorcompare: "OR",
secure: 0,
linkto: "the-pagest-page",
invert: false,
limit: 40,
since: 2022-12-09T23:27,
until: 2025-01-01T10:10
}
```

possible error codes:

- `400` if the POST datas are not recieved or in case of JSON decoding error
- `401` if user does'nt have the rights to view list of page



Usages example
--------------

Expand All @@ -161,3 +195,15 @@ Get the list of all pages.
obj = await fetch('http://localhost:8080/api/v1/pages/list')
.then(res => res.json());
```

Get the list of pages filted and sorted.

```js
const options = {sortby:"datemodif", limit: 40, tagfilter: ["galaxy", "sublime"]};
fetch('http://localhost:8080/api/v1/pages/query', {
method: "POST",
body: JSON.stringify(options),
})
.then(res => res.json())
.then(console.log);
```
27 changes: 26 additions & 1 deletion app/class/Controllerapipage.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public function update(string $page)
} elseif (!empty($_POST)) {
$datas = $_POST;
} else {
;
$this->shortresponse(400, "No POST or JSON datas recieved");
}
$update = new Page($datas);
Expand Down Expand Up @@ -155,4 +154,30 @@ public function list()
header('Content-type: application/json; charset=utf-8');
echo json_encode($this->pagemanager->list());
}

public function query()
{
if (!$this->user->iseditor()) {
http_response_code(401);
}
$json = $this->getrequestbody();
if (!empty($json)) {
try {
$datas = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
$this->shortresponse(400, "Json decoding error: " . $e->getMessage());
}
} elseif (!empty($_POST)) {
$datas = $_POST;
} else {
$this->shortresponse(400, "No POST or JSON datas recieved");
}
$opt = new Opt($datas);
$pages = $this->pagemanager->pagelist();
$pages = $this->pagemanager->pagetable($pages, $opt);
$pages = array_keys($pages);
http_response_code(200);
header('Content-type: application/json; charset=utf-8');
echo json_encode($pages);
}
}
2 changes: 1 addition & 1 deletion app/class/Opt.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class Opt extends Item

public function __construct(array $data = [])
{
$this->hydrate($data);
$page = new Page();
$this->pagevarlist = ($page->getobjectvars());
$this->hydrate($data);
}


Expand Down
1 change: 1 addition & 0 deletions app/class/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function match()
$router->addRoutes([
['GET', '/api/v1/[cid:page]', 'Controllerapipage#access', 'apipageaccess'],
['GET', '/api/v1/pages/list', 'Controllerapipage#list', 'apipagelist'],
['POST', '/api/v1/pages/query', 'Controllerapipage#query', 'apipagequery'],
['POST', '/api/v1/[cid:page]/update', 'Controllerapipage#update', 'apipageupdate'],
['POST', '/api/v1/[cid:page]/add', 'Controllerapipage#add', 'apipageadd'],
['DELETE', '/api/v1/[cid:page]', 'Controllerapipage#delete', 'apipagedelete'],
Expand Down

0 comments on commit a0d2d21

Please sign in to comment.