From 532ea3b8685cb141526fbcd2cd3dd13667452783 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Fri, 5 Sep 2014 18:57:58 -0700 Subject: Start to improve task Api operations and doc --- app/Model/Task.php | 6 +- docs/api-json-rpc.markdown | 300 ++++++++++++++++++++++++++++++++++++++++-- jsonrpc.php | 41 +++++- tests/functionals/ApiTest.php | 21 ++- vendor/JsonRPC/Client.php | 15 +-- vendor/JsonRPC/Server.php | 2 +- 6 files changed, 347 insertions(+), 38 deletions(-) diff --git a/app/Model/Task.php b/app/Model/Task.php index 07a2a2fd..d326c382 100644 --- a/app/Model/Task.php +++ b/app/Model/Task.php @@ -428,7 +428,7 @@ class Task extends Base // Fetch original task $original_task = $this->getById($values['id']); - if ($original_task === false) { + if (! $original_task) { return false; } @@ -730,14 +730,10 @@ class Task extends Base $v = new Validator($values, array( new Validators\Required('id', t('The id is required')), new Validators\Integer('id', t('This value must be an integer')), - new Validators\Required('color_id', t('The color is required')), - new Validators\Required('project_id', t('The project is required')), new Validators\Integer('project_id', t('This value must be an integer')), - new Validators\Required('column_id', t('The column is required')), new Validators\Integer('column_id', t('This value must be an integer')), new Validators\Integer('owner_id', t('This value must be an integer')), new Validators\Integer('score', t('This value must be an integer')), - new Validators\Required('title', t('The title is required')), new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200), new Validators\Date('date_due', t('Invalid date'), $this->getDateFormats()), )); diff --git a/docs/api-json-rpc.markdown b/docs/api-json-rpc.markdown index 4d598d0e..aef04b3e 100644 --- a/docs/api-json-rpc.markdown +++ b/docs/api-json-rpc.markdown @@ -211,60 +211,344 @@ Procedures ### createTask - Purpose: **Create a new task** -- Parameters: Key/value pair composed of the **title** (string), **description** (string, optional), **color_id** (string), **project_id** (integer), **column_id** (integer), **owner_id** (integer, optional), **score** (integer, optional), **date_due** (integer, optional), **category_id** (integer, optional) +- Parameters: + - **title** (string, required) + - **color_id** (string, required) + - **project_id** (integer, required) + - **column_id** (integer, required) + - **description** (string, optional) + - **owner_id** (integer, optional) + - **creator_id** (integer, optional) + - **score** (integer, optional) + - **date_due**: ISO8601 format (string, optional) + - **category_id** (integer, optional) - Result on success: **true** - Result on failure: **false** +Request example: + +```json +{ + "jsonrpc": "2.0", + "method": "createTask", + "id": 1176509098, + "params": { + "owner_id": 1, + "creator_id": 0, + "date_due": "", + "description": "", + "category_id": 0, + "score": 0, + "title": "Test", + "project_id": 1, + "color_id": "green", + "column_id": 2 + } +} +``` + +Response example: + +```json +{ + "jsonrpc": "2.0", + "id": 1176509098, + "result": true +} +``` + ### getTask - Purpose: **Get task information** -- Parameters: **task_id** (integer) +- Parameters: + - **task_id** (integer, required) - Result on success: **task properties** - Result on failure: **null** +Request example: + +```json +{ + "jsonrpc": "2.0", + "method": "getTask", + "id": 700738119, + "params": { + "task_id": 1 + } +} +``` + +Response example: + +```json +{ + "jsonrpc": "2.0", + "id": 700738119, + "result": { + "id": "1", + "title": "Task #1", + "description": "", + "date_creation": "1409963206", + "color_id": "blue", + "project_id": "1", + "column_id": "2", + "owner_id": "1", + "position": "1", + "is_active": "1", + "date_completed": null, + "score": "0", + "date_due": "0", + "category_id": "0", + "creator_id": "0", + "date_modification": "1409963206" + } +} +``` + ### getAllTasks - Purpose: **Get all available tasks** -- Parameters: **project_id** (integer) +- Parameters: + - **project_id** (integer, required) + - **status**: List of status id, the value 1 for active tasks and 0 for inactive (list, required) - Result on success: **List of tasks** - Result on failure: **false** +Request example to fetch all tasks on the board: + +```json +{ + "jsonrpc": "2.0", + "method": "getAllTasks", + "id": 133280317, + "params": { + "project_id": 1, + "status": [ + 1 + ] + } +} +``` + +Response example: + +```json +{ + "jsonrpc": "2.0", + "id": 133280317, + "result": [ + { + "id": "1", + "title": "Task #1", + "description": "", + "date_creation": "1409961789", + "color_id": "blue", + "project_id": "1", + "column_id": "2", + "owner_id": "1", + "position": "1", + "is_active": "1", + "date_completed": null, + "score": "0", + "date_due": "0", + "category_id": "0", + "creator_id": "0", + "date_modification": "1409961789" + }, + { + "id": "2", + "title": "Test", + "description": "", + "date_creation": "1409962115", + "color_id": "green", + "project_id": "1", + "column_id": "2", + "owner_id": "1", + "position": "2", + "is_active": "1", + "date_completed": null, + "score": "0", + "date_due": "0", + "category_id": "0", + "creator_id": "0", + "date_modification": "1409962115" + }, + ... + ] +} +``` + ### updateTask - Purpose: **Update a task** -- Parameters: Key/value pair composed of the **id** (integer), **title** (string), **description** (string, optional), **color_id** (string), **project_id** (integer), **column_id** (integer), **owner_id** (integer, optional), **score** (integer, optional), **date_due** (integer, optional), **category_id** (integer, optional) +- Parameters: + - **id** (integer, required) + - **title** (string, optional) + - **color_id** (string, optional) + - **project_id** (integer, optional) + - **column_id** (integer, optional) + - **description** (string, optional) + - **owner_id** (integer, optional) + - **creator_id** (integer, optional) + - **score** (integer, optional) + - **date_due**: ISO8601 format (string, optional) + - **category_id** (integer, optional) - Result on success: **true** - Result on failure: **false** +Request example to change the task color: + +```json +{ + "jsonrpc": "2.0", + "method": "updateTask", + "id": 1406803059, + "params": { + "id": 1, + "color_id": "blue" + } +} +``` + +Response example: + +```json +{ + "jsonrpc": "2.0", + "id": 1406803059, + "result": true +} +``` + ### openTask - Purpose: **Set a task to the status open** -- Parameters: **task_id** (integer) +- Parameters: + - **task_id** (integer, required) - Result on success: **true** - Result on failure: **false** +Request example: + +```json +{ + "jsonrpc": "2.0", + "method": "openTask", + "id": 1888531925, + "params": { + "task_id": 1 + } +} +``` + +Response example: + +```json +{ + "jsonrpc": "2.0", + "id": 1888531925, + "result": true +} +``` + ### closeTask - Purpose: **Set a task to the status close** -- Parameters: **task_id** (integer) +- Parameters: + - **task_id** (integer, required) - Result on success: **true** - Result on failure: **false** +Request example: + +```json +{ + "jsonrpc": "2.0", + "method": "closeTask", + "id": 1654396960, + "params": { + "task_id": 1 + } +} +``` + +Response example: + +```json +{ + "jsonrpc": "2.0", + "id": 1654396960, + "result": true +} +``` + ### removeTask - Purpose: **Remove a task** -- Parameters: **task_id** (integer) +- Parameters: + - **task_id** (integer, required) - Result on success: **true** - Result on failure: **false** +Request example: + +```json +{ + "jsonrpc": "2.0", + "method": "removeTask", + "id": 1423501287, + "params": { + "task_id": 1 + } +} +``` + +Response example: + +```json +{ + "jsonrpc": "2.0", + "id": 1423501287, + "result": true +} +``` + ### moveTaskPosition - Purpose: **Move a task to another column or another position** -- Parameters: **project_id** (integer), **task_id** (integer), **column_id** (integer), **position** (integer) +- Parameters: + - **project_id** (integer, required) + - **task_id** (integer, required) + - **column_id** (integer, required) + - **position** (integer, required) - Result on success: **true** - Result on failure: **false** +Request example: + +```json +{ + "jsonrpc": "2.0", + "method": "moveTaskPosition", + "id": 117211800, + "params": { + "project_id": 1, + "task_id": 1, + "column_id": 2, + "position": 1 + } +} +``` + +Response example: +```json +{ + "jsonrpc": "2.0", + "id": 117211800, + "result": true +} +``` ### createUser diff --git a/jsonrpc.php b/jsonrpc.php index 22b44df6..e4e2a778 100644 --- a/jsonrpc.php +++ b/jsonrpc.php @@ -116,7 +116,21 @@ $server->register('allowUser', function($project_id, $user_id) use ($project) { /** * Task procedures */ -$server->register('createTask', function(array $values) use ($task) { +$server->register('createTask', function($title, $project_id, $color_id, $column_id, $owner_id = 0, $creator_id = 0, $date_due = '', $description = '', $category_id = 0, $score = 0) use ($task) { + + $values = array( + 'title' => $title, + 'project_id' => $project_id, + 'color_id' => $color_id, + 'column_id' => $column_id, + 'owner_id' => $owner_id, + 'creator_id' => $creator_id, + 'date_due' => $date_due, + 'description' => $description, + 'category_id' => $category_id, + 'score' => $score, + ); + list($valid,) = $task->validateCreation($values); return $valid && $task->create($values) !== false; }); @@ -129,8 +143,29 @@ $server->register('getAllTasks', function($project_id, array $status) use ($task return $task->getAll($project_id, $status); }); -$server->register('updateTask', function($values) use ($task) { - list($valid,) = $task->validateModification($values); +$server->register('updateTask', function($id, $title = null, $project_id = null, $color_id = null, $column_id = null, $owner_id = null, $creator_id = null, $date_due = null, $description = null, $category_id = null, $score = null) use ($task) { + + $values = array( + 'id' => $id, + 'title' => $title, + 'project_id' => $project_id, + 'color_id' => $color_id, + 'column_id' => $column_id, + 'owner_id' => $owner_id, + 'creator_id' => $creator_id, + 'date_due' => $date_due, + 'description' => $description, + 'category_id' => $category_id, + 'score' => $score, + ); + + foreach ($values as $key => $value) { + if (is_null($value)) { + unset($values[$key]); + } + } + + list($valid) = $task->validateModification($values); return $valid && $task->update($values); }); diff --git a/tests/functionals/ApiTest.php b/tests/functionals/ApiTest.php index b41db6fc..9d347527 100644 --- a/tests/functionals/ApiTest.php +++ b/tests/functionals/ApiTest.php @@ -11,7 +11,7 @@ class Api extends PHPUnit_Framework_TestCase public function setUp() { - $this->client = new JsonRPC\Client(self::URL, 5, true); + $this->client = new JsonRPC\Client(self::URL); $this->client->authentication('jsonrpc', self::KEY); $pdo = new PDO('sqlite:data/db.sqlite'); @@ -132,7 +132,7 @@ class Api extends PHPUnit_Framework_TestCase 'column_id' => 2, ); - $this->assertTrue($this->client->createTask($task)); + $this->assertTrue($this->client->execute('createTask', $task)); $task = array( 'title' => 'Task #1', @@ -140,7 +140,7 @@ class Api extends PHPUnit_Framework_TestCase 'owner_id' => 1, ); - $this->assertFalse($this->client->createTask($task)); + $this->assertNull($this->client->createTask($task)); } public function testGetTask() @@ -175,7 +175,7 @@ class Api extends PHPUnit_Framework_TestCase $task['description'] = 'test'; $task['date_due'] = ''; - $this->assertTrue($this->client->updateTask($task)); + $this->assertTrue($this->client->execute('updateTask', $task)); } public function testRemoveTask() @@ -278,7 +278,7 @@ class Api extends PHPUnit_Framework_TestCase 'column_id' => 1, ); - $this->assertTrue($this->client->createTask($task)); + $this->assertTrue($this->client->execute('createTask', $task)); $comment = array( 'task_id' => 1, @@ -407,8 +407,15 @@ class Api extends PHPUnit_Framework_TestCase 'column_id' => 1, ); - $this->assertTrue($this->client->createTask($task)); + $this->assertTrue($this->client->execute('createTask', $task)); - $this->assertTrue($this->client->moveTaskPosition(1, 2, 3, 1)); + $this->assertTrue($this->client->moveTaskPosition(1, 1, 3, 1)); + + $task = $this->client->getTask(1); + + $this->assertNotFalse($task); + $this->assertTrue(is_array($task)); + $this->assertEquals(1, $task['position']); + $this->assertEquals(3, $task['column_id']); } } diff --git a/vendor/JsonRPC/Client.php b/vendor/JsonRPC/Client.php index bbdb7200..dda78094 100644 --- a/vendor/JsonRPC/Client.php +++ b/vendor/JsonRPC/Client.php @@ -27,14 +27,6 @@ class Client */ private $timeout; - /** - * Debug flag - * - * @access private - * @var bool - */ - private $debug; - /** * Username for authentication * @@ -69,14 +61,12 @@ class Client * @access public * @param string $url Server URL * @param integer $timeout Server URL - * @param bool $debug Debug flag * @param array $headers Custom HTTP headers */ - public function __construct($url, $timeout = 5, $debug = false, $headers = array()) + public function __construct($url, $timeout = 5, $headers = array()) { $this->url = $url; $this->timeout = $timeout; - $this->debug = $debug; $this->headers = array_merge($this->headers, $headers); } @@ -133,9 +123,6 @@ class Client if (isset($result['id']) && $result['id'] == $id && array_key_exists('result', $result)) { return $result['result']; } - else if ($this->debug && isset($result['error'])) { - print_r($result['error']); - } return null; } diff --git a/vendor/JsonRPC/Server.php b/vendor/JsonRPC/Server.php index 93d46cdb..f80531fd 100644 --- a/vendor/JsonRPC/Server.php +++ b/vendor/JsonRPC/Server.php @@ -177,7 +177,7 @@ class Server $params[$name] = $request_params[$name]; } else if ($p->isDefaultValueAvailable()) { - continue; + $params[$name] = $p->getDefaultValue(); } else { return false; -- cgit v1.2.3