Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d03a77b

Browse files
authored
New subfleet not being attached to an airline on import #479 (#505)
* Fix subfleet not being attached to an airline on creation in import #479 * Call airline name with optional() around subfleet * Minor cleanup
1 parent 6fcbd60 commit d03a77b

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

app/Services/ImportExport/AircraftImporter.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Contracts\ImportExport;
66
use App\Models\Aircraft;
7+
use App\Models\Airline;
78
use App\Models\Enums\AircraftState;
89
use App\Models\Enums\AircraftStatus;
910
use App\Models\Subfleet;
@@ -32,19 +33,21 @@ class AircraftImporter extends ImportExport
3233
];
3334

3435
/**
35-
* Find the subfleet specified, or just create it on the fly
36+
* Find the subfleet specified, or just create it on the fly and attach it to the
37+
* first airline that's been found
3638
*
3739
* @param $type
3840
*
3941
* @return Subfleet|\Illuminate\Database\Eloquent\Model|null|object|static
4042
*/
4143
protected function getSubfleet($type)
4244
{
43-
$subfleet = Subfleet::firstOrCreate([
45+
return Subfleet::firstOrCreate([
4446
'type' => $type,
45-
], ['name' => $type]);
46-
47-
return $subfleet;
47+
], [
48+
'name' => $type,
49+
'airline_id' => Airline::where('active', true)->first()->id,
50+
]);
4851
}
4952

5053
/**

resources/views/admin/subfleets/table.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
{{ $subfleet->name }}
1616
</a>
1717
</td>
18-
<td>{{ $subfleet->airline->name }}</td>
18+
<td>{{ optional($subfleet->airline)->name }}</td>
1919
<td>{{ $subfleet->type }}</td>
2020
<td>{{ $subfleet->aircraft->count() }}</td>
2121
<td class="text-right">

tests/ImporterTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ public function testFlightImporterEmptyCustomFields(): void
564564
*/
565565
public function testAircraftImporter(): void
566566
{
567-
$subfleet = factory(App\Models\Subfleet::class)->create(['type' => 'A32X']);
567+
factory(App\Models\Airline::class)->create();
568+
// $subfleet = factory(App\Models\Subfleet::class)->create(['type' => 'A32X']);
568569

569570
$file_path = base_path('tests/data/aircraft.csv');
570571
$status = $this->importSvc->importAircraft($file_path);
@@ -579,8 +580,9 @@ public function testAircraftImporter(): void
579580

580581
$this->assertNotNull($aircraft);
581582
$this->assertNotNull($aircraft->hex_code);
582-
$this->assertEquals($subfleet->id, $aircraft->id);
583-
$this->assertEquals($subfleet->type, $aircraft->subfleet->type);
583+
$this->assertNotNull($aircraft->subfleet);
584+
$this->assertNotNull($aircraft->subfleet->airline);
585+
$this->assertEquals('A32X', $aircraft->subfleet->type);
584586
$this->assertEquals('A320-211', $aircraft->name);
585587
$this->assertEquals('N309US', $aircraft->registration);
586588
$this->assertEquals(null, $aircraft->zfw);

0 commit comments

Comments
 (0)