Move subfleet add/remove to fleet service for a flight

This commit is contained in:
Nabeel Shahzad 2018-03-23 14:10:18 -05:00
parent 1161106d9c
commit f8f5a71564
5 changed files with 61 additions and 2 deletions

View File

@ -18,6 +18,7 @@ use App\Repositories\FlightRepository;
use App\Repositories\SubfleetRepository; use App\Repositories\SubfleetRepository;
use App\Services\ExportService; use App\Services\ExportService;
use App\Services\FareService; use App\Services\FareService;
use App\Services\FleetService;
use App\Services\FlightService; use App\Services\FlightService;
use App\Services\ImportService; use App\Services\ImportService;
use App\Support\Units\Time; use App\Support\Units\Time;
@ -451,12 +452,15 @@ class FlightController extends Controller
return redirect(route('admin.flights.index')); return redirect(route('admin.flights.index'));
} }
$fleetSvc = app(FleetService::class);
// add aircraft to flight // add aircraft to flight
$subfleet = $this->subfleetRepo->find($request->input('subfleet_id'));
if ($request->isMethod('post')) { if ($request->isMethod('post')) {
$flight->subfleets()->syncWithoutDetaching([$request->subfleet_id]); $fleetSvc->addSubfleetToFlight($subfleet, $flight);
} // remove aircraft from flight } // remove aircraft from flight
elseif ($request->isMethod('delete')) { elseif ($request->isMethod('delete')) {
$flight->subfleets()->detach($request->subfleet_id); $fleetSvc->removeSubfleetFromFlight($subfleet, $flight);
} }
return $this->return_subfleet_view($flight); return $this->return_subfleet_view($flight);

View File

@ -3,6 +3,7 @@
namespace App\Services; namespace App\Services;
use App\Interfaces\Service; use App\Interfaces\Service;
use App\Models\Flight;
use App\Models\Rank; use App\Models\Rank;
use App\Models\Subfleet; use App\Models\Subfleet;
@ -41,4 +42,26 @@ class FleetService extends Service
return $subfleet; 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);
}
} }

View File

@ -100,6 +100,8 @@
</body> </body>
<script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script> <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
<script src="https://cdn.jsdelivr.net/npm/pjax@0.2.5/pjax.js"></script>
<script src="{{ public_asset('/assets/admin/js/vendor.js') }}"></script> <script src="{{ public_asset('/assets/admin/js/vendor.js') }}"></script>
<script src="{{ public_asset('/assets/system/js/phpvms.js') }}"></script> <script src="{{ public_asset('/assets/system/js/phpvms.js') }}"></script>
<script src="{{ public_asset('/assets/admin/js/admin.js') }}"></script> <script src="{{ public_asset('/assets/admin/js/admin.js') }}"></script>

View File

@ -63,6 +63,14 @@ $(document).ready(function () {
setEditable(); setEditable();
setFieldsEditable(); 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) { $(document).on('submit', 'form.pjax_flight_fields', function (event) {
event.preventDefault(); event.preventDefault();
$.pjax.submit(event, '#flight_fields_wrapper', {push: false}); $.pjax.submit(event, '#flight_fields_wrapper', {push: false});

View File

@ -170,6 +170,28 @@ class FlightTest extends TestCase
$this->assertEquals($flight->id, $body['data'][0]['id']); $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 * Add/remove a bid, test the API, etc
* @throws \App\Services\Exception * @throws \App\Services\Exception