Added API

This commit is contained in:
2026-06-21 12:52:30 +10:00
parent 05ca994253
commit 5850c849d0
32 changed files with 248 additions and 107 deletions
+24 -3
View File
@@ -3,22 +3,27 @@
namespace App\Http\Controllers\Api;
use App\Http\Controllers\ApiController;
use App\Http\Controllers\UserFlightController;
use App\Models\User;
use App\Models\UserFlight;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
class UserApiController extends ApiController
{
public function nextFlight(string $username): JsonResponse
public function nextFlight(User $user): JsonResponse
{
$user = User::where('name', 'ilike', $username)->first();
if (!$user) {
if (!$user->id) {
return response()->json(['message' => 'User not found'], 404);
}
if (Gate::denies('viewProfileData', $user)) {
return response()->json(['message' => 'Cannot access private user.'], 403);
}
$flight = UserFlight::with(['departureAirport', 'arrivalAirport', 'airline', 'aircraft'])
->where('user_id', $user->id)
->where('departure_date', '>', now()->utc())
@@ -50,4 +55,20 @@ class UserApiController extends ApiController
]);
}
public function viewableFlights(User $user)
{
if (Gate::denies('viewProfileData', $user)) {
return collect([]);
}
return $user->flightsWithRelationshipsLoaded();
}
public function viewableDepartedFlights(User $user)
{
if (Gate::denies('viewProfileData', $user)) {
return collect([]);
}
return $user->flightsWithRelationshipsLoaded('departed');
}
}
@@ -11,48 +11,5 @@ use Illuminate\Support\Facades\Gate;
class UserFlightController extends Controller
{
public function viewableFlights(User $user, ?Request $request = null)
{
if (Gate::denies('viewProfileData', $user)) {
return response()->json([]);
}
return $this->flights($user, $request);
}
public function flights(User $user, ?Request $request = null)
{
$key = "user_flights_{$user->id}";
$json = Cache::remember($key, now()->addDays(30), function () use ($user) {
return UserFlight::where('user_id', $user->id)
->with([
'departureAirport.region.country',
'departureAirport.region.continent',
'arrivalAirport.region.country',
'arrivalAirport.region.continent',
'airline.country',
'airline.alliance',
'aircraft',
'seatType',
'flightReason',
'flightClass',
'crewType'
])
->orderBy('departure_date', 'desc')
->get()
->values()
->toJson();
});
if ($request?->boolean('departed_only')) {
$filtered = collect(json_decode($json))
->filter(fn($f) => $f->departure_date <= now('UTC')->toDateString())
->values()
->toJson();
return response($filtered, 200)->header('Content-Type', 'application/json');
}
return response($json, 200)->header('Content-Type', 'application/json');
}
}
@@ -36,7 +36,8 @@ class UserProfileController extends Controller
}
public static function getUserFlightApiURL(User $user){
return '/data/user/'.$user->name.'/flights';
return config('app.logo_api_url').'/user/'.$user->name.'/flights';
//return '/data/user/'.$user->name.'/flights';
}
public function profileData(User $user, string $view, ?int $selectedFlightId = null) : array {