Skip to content

Commit e2f4e30

Browse files
authored
API: filter notes by category (introduce v1.1)
1 parent 79ae40e commit e2f4e30

File tree

7 files changed

+49
-13
lines changed

7 files changed

+49
-13
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"guzzlehttp/guzzle": "^6.5",
77
"staabm/annotate-pull-request-from-checkstyle": "^1.1.0"
88
},
9-
"autoload": {
9+
"autoload-dev": {
1010
"psr-4": {
1111
"OCA\\Notes\\Tests\\API\\": "tests/api/"
1212
}

docs/api/v1.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ In this document, the Notes API major version 1 and all its minor versions are d
88
| API version | Introduced with app version | Remarkable Changes |
99
|:-----------:|:----------------------------|:-------------------|
1010
| **1.0** | Notes 3.3 (May 2020) | Separate title, no auto rename based on content |
11+
| **1.1** | Notes 3.4 (May 2020) | Filter "Get all notes" by category |
1112

1213

1314

@@ -41,11 +42,12 @@ All defined routes in the specification are appended to this url. To access all
4142
<details><summary>Details</summary>
4243

4344
#### Request parameters
44-
| Parameter | Type | Description |
45-
|:------|:-----|:-----|
46-
| `exclude` | string, optional | Fields which should be excluded from response, seperated with a comma e.g.: `?exclude=content,title`. You can use this in order to reduce transferred data size if you are interested in specific attributes, only. |
47-
| `purgeBefore` | integer, optional | All notes without change before of this Unix timestamp are purged from the response, i.e. only the attribute `id` is included. You should use the Unix timestamp value from the last request's HTTP response header `Last-Modified` in order to reduce transferred data size. |
48-
| `If-None-Match` | HTTP header, optional | Use this in order to reduce transferred data size (see [HTTP ETag](https://en.wikipedia.org/wiki/HTTP_ETag)). You should use the value from the last request's HTTP response header `ETag`. |
45+
| Parameter | Type | Description | since API version |
46+
|:------|:-----|:-----|:-----|
47+
| `category` | string, optional | Filter the result by category name, e.g. `?category=recipes`. Notes with another category are not included in the result. *Compatibility note:* in API v1.0, this parameter is ignored; i.e., the result contains all notes regardless of this parameter. | 1.1 |
48+
| `exclude` | string, optional | Fields which should be excluded from response, seperated with a comma e.g.: `?exclude=content,title`. You can use this in order to reduce transferred data size if you are interested in specific attributes, only. | 1.0 |
49+
| `purgeBefore` | integer, optional | All notes without change before of this Unix timestamp are purged from the response, i.e. only the attribute `id` is included. You should use the Unix timestamp value from the last request's HTTP response header `Last-Modified` in order to reduce transferred data size. | 1.0 |
50+
| `If-None-Match` | HTTP header, optional | Use this in order to reduce transferred data size (see [HTTP ETag](https://en.wikipedia.org/wiki/HTTP_ETag)). You should use the value from the last request's HTTP response header `ETag`. | 1.0 |
4951

5052
#### Response
5153
##### 200 OK

lib/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Application extends App {
88

9-
public static $API_VERSIONS = [ '0.2', '1.0' ];
9+
public static $API_VERSIONS = [ '0.2', '1.1' ];
1010

1111
public function __construct(array $urlParams = []) {
1212
parent::__construct('notes', $urlParams);

lib/Controller/NotesApiController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ private function getUID() : string {
4747
* @CORS
4848
* @NoCSRFRequired
4949
*/
50-
public function index(string $exclude = '', int $pruneBefore = 0) : JSONResponse {
51-
return $this->helper->handleErrorResponse(function () use ($exclude, $pruneBefore) {
50+
public function index(?string $category = null, string $exclude = '', int $pruneBefore = 0) : JSONResponse {
51+
return $this->helper->handleErrorResponse(function () use ($category, $exclude, $pruneBefore) {
5252
$exclude = explode(',', $exclude);
5353
$now = new \DateTime(); // this must be before loading notes if there are concurrent changes possible
5454
$notes = $this->service->getAll($this->getUID())['notes'];
55+
if ($category !== null) {
56+
$notes = array_values(array_filter($notes, function ($note) use ($category) {
57+
return $note->getCategory() === $category;
58+
}));
59+
}
5560
$metas = $this->metaService->updateAll($this->getUID(), $notes);
5661
$notesData = array_map(function ($note) use ($metas, $pruneBefore, $exclude) {
5762
$lastUpdate = $metas[$note->getId()]->getLastUpdate();

tests/api/APIv1Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
class APIv1Test extends CommonAPITest {
66

77
public function __construct() {
8-
parent::__construct('1.0', false);
8+
parent::__construct('1.1', false);
99
}
1010
}

tests/api/AbstractAPITest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ public function __construct(string $apiVersion) {
1313
$this->apiVersion = $apiVersion;
1414
}
1515

16-
protected function setUp() : void {
16+
protected function getAPIMajorVersion() {
1717
if ($this->apiVersion === '0.2') {
18-
$v = $this->apiVersion;
18+
return $this->apiVersion;
1919
} else {
20-
$v = intval($this->apiVersion);
20+
return intval($this->apiVersion);
2121
}
22+
}
23+
24+
protected function setUp() : void {
25+
$v = $this->getAPIMajorVersion();
2226
$this->http = new \GuzzleHttp\Client([
2327
'base_uri' => 'http://localhost:8080/index.php/apps/notes/api/v'.$v.'/',
2428
'auth' => ['test', 'test'],

tests/api/CommonAPITest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,31 @@ public function testGetSingleNote(array $refNotes, array $testNotes) : void {
144144
$this->assertEquals(404, $response->getStatusCode());
145145
}
146146

147+
/**
148+
* @depends testCheckForReferenceNotes
149+
* @depends testCreateNotes
150+
*/
151+
public function testGetNotesWithCategory(array $refNotes, array $testNotes) : void {
152+
if ($this->getAPIMajorVersion() < 1) {
153+
$this->markTestSkipped('Get Notes with Category requires API v1');
154+
}
155+
$allNotes = array_merge($refNotes, $testNotes);
156+
$this->checkGetReferenceNotes($allNotes, 'Pre-condition');
157+
$note = $testNotes[0];
158+
$category = $note->category;
159+
$filteredNotes = array_filter(
160+
$allNotes,
161+
function ($note) use ($category) {
162+
return $category === $note->category;
163+
}
164+
);
165+
$this->assertNotEmpty($filteredNotes, 'Filtered notes');
166+
$this->checkGetReferenceNotes(
167+
$filteredNotes,
168+
'Get notes with category '.$category,
169+
'?category='.urlencode($category)
170+
);
171+
}
147172

148173
/**
149174
* @depends testCheckForReferenceNotes

0 commit comments

Comments
 (0)