|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Constants\TranslationCode; |
|
6
|
|
|
use App\Models\Permission; |
|
7
|
|
|
use App\Models\RolePermission; |
|
8
|
|
|
use App\Models\User; |
|
9
|
|
|
use App\Services\LogService; |
|
10
|
|
|
use App\Services\TaskService; |
|
11
|
|
|
use Illuminate\Http\JsonResponse; |
|
12
|
|
|
use Illuminate\Http\Request; |
|
13
|
|
|
use Illuminate\Support\Facades\Auth; |
|
14
|
|
|
use Illuminate\Support\Facades\DB; |
|
15
|
|
|
use Illuminate\Support\Facades\Log; |
|
16
|
|
|
use Throwable; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class TaskController |
|
20
|
|
|
* |
|
21
|
|
|
* @package App\Http\Controllers |
|
22
|
|
|
*/ |
|
23
|
|
|
class TaskController extends Controller |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var TaskService */ |
|
26
|
|
|
private $taskService; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* UserController constructor. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
|
|
35
|
|
|
$this->taskService = new TaskService(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get users tasks |
|
40
|
|
|
* |
|
41
|
|
|
* @param Request $request |
|
42
|
|
|
* |
|
43
|
|
|
* @return JsonResponse |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getUserTasks(Request $request) |
|
46
|
|
|
{ |
|
47
|
|
|
try { |
|
48
|
|
|
/** @var User $user */ |
|
49
|
|
|
$user = Auth::user(); |
|
50
|
|
|
|
|
51
|
|
|
$userRolePermission = $this->baseService->getUserPermissionActions($user->id, Permission::ID_TASKS); |
|
52
|
|
|
|
|
53
|
|
|
if ($userRolePermission->read !== RolePermission::PERMISSION_TRUE) { |
|
54
|
|
|
return $this->forbiddenResponse(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$userTasks = $this->taskService->getUserTasksBuilder($userRolePermission->manage); |
|
58
|
|
|
|
|
59
|
|
|
if ($request->has('search')) { |
|
60
|
|
|
$userTasks = $this->baseService->applySearch($userTasks, $request->get('search')); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ($request->has('filters') && is_array($request->get('filters'))) { |
|
64
|
|
|
$userTasks = $this->baseService->applyFilters($userTasks, $request->get('filters')); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$userTasks = $this->baseService->applySortParams($request, $userTasks); |
|
68
|
|
|
|
|
69
|
|
|
$paginationParams = $this->baseService->getPaginationParams($request); |
|
70
|
|
|
|
|
71
|
|
|
$pagination = $this->baseService->getPaginationData($userTasks, $paginationParams['page'], |
|
72
|
|
|
$paginationParams['limit']); |
|
73
|
|
|
|
|
74
|
|
|
$userTasks = $userTasks->offset($paginationParams['offset'])->limit($paginationParams['limit'])->get(); |
|
75
|
|
|
|
|
76
|
|
|
return $this->successResponse($userTasks, $pagination); |
|
77
|
|
|
} catch (Throwable $t) { |
|
78
|
|
|
Log::error(LogService::getThrowableTraceAsString($t, $request)); |
|
79
|
|
|
|
|
80
|
|
|
return $this->errorResponse(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Create a task |
|
86
|
|
|
* |
|
87
|
|
|
* @param Request $request |
|
88
|
|
|
* |
|
89
|
|
|
* @return JsonResponse |
|
90
|
|
|
*/ |
|
91
|
|
|
public function createTask(Request $request) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
try { |
|
94
|
|
|
/** @var User $user */ |
|
95
|
|
|
$user = Auth::user(); |
|
96
|
|
|
|
|
97
|
|
|
$userRolePermission = $this->baseService->getUserPermissionActions($user->id, Permission::ID_TASKS); |
|
98
|
|
|
|
|
99
|
|
|
if ($userRolePermission->create !== RolePermission::PERMISSION_TRUE) { |
|
100
|
|
|
return $this->forbiddenResponse(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
//TODO |
|
104
|
|
|
|
|
105
|
|
|
return $this->successResponse(); |
|
106
|
|
|
} catch (Throwable $t) { |
|
107
|
|
|
Log::error(LogService::getThrowableTraceAsString($t)); |
|
108
|
|
|
|
|
109
|
|
|
return $this->errorResponse(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get a single task |
|
115
|
|
|
* |
|
116
|
|
|
* @param $id |
|
117
|
|
|
* |
|
118
|
|
|
* @return JsonResponse |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getTask($id) |
|
121
|
|
|
{ |
|
122
|
|
|
try { |
|
123
|
|
|
/** @var User $user */ |
|
124
|
|
|
$user = Auth::user(); |
|
125
|
|
|
|
|
126
|
|
|
$userRolePermission = $this->baseService->getUserPermissionActions($user->id, Permission::ID_TASKS); |
|
127
|
|
|
|
|
128
|
|
|
if ($userRolePermission->read !== RolePermission::PERMISSION_TRUE) { |
|
129
|
|
|
return $this->forbiddenResponse(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$userTasks = $this->taskService->getUserTasksBuilder($userRolePermission->manage); |
|
133
|
|
|
|
|
134
|
|
|
$userTask = $userTasks->where('id', $id)->first(); |
|
135
|
|
|
|
|
136
|
|
|
if (!$userTask) { |
|
137
|
|
|
return $this->userErrorResponse(['notFound' => TranslationCode::ERROR_NOT_FOUND]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $this->successResponse($userTask); |
|
141
|
|
|
} catch (Throwable $t) { |
|
142
|
|
|
Log::error(LogService::getThrowableTraceAsString($t)); |
|
143
|
|
|
|
|
144
|
|
|
return $this->errorResponse(); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Update a task |
|
150
|
|
|
* |
|
151
|
|
|
* @param $id |
|
152
|
|
|
* @param Request $request |
|
153
|
|
|
* |
|
154
|
|
|
* @return JsonResponse |
|
155
|
|
|
*/ |
|
156
|
|
|
public function updateTask($id, Request $request) |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
|
|
try { |
|
159
|
|
|
/** @var User $user */ |
|
160
|
|
|
$user = Auth::user(); |
|
161
|
|
|
|
|
162
|
|
|
$userRolePermission = $this->baseService->getUserPermissionActions($user->id, Permission::ID_TASKS); |
|
163
|
|
|
|
|
164
|
|
|
if ($userRolePermission->update !== RolePermission::PERMISSION_TRUE) { |
|
165
|
|
|
return $this->forbiddenResponse(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$userTasks = $this->taskService->getUserTasksBuilder($userRolePermission->manage); |
|
169
|
|
|
|
|
170
|
|
|
$userTask = $userTasks->where('id', $id)->first(); |
|
171
|
|
|
|
|
172
|
|
|
if (!$userTask) { |
|
173
|
|
|
return $this->userErrorResponse(['notFound' => TranslationCode::ERROR_NOT_FOUND]); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
//TODO |
|
177
|
|
|
|
|
178
|
|
|
return $this->successResponse(); |
|
179
|
|
|
} catch (Throwable $t) { |
|
180
|
|
|
Log::error(LogService::getThrowableTraceAsString($t)); |
|
181
|
|
|
|
|
182
|
|
|
return $this->errorResponse(); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Delete a task |
|
188
|
|
|
* |
|
189
|
|
|
* @param $id |
|
190
|
|
|
* |
|
191
|
|
|
* @return JsonResponse |
|
192
|
|
|
*/ |
|
193
|
|
|
public function deleteTask($id) |
|
194
|
|
|
{ |
|
195
|
|
|
try { |
|
196
|
|
|
/** @var User $user */ |
|
197
|
|
|
$user = Auth::user(); |
|
198
|
|
|
|
|
199
|
|
|
$userRolePermission = $this->baseService->getUserPermissionActions($user->id, Permission::ID_TASKS); |
|
200
|
|
|
|
|
201
|
|
|
if ($userRolePermission->delete !== RolePermission::PERMISSION_TRUE) { |
|
202
|
|
|
return $this->forbiddenResponse(); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$userTasks = $this->taskService->getUserTasksBuilder($userRolePermission->manage, true); |
|
206
|
|
|
|
|
207
|
|
|
$userTask = $userTasks->where('id', $id)->first(); |
|
208
|
|
|
|
|
209
|
|
|
if (!$userTask) { |
|
210
|
|
|
return $this->userErrorResponse(['notFound' => TranslationCode::ERROR_NOT_FOUND]); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
DB::beginTransaction(); |
|
214
|
|
|
|
|
215
|
|
|
$userTask->delete(); |
|
216
|
|
|
|
|
217
|
|
|
DB::commit(); |
|
218
|
|
|
|
|
219
|
|
|
return $this->successResponse(); |
|
220
|
|
|
} catch (Throwable $t) { |
|
221
|
|
|
Log::error(LogService::getThrowableTraceAsString($t)); |
|
222
|
|
|
|
|
223
|
|
|
return $this->errorResponse(); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|