mirror of
https://bitbucket.org/studiotem/jandl-taskmanager-app.git
synced 2026-10-21 06:20:21 +00:00
refactor(SubTaskController): improve code formatting and consistency in validation and response handling
This commit is contained in:
1 parent
b101da8da5
commit
b7bad5f985
2 files changed
+22
-24
No files matched your search
@@ -1 +0,0 @@
|
|||||||
VUE_APP_WAXIOS_URL=http://127.0.0.1:8001/api/
|
|
||||||
@@ -43,7 +43,7 @@ class SubTaskController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$validator->after(function ($validator) use ($parameters) {
|
$validator->after(function ($validator) use ($parameters) {
|
||||||
if ( ! isset($parameters['employee_id']) && ! isset($parameters['team_id'])) {
|
if (! isset($parameters['employee_id']) && ! isset($parameters['team_id'])) {
|
||||||
$validator->errors()->add(
|
$validator->errors()->add(
|
||||||
'error', 'You must provide either employee_id or team_id.'
|
'error', 'You must provide either employee_id or team_id.'
|
||||||
);
|
);
|
||||||
@@ -116,7 +116,7 @@ class SubTaskController extends Controller
|
|||||||
->get();
|
->get();
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'subtasks' => SubTaskResource::collection($subtasks)
|
'subtasks' => SubTaskResource::collection($subtasks),
|
||||||
], 200);
|
], 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +141,7 @@ class SubTaskController extends Controller
|
|||||||
->get();
|
->get();
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'subtasks' => SubTaskResource::collection($subtasks)
|
'subtasks' => SubTaskResource::collection($subtasks),
|
||||||
], 200);
|
], 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,7 +162,7 @@ class SubTaskController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'subtasks' => SubTaskResource::collection($query->get())
|
'subtasks' => SubTaskResource::collection($query->get()),
|
||||||
], 200);
|
], 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,7 +231,7 @@ class SubTaskController extends Controller
|
|||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Draft created successfully',
|
'message' => 'Draft created successfully',
|
||||||
'subtasks' => SubTaskResource::collection([$subtask])
|
'subtasks' => SubTaskResource::collection([$subtask]),
|
||||||
], 201);
|
], 201);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,7 +255,7 @@ class SubTaskController extends Controller
|
|||||||
|
|
||||||
// syncnutie timeblocks
|
// syncnutie timeblocks
|
||||||
foreach ($request->all()['data']['employees'] as $key => $employee) {
|
foreach ($request->all()['data']['employees'] as $key => $employee) {
|
||||||
if ( ! isset($data['data']['employees'][$key]['timeblocks']) || $data['data']['employees'][$key]['timeblocks'] == null) {
|
if (! isset($data['data']['employees'][$key]['timeblocks']) || $data['data']['employees'][$key]['timeblocks'] == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,7 +278,7 @@ class SubTaskController extends Controller
|
|||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Sub Task created successfully',
|
'message' => 'Sub Task created successfully',
|
||||||
'subtasks' => SubTaskResource::collection($subtasks)
|
'subtasks' => SubTaskResource::collection($subtasks),
|
||||||
], 201);
|
], 201);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -287,9 +287,9 @@ class SubTaskController extends Controller
|
|||||||
{
|
{
|
||||||
$subtask = SubTask::find($id);
|
$subtask = SubTask::find($id);
|
||||||
|
|
||||||
if (!$subtask) {
|
if (! $subtask) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Subtask not found.'
|
'message' => 'Subtask not found.',
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,7 +329,7 @@ class SubTaskController extends Controller
|
|||||||
if ($publishValidator->fails()) {
|
if ($publishValidator->fails()) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Draft cannot be published. Missing required fields.',
|
'message' => 'Draft cannot be published. Missing required fields.',
|
||||||
'errors' => $publishValidator->errors()
|
'errors' => $publishValidator->errors(),
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -341,7 +341,7 @@ class SubTaskController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Pre drafty je validácia duration voliteľná
|
// Pre drafty je validácia duration voliteľná
|
||||||
if (!$subtask->is_draft) {
|
if (! $subtask->is_draft) {
|
||||||
$validator = Validator::make($request->all(), [
|
$validator = Validator::make($request->all(), [
|
||||||
'data.duration' => 'required',
|
'data.duration' => 'required',
|
||||||
]);
|
]);
|
||||||
@@ -400,7 +400,7 @@ class SubTaskController extends Controller
|
|||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => $message,
|
'message' => $message,
|
||||||
'subtask' => new SubTaskResource($subtask)
|
'subtask' => new SubTaskResource($subtask),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -420,15 +420,15 @@ class SubTaskController extends Controller
|
|||||||
|
|
||||||
// Validácia a doplnenie duration z timeblocks
|
// Validácia a doplnenie duration z timeblocks
|
||||||
foreach ($employees as $key => $employee) {
|
foreach ($employees as $key => $employee) {
|
||||||
if (!isset($employee['id'])) {
|
if (! isset($employee['id'])) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Draft cannot be published. Each employee must have id.',
|
'message' => 'Draft cannot be published. Each employee must have id.',
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ak duration nie je nastavený, vypočítaj ho z timeblocks
|
// Ak duration nie je nastavený, vypočítaj ho z timeblocks
|
||||||
if (!isset($employee['duration']) || $employee['duration'] === null) {
|
if (! isset($employee['duration']) || $employee['duration'] === null) {
|
||||||
if (isset($employee['timeblocks']) && !empty($employee['timeblocks'])) {
|
if (isset($employee['timeblocks']) && ! empty($employee['timeblocks'])) {
|
||||||
$totalDuration = 0;
|
$totalDuration = 0;
|
||||||
foreach ($employee['timeblocks'] as $timeblock) {
|
foreach ($employee['timeblocks'] as $timeblock) {
|
||||||
if (isset($timeblock['start']) && isset($timeblock['end'])) {
|
if (isset($timeblock['start']) && isset($timeblock['end'])) {
|
||||||
@@ -465,7 +465,7 @@ class SubTaskController extends Controller
|
|||||||
$newSubtask = $mastertask->subtasks()->create($subtaskData);
|
$newSubtask = $mastertask->subtasks()->create($subtaskData);
|
||||||
|
|
||||||
// Sync timeblocks ak existujú
|
// Sync timeblocks ak existujú
|
||||||
if (isset($employee['timeblocks']) && !empty($employee['timeblocks'])) {
|
if (isset($employee['timeblocks']) && ! empty($employee['timeblocks'])) {
|
||||||
$newSubtask->syncTimeblocks($employee['timeblocks']);
|
$newSubtask->syncTimeblocks($employee['timeblocks']);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,7 +489,7 @@ class SubTaskController extends Controller
|
|||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Draft published successfully',
|
'message' => 'Draft published successfully',
|
||||||
'subtasks' => SubTaskResource::collection($createdSubtasks)
|
'subtasks' => SubTaskResource::collection($createdSubtasks),
|
||||||
], 201);
|
], 201);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -512,7 +512,7 @@ class SubTaskController extends Controller
|
|||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Subtask deleted successfully.',
|
'message' => 'Subtask deleted successfully.',
|
||||||
'subtask' => $subtask
|
'subtask' => $subtask,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -530,7 +530,7 @@ class SubTaskController extends Controller
|
|||||||
if ($subtask->user_id != $request->all()['data']['employee_id']) {
|
if ($subtask->user_id != $request->all()['data']['employee_id']) {
|
||||||
$validator->errors()->add(
|
$validator->errors()->add(
|
||||||
'data.employee_id',
|
'data.employee_id',
|
||||||
'This user cannot submit timesheet for the selected subtask. Subtask is assigned to user with id: ' . $subtask->user_id . '.'
|
'This user cannot submit timesheet for the selected subtask. Subtask is assigned to user with id: '.$subtask->user_id.'.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -543,14 +543,13 @@ class SubTaskController extends Controller
|
|||||||
'time' => $request->all()['data']['time'],
|
'time' => $request->all()['data']['time'],
|
||||||
'user_id' => $request->all()['data']['employee_id'],
|
'user_id' => $request->all()['data']['employee_id'],
|
||||||
'sub_task_id' => $subtask->id,
|
'sub_task_id' => $subtask->id,
|
||||||
"created_at" => Carbon::now(),
|
'created_at' => Carbon::now(),
|
||||||
"updated_at" => Carbon::now(),
|
'updated_at' => Carbon::now(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Subtask timesheet submitted successfully.',
|
'message' => 'Subtask timesheet submitted successfully.',
|
||||||
'subtask' => $subtask
|
'subtask' => $subtask,
|
||||||
], 201);
|
], 201);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user