diff --git a/app/Http/Controllers/Admin/FlightController.php b/app/Http/Controllers/Admin/FlightController.php index ba3682b1..b50ea203 100644 --- a/app/Http/Controllers/Admin/FlightController.php +++ b/app/Http/Controllers/Admin/FlightController.php @@ -18,6 +18,7 @@ use App\Repositories\FlightRepository; use App\Repositories\SubfleetRepository; use App\Services\ExportService; use App\Services\FareService; +use App\Services\FleetService; use App\Services\FlightService; use App\Services\ImportService; use App\Support\Units\Time; @@ -451,12 +452,15 @@ class FlightController extends Controller return redirect(route('admin.flights.index')); } + $fleetSvc = app(FleetService::class); + // add aircraft to flight + $subfleet = $this->subfleetRepo->find($request->input('subfleet_id')); if ($request->isMethod('post')) { - $flight->subfleets()->syncWithoutDetaching([$request->subfleet_id]); + $fleetSvc->addSubfleetToFlight($subfleet, $flight); } // remove aircraft from flight elseif ($request->isMethod('delete')) { - $flight->subfleets()->detach($request->subfleet_id); + $fleetSvc->removeSubfleetFromFlight($subfleet, $flight); } return $this->return_subfleet_view($flight); diff --git a/app/Services/FleetService.php b/app/Services/FleetService.php index 1106b7c2..f56e6021 100644 --- a/app/Services/FleetService.php +++ b/app/Services/FleetService.php @@ -3,6 +3,7 @@ namespace App\Services; use App\Interfaces\Service; +use App\Models\Flight; use App\Models\Rank; use App\Models\Subfleet; @@ -41,4 +42,26 @@ class FleetService extends Service return $subfleet; } + + /** + * Add the subfleet to a flight + * @param Subfleet $subfleet + * @param Flight $flight + */ + public function addSubfleetToFlight(Subfleet $subfleet, Flight $flight) + { + $flight->subfleets()->syncWithoutDetaching([$subfleet->id]); + $subfleet->save(); + $subfleet->refresh(); + } + + /** + * Remove the subfleet from a flight + * @param Subfleet $subfleet + * @param Flight $flight + */ + public function removeSubfleetFromFlight(Subfleet $subfleet, Flight $flight) + { + $flight->subfleets()->detach($subfleet->id); + } } diff --git a/resources/views/admin/app.blade.php b/resources/views/admin/app.blade.php index ccd80523..dcdebc33 100644 --- a/resources/views/admin/app.blade.php +++ b/resources/views/admin/app.blade.php @@ -100,6 +100,8 @@ + + diff --git a/resources/views/admin/flights/scripts.blade.php b/resources/views/admin/flights/scripts.blade.php index 707f70e8..c9ea7540 100644 --- a/resources/views/admin/flights/scripts.blade.php +++ b/resources/views/admin/flights/scripts.blade.php @@ -63,6 +63,14 @@ $(document).ready(function () { setEditable(); setFieldsEditable(); + /*const pjax = new Pjax({ + elements: 'form[action]', + selectors: ['.pjax_subfleet_form'], + switches: { + '#subfleet_flight_wrapper': Pjax.switches.replaceNode + } + });*/ + $(document).on('submit', 'form.pjax_flight_fields', function (event) { event.preventDefault(); $.pjax.submit(event, '#flight_fields_wrapper', {push: false}); diff --git a/tests/FlightTest.php b/tests/FlightTest.php index ae00ae46..83fc6666 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -170,6 +170,28 @@ class FlightTest extends TestCase $this->assertEquals($flight->id, $body['data'][0]['id']); } + /** + * + */ + public function testAddSubfleet() + { + $subfleet = factory(App\Models\Subfleet::class)->create(); + $flight = factory(App\Models\Flight::class)->create(); + + $fleetSvc = app(App\Services\FleetService::class); + $fleetSvc->addSubfleetToFlight($subfleet, $flight); + + $flight->refresh(); + $found = $flight->subfleets()->get(); + $this->assertCount(1, $found); + + # Make sure it hasn't been added twice + $fleetSvc->addSubfleetToFlight($subfleet, $flight); + $flight->refresh(); + $found = $flight->subfleets()->get(); + $this->assertCount(1, $found); + } + /** * Add/remove a bid, test the API, etc * @throws \App\Services\Exception