diff --git a/src/app/Event.php b/src/app/Event.php index 4911f044..2169a1c0 100644 --- a/src/app/Event.php +++ b/src/app/Event.php @@ -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; diff --git a/src/app/Exceptions/Handler.php b/src/app/Exceptions/Handler.php index fbc54298..fd3abe58 100644 --- a/src/app/Exceptions/Handler.php +++ b/src/app/Exceptions/Handler.php @@ -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 @@ -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); diff --git a/src/app/Http/Controllers/Api/Events/EventsController.php b/src/app/Http/Controllers/Api/Events/EventsController.php index 57174d51..351bdf17 100644 --- a/src/app/Http/Controllers/Api/Events/EventsController.php +++ b/src/app/Http/Controllers/Api/Events/EventsController.php @@ -121,7 +121,7 @@ public function show($event) } if (!$event) { - abort(404); + abort(404, "Event not found."); } $return = [ diff --git a/src/app/Http/Controllers/Api/Events/ParticipantsController.php b/src/app/Http/Controllers/Api/Events/ParticipantsController.php index cffedaf5..4db7c364 100644 --- a/src/app/Http/Controllers/Api/Events/ParticipantsController.php +++ b/src/app/Http/Controllers/Api/Events/ParticipantsController.php @@ -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; } } diff --git a/src/app/Http/Controllers/Api/Events/TicketsController.php b/src/app/Http/Controllers/Api/Events/TicketsController.php index 4b21643a..b6f5c34d 100644 --- a/src/app/Http/Controllers/Api/Events/TicketsController.php +++ b/src/app/Http/Controllers/Api/Events/TicketsController.php @@ -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, @@ -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, diff --git a/src/app/Http/Controllers/Api/Events/TimetablesController.php b/src/app/Http/Controllers/Api/Events/TimetablesController.php index 31b1f9a5..fe6fa8a8 100644 --- a/src/app/Http/Controllers/Api/Events/TimetablesController.php +++ b/src/app/Http/Controllers/Api/Events/TimetablesController.php @@ -33,7 +33,7 @@ public function index($event) } if (!$event) { - abort(404); + abort(404, "Event not found."); } $event = Event::where('id', $event->id)->first(); @@ -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; } } diff --git a/src/app/Http/Controllers/Userapi/MeController.php b/src/app/Http/Controllers/Userapi/MeController.php index af743758..da3f4ec1 100644 --- a/src/app/Http/Controllers/Userapi/MeController.php +++ b/src/app/Http/Controllers/Userapi/MeController.php @@ -21,7 +21,7 @@ public function getMe(Request $request) $user = auth('sanctum')->user(); if (!isset($user)) { - abort(404); + abort(404, "User not found."); } diff --git a/src/app/Http/Kernel.php b/src/app/Http/Kernel.php index a360bb3f..fc81db02 100644 --- a/src/app/Http/Kernel.php +++ b/src/app/Http/Kernel.php @@ -37,6 +37,7 @@ class Kernel extends HttpKernel 'api' => [ 'throttle:60000,1', \Illuminate\Routing\Middleware\SubstituteBindings::class, + \App\Http\Middleware\ApiGlobalScopesMiddleware::class, ], 'userapi' => [ diff --git a/src/app/Http/Middleware/ApiGlobalScopesMiddleware.php b/src/app/Http/Middleware/ApiGlobalScopesMiddleware.php new file mode 100644 index 00000000..851e53ce --- /dev/null +++ b/src/app/Http/Middleware/ApiGlobalScopesMiddleware.php @@ -0,0 +1,29 @@ +where('status', 'PUBLISHED'); + }); + + + return $next($request); + } +}