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