Updated Map View
This commit is contained in:
@@ -5,34 +5,54 @@ namespace App\Http\Controllers;
|
||||
use App\Models\User;
|
||||
use App\Models\UserFlight;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
class UserFlightController extends Controller
|
||||
{
|
||||
|
||||
protected User $user;
|
||||
|
||||
function __construct(User $user){
|
||||
$this->user = $user;
|
||||
public function viewableFlights(User $user, ?Request $request = null)
|
||||
{
|
||||
if (Gate::denies('viewProfileData', $user)) {
|
||||
return response()->json([]);
|
||||
}
|
||||
return $this->flights($user, $request);
|
||||
}
|
||||
|
||||
public function flights(?Request $request = null)
|
||||
public function flights(User $user, ?Request $request = null)
|
||||
{
|
||||
return UserFlight::where('user_id', $this->user->id)
|
||||
->with([
|
||||
'departureAirport.region.country',
|
||||
'departureAirport.region.continent',
|
||||
'arrivalAirport.region.country',
|
||||
'arrivalAirport.region.continent',
|
||||
'airline.country',
|
||||
'airline.alliance',
|
||||
'aircraft',
|
||||
'seatType',
|
||||
'flightReason',
|
||||
'flightClass',
|
||||
'crewType'
|
||||
])
|
||||
->when($request?->boolean('departed_only'), fn($q) => $q->where('departure_date', '<=', now('UTC')))
|
||||
->orderBy('departure_date', 'desc')
|
||||
->get();
|
||||
$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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user