-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from NIAEFEUP/28-create-quest-grant-endpoint-w…
…ith-tokens 28 create quest grant endpoint with tokens
- Loading branch information
Showing
7 changed files
with
508 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.