Skip to content

Commit

Permalink
Merge pull request #892 from Lan2Play/feature/fixEventApi
Browse files Browse the repository at this point in the history
Fix (event) api
  • Loading branch information
Apfelwurm authored Oct 28, 2024
2 parents f6c1c80 + 72d6989 commit 820c1d5
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 37 deletions.
2 changes: 2 additions & 0 deletions src/app/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Event extends Model

protected static function boot()
{
// Remember There are is also an ApiGlobalScopesMiddleware used here

parent::boot();

$admin = false;
Expand Down
14 changes: 11 additions & 3 deletions src/app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Throwable;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
Expand Down Expand Up @@ -49,10 +50,17 @@ public function register()
public function render($request, Throwable $exception)
{
if (
strpos($request->getRequestUri(), '/api/', 0) === 0 &&
$exception instanceof NotFoundHttpException
$request->is('api/*')
) {
return response()->json(['error' => $exception->getMessage()], 404);
if ($exception instanceof HttpExceptionInterface) {
$statuscode = $exception->getStatusCode();
}
else
{
$statuscode = 500;
}
return response()->json(['error' => $exception->getMessage()], $statuscode);

}

return parent::render($request, $exception);
Expand Down
2 changes: 1 addition & 1 deletion src/app/Http/Controllers/Api/Events/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function show($event)
}

if (!$event) {
abort(404);
abort(404, "Event not found.");
}

$return = [
Expand Down
38 changes: 17 additions & 21 deletions src/app/Http/Controllers/Api/Events/ParticipantsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,30 @@ public function index($event)
}

if (!$event) {
abort(404);
abort(404, "Event not found.");
}

if ($event->private_participants)
{
abort(403);
}


$return = array();

foreach ($event->eventParticipants as $participant) {
// $x["id"] = $participant->id;
// $x["user_id"] = $participant->user_id;
// $x["ticket_id"] = $participant->ticket_id;
// $x["gift"] = $participant->gift;
// $x["gift_sendee"] = $participant->gift_sendee;
$seat = "Not Seated";
if ($participant->seat) {
$seat = $participant->seat->seat;
$return = [
'count' => $event->eventParticipants->count(),
'participants' => array(),
];

if (!$event->private_participants) {
foreach ($event->eventParticipants as $participant) {
$seat = "Not Seated";
if ($participant->seat) {
$seat = $participant->seat->seat;
}
$return["participants"][] = [
'username' => $participant->user->steamname ?? $participant->user->username,
'seat' => $seat,
];
}
$return[] = [
'username' => $participant->user->steamname ?? $participant->user->username,
'seat' => $seat,
];
// $x['user']['steamname'] = $participant->user->steamname;
// $x['user']['username'] = $participant->user->username;
}

return $return;
}
}
10 changes: 5 additions & 5 deletions src/app/Http/Controllers/Api/Events/TicketsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ public function index($event)
$event = Event::where('slug', $event)->first();
}
if (!$event) {
abort(404);
abort(404, "Event not found.");
}

$return = array();
foreach ($event->tickets as $ticket) {
$return[] = [
'id' => $ticket->id,
'name' => $ticket->name,
'type' => $ticket->type,
'price' => $ticket->price,
Expand All @@ -64,15 +65,14 @@ public function show($event, $ticket)
$event = Event::where('slug', $event)->first();
}
if (is_numeric($ticket)) {
$ticket = EventTicket::where('id', $ticket)->first();
} else {
$ticket = EventTicket::where('slug', $ticket)->first();
$ticket = $event->tickets()->where('id', $ticket)->first();
}
if (!$event || !$ticket) {
abort(404);
abort(404, "Event not found.");
}

$return = [
'id' => $ticket->id,
'name' => $ticket->name,
'type' => $ticket->type,
'price' => $ticket->price,
Expand Down
11 changes: 5 additions & 6 deletions src/app/Http/Controllers/Api/Events/TimetablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function index($event)
}

if (!$event) {
abort(404);
abort(404, "Event not found.");
}

$event = Event::where('id', $event->id)->first();
Expand All @@ -54,17 +54,16 @@ public function show($event, $timetable)
$event = Event::where('slug', $event)->first();
}
if (is_numeric($timetable)) {
$timetable = Event::where('id', $timetable)->first();
$timetable = $event->timetables()->with('data')->where('id', $timetable)->first();

} else {
$timetable = Event::where('slug', $timetable)->first();
$timetable = $event->timetables()->with('data')->where('slug', $timetable)->first();
}

if (!$event || !$timetable) {
abort(404);
abort(404, "Event not found.");
}

$event = Event::where('id', $event)->first();
$timetable = EventTimetable::where('id', $timetable)->first();
return $timetable;
}
}
2 changes: 1 addition & 1 deletion src/app/Http/Controllers/Userapi/MeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getMe(Request $request)
$user = auth('sanctum')->user();

if (!isset($user)) {
abort(404);
abort(404, "User not found.");
}


Expand Down
1 change: 1 addition & 0 deletions src/app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Kernel extends HttpKernel
'api' => [
'throttle:60000,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\ApiGlobalScopesMiddleware::class,
],

'userapi' => [
Expand Down
29 changes: 29 additions & 0 deletions src/app/Http/Middleware/ApiGlobalScopesMiddleware.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;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Database\Eloquent\Builder;
use App\Event;


class ApiGlobalScopesMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{

Event::addGlobalScope('statusNotPrivate', function (Builder $builder) {
$builder->where('status', 'PUBLISHED');
});


return $next($request);
}
}

0 comments on commit 820c1d5

Please sign in to comment.