|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Controller\BaseController; |
|
6
|
|
|
use App\Service\UserService; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Users Controller. |
|
10
|
|
|
*/ |
|
11
|
|
View Code Duplication |
class UserController extends BaseController |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Get all users. |
|
15
|
|
|
* |
|
16
|
|
|
* @param Request $request |
|
17
|
|
|
* @param Response $response |
|
18
|
|
|
* @param array $args |
|
19
|
|
|
* @return array |
|
20
|
|
|
*/ |
|
21
|
|
|
public function getUsers($request, $response, $args) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->setParams($request, $response, $args); |
|
24
|
|
|
$service = new UserService($this->database); |
|
25
|
|
|
$result = $service->getUsers(); |
|
26
|
|
|
|
|
27
|
|
|
return $this->jsonResponse('success', $result, 200); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get one user by id. |
|
32
|
|
|
* |
|
33
|
|
|
* @param Request $request |
|
34
|
|
|
* @param Response $response |
|
35
|
|
|
* @param array $args |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getUser($request, $response, $args) |
|
39
|
|
|
{ |
|
40
|
|
|
try { |
|
41
|
|
|
$this->setParams($request, $response, $args); |
|
42
|
|
|
$service = new UserService($this->database); |
|
43
|
|
|
$result = $service->getUser($this->args['id']); |
|
44
|
|
|
|
|
45
|
|
|
return $this->jsonResponse('success', $result, 200); |
|
46
|
|
|
} catch (\Exception $ex) { |
|
47
|
|
|
return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Search users by name. |
|
53
|
|
|
* |
|
54
|
|
|
* @param Request $request |
|
55
|
|
|
* @param Response $response |
|
56
|
|
|
* @param array $args |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function searchUsers($request, $response, $args) |
|
60
|
|
|
{ |
|
61
|
|
|
try { |
|
62
|
|
|
$this->setParams($request, $response, $args); |
|
63
|
|
|
$service = new UserService($this->database); |
|
64
|
|
|
$result = $service->searchUsers($this->args['query']); |
|
65
|
|
|
|
|
66
|
|
|
return $this->jsonResponse('success', $result, 200); |
|
67
|
|
|
} catch (\Exception $ex) { |
|
68
|
|
|
return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Create a user. |
|
74
|
|
|
* |
|
75
|
|
|
* @param Request $request |
|
76
|
|
|
* @param Response $response |
|
77
|
|
|
* @param array $args |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public function createUser($request, $response, $args) |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
|
|
$this->setParams($request, $response, $args); |
|
84
|
|
|
$service = new UserService($this->database); |
|
85
|
|
|
$input = $this->request->getParsedBody(); |
|
86
|
|
|
$result = $service->createUser($input); |
|
87
|
|
|
|
|
88
|
|
|
return $this->jsonResponse('success', $result, 200); |
|
89
|
|
|
} catch (\Exception $ex) { |
|
90
|
|
|
return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Update a user. |
|
96
|
|
|
* |
|
97
|
|
|
* @param Request $request |
|
98
|
|
|
* @param Response $response |
|
99
|
|
|
* @param array $args |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
public function updateUser($request, $response, $args) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->setParams($request, $response, $args); |
|
105
|
|
|
$input = $this->request->getParsedBody(); |
|
106
|
|
|
$service = new UserService($this->database); |
|
107
|
|
|
try { |
|
108
|
|
|
$result = $service->updateUser($input, $this->args['id']); |
|
109
|
|
|
return $this->jsonResponse('success', $result, 200); |
|
110
|
|
|
} catch (\Exception $ex) { |
|
111
|
|
|
return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Delete a user. |
|
117
|
|
|
* |
|
118
|
|
|
* @param Request $request |
|
119
|
|
|
* @param Response $response |
|
120
|
|
|
* @param array $args |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
|
|
public function deleteUser($request, $response, $args) |
|
124
|
|
|
{ |
|
125
|
|
|
try { |
|
126
|
|
|
$this->setParams($request, $response, $args); |
|
127
|
|
|
$service = new UserService($this->database); |
|
128
|
|
|
$result = $service->deleteUser($this->args['id']); |
|
129
|
|
|
|
|
130
|
|
|
return $this->jsonResponse('success', $result, 200); |
|
131
|
|
|
} catch (\Exception $ex) { |
|
132
|
|
|
return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Constructor of the class. |
|
138
|
|
|
* |
|
139
|
|
|
* @param \Slim\Container $container |
|
140
|
|
|
*/ |
|
141
|
|
|
public function __construct(\Slim\Container $container) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->logger = $container->get('logger'); |
|
144
|
|
|
$this->database = $container->get('db'); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|