Logo API
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Airline;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class LogoController extends Controller
|
||||
{
|
||||
public function getAirlineLogo(?Airline $airline){
|
||||
$logoFile = $airline?->logo ?? 'blank.png';
|
||||
$path = 'images/logos/tail/' . $logoFile;
|
||||
if (!Storage::disk('local')->exists($path)) {
|
||||
$path = 'images/logos/tail/blank.png';
|
||||
}
|
||||
|
||||
return response()->file(Storage::disk('local')->path($path), [
|
||||
'Content-Type' => 'image/png',
|
||||
]);
|
||||
}
|
||||
|
||||
public function getLogoByIATACode(string $code)
|
||||
{
|
||||
$airline = Airline::where('IATA_code', strtoupper($code))->first();
|
||||
return $this->getAirlineLogo($airline);
|
||||
}
|
||||
|
||||
public function getLogoByICAOCode(string $code)
|
||||
{
|
||||
$airline = Airline::where('ICAO_code', strtoupper($code))->first();
|
||||
return $this->getAirlineLogo($airline);
|
||||
}
|
||||
|
||||
public function getLogoByCode(string $code){
|
||||
|
||||
return strlen($code) == 2
|
||||
? $this->getLogoByIATACode($code)
|
||||
: $this->getLogoByICAOCode($code);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Airline extends Model
|
||||
{
|
||||
protected $table = 'airlines';
|
||||
|
||||
protected $fillable = [
|
||||
'IATA_code',
|
||||
'ICAO_code',
|
||||
'name',
|
||||
'internal_name',
|
||||
'country_code',
|
||||
'country_name',
|
||||
'active',
|
||||
'logo',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'active' => 'boolean',
|
||||
];
|
||||
|
||||
public $timestamps = false;
|
||||
}
|
||||
@@ -47,6 +47,7 @@ return [
|
||||
'report' => false,
|
||||
],
|
||||
|
||||
|
||||
's3' => [
|
||||
'driver' => 's3',
|
||||
'key' => env('AWS_ACCESS_KEY_ID'),
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('airlines', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('IATA_code', 3)->nullable();
|
||||
$table->string('ICAO_code', 4)->nullable();
|
||||
$table->string('name', 81)->nullable();
|
||||
$table->string('internal_name', 81)->nullable();
|
||||
$table->string('country_code', 2)->nullable();
|
||||
$table->string('country_name', 36)->nullable();
|
||||
$table->boolean('active');
|
||||
$table->string('logo', 40)->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('airlines');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AirlinesSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$path = storage_path('app/private/seed_data/airlines.csv');
|
||||
$file = fopen($path, 'r');
|
||||
|
||||
$header = fgetcsv($file, separator: ';');
|
||||
|
||||
$keep = ['codeIataAirline', 'codeIcaoAirline', 'nameAirline', 'codeIso2Country', 'nameCountry', 'slug', 'statusAirline'];
|
||||
|
||||
$renames = [
|
||||
'airlineId' => 'id',
|
||||
'codeIataAirline' => 'IATA_code',
|
||||
'codeIcaoAirline' => 'ICAO_code',
|
||||
'slug' => 'internal_name',
|
||||
'nameAirline' => 'name',
|
||||
'codeIso2Country' => 'country_code',
|
||||
'nameCountry' => 'country_name',
|
||||
'statusAirline' => 'active'
|
||||
];
|
||||
|
||||
while (($row = fgetcsv($file, separator: ';')) !== false) {
|
||||
$data = array_combine($header, $row);
|
||||
$filtered = array_intersect_key($data, array_flip($keep));
|
||||
|
||||
foreach ($renames as $old => $new) {
|
||||
if (array_key_exists($old, $filtered)) {
|
||||
$filtered[$new] = $filtered[$old];
|
||||
unset($filtered[$old]);
|
||||
}
|
||||
}
|
||||
$filtered['active'] = $filtered['active'] === 'active';
|
||||
$filtered['logo'] = $filtered['IATA_code'] ? $filtered['IATA_code'].'.png' : null;
|
||||
DB::table('airlines')->insert($filtered);
|
||||
}
|
||||
|
||||
fclose($file);
|
||||
}
|
||||
}
|
||||
@@ -15,11 +15,6 @@ class DatabaseSeeder extends Seeder
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
// User::factory(10)->create();
|
||||
|
||||
User::factory()->create([
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
]);
|
||||
$this->call(AirlinesSeeder::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\LogoController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::get('/', function () {
|
||||
return response()->json(['message' => 'Welcome to Dredgy\'s Flights API']);
|
||||
});
|
||||
|
||||
Route::get('airlines/logos/{code}', [LogoController::class, 'getLogoByCode'])
|
||||
->where('code', '[A-Za-z0-9]{2,3}');
|
||||
|
||||
Reference in New Issue
Block a user