1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Task; |
4
|
|
|
|
5
|
|
|
use App\Controller\BaseController; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Tasks Controller. |
9
|
|
|
*/ |
10
|
|
|
class TaskController extends BaseController |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param \Slim\Container $container |
14
|
|
|
*/ |
15
|
|
|
public function __construct(\Slim\Container $container) |
16
|
|
|
{ |
17
|
|
|
$this->logger = $container->get('logger'); |
18
|
|
|
$this->database = $container->get('db'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get one task by id. |
23
|
|
|
* |
24
|
|
|
* @param Request $request |
25
|
|
|
* @param Response $response |
26
|
|
|
* @param array $args |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function getTask($request, $response, $args) |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
|
|
$this->setParams($request, $response, $args); |
33
|
|
|
$result = $this->getTaskService()->getTask($this->args['id']); |
34
|
|
|
|
35
|
|
|
return $this->jsonResponse('success', $result, 200); |
36
|
|
|
} catch (\Exception $ex) { |
37
|
|
|
return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Search tasks by name. |
43
|
|
|
* |
44
|
|
|
* @param Request $request |
45
|
|
|
* @param Response $response |
46
|
|
|
* @param array $args |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function searchTasks($request, $response, $args) |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
$this->setParams($request, $response, $args); |
53
|
|
|
$result = $this->getTaskService()->searchTasks($this->args['query']); |
54
|
|
|
|
55
|
|
|
return $this->jsonResponse('success', $result, 200); |
56
|
|
|
} catch (\Exception $ex) { |
57
|
|
|
return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create a task. |
63
|
|
|
* |
64
|
|
|
* @param Request $request |
65
|
|
|
* @param Response $response |
66
|
|
|
* @param array $args |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function createTask($request, $response, $args) |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
|
|
$this->setParams($request, $response, $args); |
73
|
|
|
$input = $this->request->getParsedBody(); |
74
|
|
|
$result = $this->getTaskService()->createTask($input); |
75
|
|
|
|
76
|
|
|
return $this->jsonResponse('success', $result, 201); |
77
|
|
|
} catch (\Exception $ex) { |
78
|
|
|
return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Update a task. |
84
|
|
|
* |
85
|
|
|
* @param Request $request |
86
|
|
|
* @param Response $response |
87
|
|
|
* @param array $args |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function updateTask($request, $response, $args) |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
|
|
$this->setParams($request, $response, $args); |
94
|
|
|
$input = $this->request->getParsedBody(); |
95
|
|
|
$result = $this->getTaskService()->updateTask($input, $this->args['id']); |
96
|
|
|
|
97
|
|
|
return $this->jsonResponse('success', $result, 200); |
98
|
|
|
} catch (\Exception $ex) { |
99
|
|
|
return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Delete a task. |
105
|
|
|
* |
106
|
|
|
* @param Request $request |
107
|
|
|
* @param Response $response |
108
|
|
|
* @param array $args |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function deleteTask($request, $response, $args) |
112
|
|
|
{ |
113
|
|
|
try { |
114
|
|
|
$this->setParams($request, $response, $args); |
115
|
|
|
$result = $this->getTaskService()->deleteTask($this->args['id']); |
116
|
|
|
|
117
|
|
|
return $this->jsonResponse('success', $result, 200); |
118
|
|
|
} catch (\Exception $ex) { |
119
|
|
|
return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|