Skip to content

Commit

Permalink
Merge pull request #58 from NIAEFEUP/28-create-quest-grant-endpoint-w…
Browse files Browse the repository at this point in the history
…ith-tokens

28 create quest grant endpoint with tokens
  • Loading branch information
limwa authored Nov 11, 2024
2 parents 91a78b6 + 588d9ea commit d581973
Show file tree
Hide file tree
Showing 7 changed files with 508 additions and 3 deletions.
75 changes: 75 additions & 0 deletions app/Http/Controllers/QuestApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Http\Controllers;

use App\Models\Quest;
use App\Models\Participant;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;


class QuestApiController extends Controller
{
/**
* Assign a quest to a participant.
*/
public function give(Request $request, Quest $quest): JsonResponse
{
$validator = Validator::make($request->all(), [
'quest_code' => 'required|exists:participants,quest_code',
]);

if ($validator->fails()) {
return response()->json([
'status' => 'error',
'message' => 'Invalid input',
'errors' => $validator->errors(),
], 400);
}
$editionId = $request->input('edition')->id;
if ($editionId === null) {
return response()->json([
'status' => 'error',
'message' => 'Edition not found!',
], 404);
}
$participant = Participant::firstWhere('quest_code', $request->get('quest_code'));

if ($participant=== null) {
return response()->json([
'status' => 'error',
'message' => 'Participant not found!',
], 404);
};

$enrollment = $participant->enrollments()->where('edition_id', $editionId)->first();
if ($enrollment === null) {
return response()->json([
'status' => 'error',
'message' => 'Participant not enrolled in this edition!',
], 412);
}

try {
$enrollment->quests()->attach($quest);
} catch (\Exception $e) {
Log::error("Failed to attach quest (ID: {$quest->id}) to enrollment (ID: {$enrollment->id})", [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return response()->json([
'status' => 'error',
'message' => 'Failed to assign quest',
], 500);
}


return response()->json([
'status' => 'success',
'message' => 'Quest assigned successfully!',
], 200);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/QuestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public function give(Request $request, Quest $quest)
$request->validate([
'quest_code' => 'required|exists:participants,quest_code',
]);

$edition = $request->input('edition');
$participant = Participant::firstWhere('quest_code', $request->get('quest_code'));
$enrollment = $participant->enrollments()->where('edition_id', $edition?->id)->first();
Expand Down Expand Up @@ -50,4 +49,5 @@ public function give(Request $request, Quest $quest)

return redirect()->back()->banner('Tarefa atribuída com sucesso!');
}

}
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Kernel extends HttpKernel
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\AuthTokenMiddleware::class,
],
];

Expand All @@ -68,5 +69,6 @@ class Kernel extends HttpKernel
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'auth.token' => \App\Http\Middleware\AuthTokenMiddleware::class,
];
}
29 changes: 29 additions & 0 deletions app/Http/Middleware/AuthTokenMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class AuthTokenMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$token = $request->bearerToken();
$authToken = env('AUTH_TOKEN_CTF');

if ($token !== $authToken) {
return response()->json(['message' => 'Unauthorized'], 401);
}


return $next($request);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"license": "MIT",
"require": {
"php": "^8.1",
"doctrine/dbal": "^3.5.1",
"fedeisas/laravel-mail-css-inliner": "^5.2",
"guzzlehttp/guzzle": "^7.2",
"http-interop/http-factory-guzzle": "^1.2",
Expand Down
Loading

0 comments on commit d581973

Please sign in to comment.