diff --git a/lib/Controller/BoardApiController.php b/lib/Controller/BoardApiController.php index d7afc6dcc..cc974f083 100644 --- a/lib/Controller/BoardApiController.php +++ b/lib/Controller/BoardApiController.php @@ -24,6 +24,7 @@ namespace OCA\Deck\Controller; +use OCA\Deck\Db\Board; use OCA\Deck\StatusException; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http; @@ -72,7 +73,11 @@ class BoardApiController extends ApiController { } $boards = $this->boardService->findAll($date->getTimestamp(), $details); } - return new DataResponse($boards, HTTP::STATUS_OK); + $response = new DataResponse($boards, HTTP::STATUS_OK); + $response->setETag(md5(json_encode(array_map(function (Board $board) { + return $board->getId() . '-' . $board->getETag(); + }, $boards)))); + return $response; } /** diff --git a/lib/Db/Label.php b/lib/Db/Label.php index 830a66d9f..b6e6b414f 100644 --- a/lib/Db/Label.php +++ b/lib/Db/Label.php @@ -36,4 +36,8 @@ class Label extends RelationalEntity { $this->addType('cardId', 'integer'); $this->addType('lastModified', 'integer'); } + + public function getETag() { + return md5((string)$this->getLastModified()); + } } diff --git a/lib/Db/RelationalEntity.php b/lib/Db/RelationalEntity.php index 5da532daa..7f21a9a7e 100644 --- a/lib/Db/RelationalEntity.php +++ b/lib/Db/RelationalEntity.php @@ -80,6 +80,9 @@ class RelationalEntity extends Entity implements \JsonSerializable { $json[$property] = $value; } } + if ($reflection->hasMethod('getETag')) { + $json['ETag'] = $this->getETag(); + } return $json; } diff --git a/tests/unit/Db/BoardTest.php b/tests/unit/Db/BoardTest.php index 369e13a61..e7bef3328 100644 --- a/tests/unit/Db/BoardTest.php +++ b/tests/unit/Db/BoardTest.php @@ -32,6 +32,7 @@ class BoardTest extends TestCase { 'archived' => false, 'users' => ['user1', 'user2'], 'settings' => [], + 'ETag' => $board->getETag(), ], $board->jsonSerialize()); } @@ -52,6 +53,7 @@ class BoardTest extends TestCase { 'archived' => false, 'users' => [], 'settings' => [], + 'ETag' => $board->getETag(), ], $board->jsonSerialize()); } public function testSetAcl() { @@ -80,6 +82,7 @@ class BoardTest extends TestCase { 'shared' => 1, 'users' => [], 'settings' => [], + 'ETag' => $board->getETag(), ], $board->jsonSerialize()); } } diff --git a/tests/unit/Db/CardTest.php b/tests/unit/Db/CardTest.php index 995ff2d54..01f638a07 100644 --- a/tests/unit/Db/CardTest.php +++ b/tests/unit/Db/CardTest.php @@ -84,6 +84,7 @@ class CardTest extends TestCase { 'deletedAt' => 0, 'commentsUnread' => 0, 'lastEditor' => null, + 'ETag' => $card->getETag(), ], $card->jsonSerialize()); } public function testJsonSerializeLabels() { @@ -109,6 +110,7 @@ class CardTest extends TestCase { 'deletedAt' => 0, 'commentsUnread' => 0, 'lastEditor' => null, + 'ETag' => $card->getETag(), ], $card->jsonSerialize()); } @@ -144,6 +146,7 @@ class CardTest extends TestCase { 'deletedAt' => 0, 'commentsUnread' => 0, 'lastEditor' => null, + 'ETag' => $card->getETag(), ], $card->jsonSerialize()); } } diff --git a/tests/unit/Db/LabelTest.php b/tests/unit/Db/LabelTest.php index b129c29a3..ee8a370a1 100644 --- a/tests/unit/Db/LabelTest.php +++ b/tests/unit/Db/LabelTest.php @@ -42,7 +42,8 @@ class LabelTest extends TestCase { 'boardId' => 123, 'cardId' => null, 'lastModified' => null, - 'color' => '000000' + 'color' => '000000', + 'ETag' => $label->getETag(), ], $label->jsonSerialize()); } public function testJsonSerializeCard() { @@ -54,7 +55,8 @@ class LabelTest extends TestCase { 'boardId' => null, 'cardId' => 123, 'lastModified' => null, - 'color' => '000000' + 'color' => '000000', + 'ETag' => $label->getETag(), ], $label->jsonSerialize()); } } diff --git a/tests/unit/Db/StackTest.php b/tests/unit/Db/StackTest.php index 76016d555..3ec9688e5 100644 --- a/tests/unit/Db/StackTest.php +++ b/tests/unit/Db/StackTest.php @@ -33,7 +33,7 @@ class StackTest extends \Test\TestCase { return $board; } public function testJsonSerialize() { - $board = $this->createStack(); + $stack = $this->createStack(); $this->assertEquals([ 'id' => 1, 'title' => "My Stack", @@ -41,12 +41,13 @@ class StackTest extends \Test\TestCase { 'boardId' => 1, 'deletedAt' => 0, 'lastModified' => 0, - ], $board->jsonSerialize()); + 'ETag' => $stack->getETag(), + ], $stack->jsonSerialize()); } public function testJsonSerializeWithCards() { $cards = ["foo", "bar"]; - $board = $this->createStack(); - $board->setCards($cards); + $stack = $this->createStack(); + $stack->setCards($cards); $this->assertEquals([ 'id' => 1, 'title' => "My Stack", @@ -55,6 +56,7 @@ class StackTest extends \Test\TestCase { 'cards' => ["foo", "bar"], 'deletedAt' => 0, 'lastModified' => 0, - ], $board->jsonSerialize()); + 'ETag' => $stack->getETag(), + ], $stack->jsonSerialize()); } } diff --git a/tests/unit/controller/BoardApiControllerTest.php b/tests/unit/controller/BoardApiControllerTest.php index 1f63acf4f..3fd9198f0 100644 --- a/tests/unit/controller/BoardApiControllerTest.php +++ b/tests/unit/controller/BoardApiControllerTest.php @@ -72,7 +72,7 @@ class BoardApiControllerTest extends \Test\TestCase { $expected = new DataResponse($boards, HTTP::STATUS_OK); $actual = $this->controller->index(); - + $actual->setETag(null); $this->assertEquals($expected, $actual); } @@ -90,6 +90,7 @@ class BoardApiControllerTest extends \Test\TestCase { ->will($this->returnValue($boardId)); $expected = new DataResponse($board, HTTP::STATUS_OK); + $expected->setETag($board->getETag()); $actual = $this->controller->get(); $this->assertEquals($expected, $actual); } diff --git a/tests/unit/controller/CardApiControllerTest.php b/tests/unit/controller/CardApiControllerTest.php index e4980a834..4ef894f16 100644 --- a/tests/unit/controller/CardApiControllerTest.php +++ b/tests/unit/controller/CardApiControllerTest.php @@ -72,6 +72,7 @@ class CardApiControllerTest extends \Test\TestCase { ->willReturn($card); $expected = new DataResponse($card, HTTP::STATUS_OK); + $expected->setETag($card->getETag()); $actual = $this->controller->get(); $this->assertEquals($expected, $actual); } diff --git a/tests/unit/controller/StackApiControllerTest.php b/tests/unit/controller/StackApiControllerTest.php index 5878e560f..4d30176a9 100644 --- a/tests/unit/controller/StackApiControllerTest.php +++ b/tests/unit/controller/StackApiControllerTest.php @@ -78,6 +78,7 @@ class StackApiControllerTest extends \Test\TestCase { $expected = new DataResponse($stacks, HTTP::STATUS_OK); $actual = $this->controller->index(); + $actual->setETag(null); $this->assertEquals($expected, $actual); } @@ -97,6 +98,7 @@ class StackApiControllerTest extends \Test\TestCase { ->willReturn($this->exampleStack['id']); $expected = new DataResponse($stack, HTTP::STATUS_OK); + $expected->setETag($stack->getETag()); $actual = $this->controller->get(); $this->assertEquals($expected, $actual); }