Added User API
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Airline;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class LogoController extends ApiController
|
||||
{
|
||||
const array CONDOR_LOGOS = ['BEACH', 'ISLAND', 'PASSION', 'SEA', 'SUNSHINE'];
|
||||
|
||||
public function getAirlineLogo(?Airline $airline)
|
||||
{
|
||||
$logoFile = $airline?->logo ?? 'blank.png';
|
||||
$cacheLimit = 60 * 60 * 24;
|
||||
|
||||
if ($airline?->internal_name == 'condor') {
|
||||
$logoKey = array_rand(self::CONDOR_LOGOS);
|
||||
$logoFile = 'DE_' . self::CONDOR_LOGOS[$logoKey] . '.png';
|
||||
$cacheLimit = 1;
|
||||
}
|
||||
|
||||
$path = 'images/logos/tail/' . $logoFile;
|
||||
if (!Storage::disk('local')->exists($path)) {
|
||||
$path = 'images/logos/tail/blank.png';
|
||||
}
|
||||
|
||||
$fullPath = Storage::disk('local')->path($path);
|
||||
$lastModified = filemtime($fullPath);
|
||||
|
||||
return response()->file($fullPath, [
|
||||
'Content-Type' => 'image/png',
|
||||
'Cache-Control' => 'public, max-age='.$cacheLimit, // 24 hours
|
||||
'Last-Modified' => gmdate('D, d M Y H:i:s', $lastModified) . ' GMT',
|
||||
'ETag' => md5($path . $lastModified),
|
||||
]);
|
||||
}
|
||||
|
||||
public function getLogoById($id){
|
||||
$airline = Airline::where('id', $id)
|
||||
->first();
|
||||
|
||||
return $this->getAirlineLogo($airline);
|
||||
}
|
||||
|
||||
public function getLogoByInternalName(string $internalName){
|
||||
$airline = Airline::where('internal_name', $internalName)
|
||||
->first();
|
||||
|
||||
return $this->getAirlineLogo($airline);
|
||||
}
|
||||
|
||||
|
||||
public function getLogoByCode(string $code){
|
||||
|
||||
$column = strlen($code) == 2
|
||||
? 'IATA_code'
|
||||
: 'ICAO_code';
|
||||
|
||||
$airline = Airline::where($column, strtoupper($code))
|
||||
->whereNotNull('logo')
|
||||
->where('active', true)
|
||||
->latest('id')
|
||||
->first();
|
||||
|
||||
if (!$airline) {
|
||||
$airline = Airline::where($column, strtoupper($code))
|
||||
->whereNotNull('logo')
|
||||
->latest('id')
|
||||
->first();
|
||||
}
|
||||
|
||||
return $this->getAirlineLogo($airline);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use App\Models\UserFlight;
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class UserController extends ApiController
|
||||
{
|
||||
function nextFlight(string $username): ?array
|
||||
{
|
||||
$user = User::where('name' ,'ilike', $username)->first();
|
||||
if(!$user) return [
|
||||
'message' => 'User not found',
|
||||
];
|
||||
|
||||
$flight = UserFlight::with(['departureAirport', 'arrivalAirport', 'airline', 'aircraft'])
|
||||
->where('user_id', $user->id)
|
||||
->where('departure_date', '>', now()->utc())
|
||||
->orderBy('departure_date', 'asc')
|
||||
->first();
|
||||
|
||||
$departure = Carbon::parse($flight->departure_date)->setTimezone($flight->departureAirport->timezone);
|
||||
$arrival = Carbon::parse($flight->arrival_date)->setTimezone($flight->arrivalAirport->timezone);
|
||||
|
||||
return [
|
||||
'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' => config('app.logo_api_url') . "/airlines/logos/tail/name/{$flight->airline->internal_name}"
|
||||
];
|
||||
}
|
||||
|
||||
function flights(string $username){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ApiController extends Controller
|
||||
{
|
||||
|
||||
}
|
||||
@@ -8,67 +8,6 @@ use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class LogoController extends Controller
|
||||
{
|
||||
const array CONDOR_LOGOS = ['BEACH', 'ISLAND', 'PASSION', 'SEA', 'SUNSHINE'];
|
||||
|
||||
public function getAirlineLogo(?Airline $airline)
|
||||
{
|
||||
$logoFile = $airline?->logo ?? 'blank.png';
|
||||
$cacheLimit = 60 * 60 * 24;
|
||||
|
||||
if ($airline?->internal_name == 'condor') {
|
||||
$logoKey = array_rand(self::CONDOR_LOGOS);
|
||||
$logoFile = 'DE_' . self::CONDOR_LOGOS[$logoKey] . '.png';
|
||||
$cacheLimit = 1;
|
||||
}
|
||||
|
||||
$path = 'images/logos/tail/' . $logoFile;
|
||||
if (!Storage::disk('local')->exists($path)) {
|
||||
$path = 'images/logos/tail/blank.png';
|
||||
}
|
||||
|
||||
$fullPath = Storage::disk('local')->path($path);
|
||||
$lastModified = filemtime($fullPath);
|
||||
|
||||
return response()->file($fullPath, [
|
||||
'Content-Type' => 'image/png',
|
||||
'Cache-Control' => 'public, max-age='.$cacheLimit, // 24 hours
|
||||
'Last-Modified' => gmdate('D, d M Y H:i:s', $lastModified) . ' GMT',
|
||||
'ETag' => md5($path . $lastModified),
|
||||
]);
|
||||
}
|
||||
|
||||
public function getLogoById($id){
|
||||
$airline = Airline::where('id', $id)
|
||||
->first();
|
||||
|
||||
//Blank Airline Logo
|
||||
if(!$airline) return $this->getLogoByCode('2H');
|
||||
|
||||
return $this->getAirlineLogo($airline);
|
||||
}
|
||||
|
||||
public function getLogoByCode(string $code){
|
||||
|
||||
$column = strlen($code) == 2
|
||||
? 'IATA_code'
|
||||
: 'ICAO_code';
|
||||
|
||||
$airline = Airline::where($column, strtoupper($code))
|
||||
->whereNotNull('logo')
|
||||
->where('active', true)
|
||||
->latest('id')
|
||||
->first();
|
||||
|
||||
if (!$airline) {
|
||||
$airline = Airline::where($column, strtoupper($code))
|
||||
->whereNotNull('logo')
|
||||
->latest('id')
|
||||
->first();
|
||||
}
|
||||
|
||||
return $this->getAirlineLogo($airline);
|
||||
}
|
||||
|
||||
|
||||
public static function deduplicateLogo(string $logo, array $correctInternalNames)
|
||||
{
|
||||
|
||||
@@ -11,7 +11,7 @@ const props = defineProps<{
|
||||
}>();
|
||||
|
||||
const page = usePage<SharedProps>().props;
|
||||
const logoUrl = computed(() => `url('${page.logo_api_url}/airlines/logos/tail/id/${props.airline?.id}')`);
|
||||
const logoUrl = computed(() => `url('${page.logo_api_url}/airlines/logos/tail/name/${props.airline?.internal_name}')`);
|
||||
const logoStyle = computed(() => ({
|
||||
width: size.value,
|
||||
height: size.value,
|
||||
|
||||
@@ -233,12 +233,13 @@ watch(
|
||||
|
||||
<td class="v-data-table__td ">
|
||||
<span class="class-cell">
|
||||
<CrewTooltip v-if="(item as Flight).flight_reason?.name == 'Crew'" :crew-type="(item as Flight).crew_type!">
|
||||
<FlightClassBadge :flight="(item as Flight)" />
|
||||
</CrewTooltip>
|
||||
<FlightClassBadge v-else :flight="(item as Flight)" />
|
||||
<InlineBadge v-if="(item as Flight).seat_number" variant="economy">{{(item as Flight).seat_number}}</InlineBadge>
|
||||
<InlineBadge v-if="(item as Flight).seat_type?.name && (item as Flight).seat_type?.name !== 'Unassigned'" variant="economy">{{(item as Flight).seat_type?.name}}</InlineBadge>
|
||||
<CrewTooltip v-if="(item as Flight).flight_reason?.name == 'Crew'" :crew-type="(item as Flight).crew_type!">
|
||||
<FlightClassBadge v-if="(item as Flight).flight_class?.internal_name === 'crew'" :flight="(item as Flight)" />
|
||||
<InlineBadge v-else variant="crew">Crew</InlineBadge>
|
||||
</CrewTooltip>
|
||||
<FlightClassBadge v-if="(item as Flight).flight_reason?.name !== 'Crew' || (item as Flight).flight_class?.internal_name !== 'crew'" :flight="(item as Flight)" />
|
||||
<InlineBadge v-if="(item as Flight).seat_number" variant="economy">{{ (item as Flight).seat_number }}</InlineBadge>
|
||||
<InlineBadge v-if="(item as Flight).seat_type?.name && (item as Flight).seat_type?.name !== 'Unassigned'" variant="economy">{{ (item as Flight).seat_type?.name }}</InlineBadge>
|
||||
</span>
|
||||
</td>
|
||||
|
||||
|
||||
@@ -82,8 +82,8 @@ function emitFilters() {
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────────
|
||||
const page = usePage()
|
||||
const airlineLogoUrl = (id: number) =>
|
||||
`${page.props.logo_api_url}/airlines/logos/tail/id/${id}`
|
||||
const airlineLogoUrl = (internal_name: string) =>
|
||||
`${page.props.logo_api_url}/airlines/logos/tail/name/${internal_name}`
|
||||
|
||||
const countryFlagClass = (code: string) =>
|
||||
`fi fi-${code.toLowerCase()}`
|
||||
@@ -123,7 +123,7 @@ const countryFlagClass = (code: string) =>
|
||||
</template>
|
||||
<template #title>
|
||||
<img
|
||||
:src="airlineLogoUrl((item as any).id)"
|
||||
:src="airlineLogoUrl((item as any).internal_name)"
|
||||
width="32" height="32"
|
||||
style="object-fit: contain; margin-right: 8px; vertical-align: middle;"
|
||||
alt=""
|
||||
|
||||
+5
-3
@@ -90,7 +90,9 @@ Route::domain(config('app.api_domain'))->group(function () {
|
||||
Route::get('/', function () {
|
||||
return response()->json(['message' => 'Welcome to the FlightsGoneBy API']);
|
||||
});
|
||||
Route::get('airlines/logos/tail/{code}', [LogoController::class, 'getLogoByCode'])
|
||||
->where('code', '[A-Za-z0-9]{2,3}');
|
||||
Route::get('airlines/logos/tail/id/{id}', [LogoController::class, 'getLogoById'])->where('id', '[0-9]+');
|
||||
|
||||
Route::get('airlines/logos/tail/id/{id}', [App\Http\Controllers\Api\LogoController::class, 'getLogoById'])->where('id', '[0-9]+');
|
||||
Route::get('airlines/logos/tail/name/{internalName}', [App\Http\Controllers\Api\LogoController::class, 'getLogoByInternalName']);
|
||||
Route::get('user/{username}/next-flight', [App\Http\Controllers\Api\UserController::class, 'nextFlight']);
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user