Files
2026-04-23 21:32:25 +10:00

61 lines
2.3 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\ApiController;
use App\Models\User;
use App\Models\UserFlight;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
class UserApiController extends ApiController
{
public function nextFlight(string $username): JsonResponse
{
$user = User::where('name', 'ilike', $username)->first();
if (!$user) {
return response()->json(['message' => 'User not found'], 404);
}
$flight = UserFlight::with(['departureAirport', 'arrivalAirport', 'airline', 'aircraft'])
->where('user_id', $user->id)
->where('departure_date', '>', now()->utc())
->orderBy('departure_date', 'asc')
->first();
if (!$flight) {
return response()->json(['message' => 'No upcoming flights found'], 404);
}
$departure = Carbon::parse($flight->departure_date)->setTimezone($flight->departureAirport->timezone);
$arrival = Carbon::parse($flight->arrival_date)->setTimezone($flight->arrivalAirport->timezone);
return response()->json([
'departureAirportCode' => $flight->departureAirport->iata_code,
'departureCity' => $flight->departureAirport->municipality,
'departureDateReadable' => $departure->format('F j'),
'departureTime' => $departure->format('H:i'),
'arrivalAirportCode' => $flight->arrivalAirport->iata_code,
'arrivalCity' => $flight->arrivalAirport->municipality,
'arrivalDateReadable' => $arrival->format('F j'),
'arrivalTime' => $arrival->format('H:i'),
'flightNumber' => $flight->flight_number,
'airlineName' => $flight->airline->name,
'aircraftType' => $flight->aircraft->manufacturer_code . ' ' . $flight->aircraft->model_full_name,
'logoUrl' => $flight->airline?->logo_url ?? 'undefined',
]);
}
public function flights(string $username): JsonResponse
{
$user = User::where('name', 'ilike', $username)->first();
if (!$user) {
return response()->json(['message' => 'User not found'], 404);
}
return response()->json($user->FlightController()->flights());
}
}