Create assign task to user route
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Events;
|
||||
use App\Models\Task;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -63,4 +64,30 @@ class TasksController extends Controller
|
||||
return response()->json(['message' => "Server error"], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function assignUser(request $request, int $id): JsonResponse {
|
||||
|
||||
if(!$this->isAdmin($request->user()->role)){
|
||||
return response()->json(["message" => "Can't create task"], 403);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
$task = Task::find($request["task_id"]);
|
||||
|
||||
if($task->max_participants = $task->users()->count()) {
|
||||
return response()->json(["message" => "Max participants reached"], 403);
|
||||
}
|
||||
|
||||
$user = User::find($id);
|
||||
|
||||
if($user) $task->users()->attach($id);
|
||||
|
||||
return response()->json(['message' => "Task assigned"], 200);
|
||||
|
||||
} catch(\Exception $e) {
|
||||
Log::info($e->getMessage());
|
||||
return response()->json(['message' => "Server error"], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,3 +38,5 @@ Route::middleware(['web', 'auth:sanctum'])->post('/events/create', [EventsContro
|
||||
Route::middleware(['web', 'auth:sanctum'])->post('/events/task', [TasksController::class, "create"]);
|
||||
|
||||
Route::middleware(['web', 'auth:sanctum'])->post('/events/task/assign', [TasksController::class, "assignSelf"]);
|
||||
|
||||
Route::middleware(['web', 'auth:sanctum'])->post('/events/task/assign/{id}', [TasksController::class, "assignUser"]);
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
meta {
|
||||
name: Assign task to user
|
||||
type: http
|
||||
seq: 3
|
||||
}
|
||||
|
||||
post {
|
||||
url: {{url}}/api/events/task/assign/{{id}}
|
||||
body: json
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
headers {
|
||||
X-XSRF-TOKEN: eyJpdiI6Im9TOXR2dG96amtTazVlRDh0dkRLZGc9PSIsInZhbHVlIjoiaWgyNG13UXpMRnRVRlRTVTFmOVN5b20rWjhSOEdhUEFRUEo5Qm9ZUDYybTZhSnJxWjEzNWN1cEhVRm9yc3c2UE1tNkZnNFpJZHZUTXVkSC92VnFvZ0ZXT3dLNGRrT2FIRzdvdFZyTFV4M3VlMlM5MmlOc2FkVGhDeUJQczBLdS8iLCJtYWMiOiIyYWY5NjBiZjJlOTZhM2UxNGU5MDg1YTIwNTIzZjFjNzQyNDAzODYwMDI4MzFkOGM2ZWQyNTlkZWVkZjg3MzhhIiwidGFnIjoiIn0=
|
||||
}
|
||||
|
||||
body:json {
|
||||
{
|
||||
"task_id": "{{task_id}}"
|
||||
}
|
||||
}
|
||||
|
||||
vars:pre-request {
|
||||
id: 2
|
||||
task_id: 2
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
}
|
||||
Reference in New Issue
Block a user