Export airports

This commit is contained in:
Nabeel Shahzad 2018-03-22 13:04:13 -05:00
parent d4f79b1331
commit 4e3a9fd9ea
6 changed files with 77 additions and 12 deletions

View File

@ -154,7 +154,7 @@ class AircraftController extends Controller
} }
/** /**
* Run the flight exporter * Run the aircraft exporter
* @param Request $request * @param Request $request
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
* @throws \League\Csv\Exception * @throws \League\Csv\Exception

View File

@ -9,6 +9,7 @@ use App\Models\Airport;
use App\Models\Expense; use App\Models\Expense;
use App\Repositories\AirportRepository; use App\Repositories\AirportRepository;
use App\Repositories\Criteria\WhereCriteria; use App\Repositories\Criteria\WhereCriteria;
use App\Services\ExportService;
use App\Services\ImportService; use App\Services\ImportService;
use Flash; use Flash;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -178,6 +179,25 @@ class AirportController extends Controller
return redirect(route('admin.airports.index')); return redirect(route('admin.airports.index'));
} }
/**
* Run the airport exporter
* @param Request $request
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
* @throws \League\Csv\Exception
*/
public function export(Request $request)
{
$exporter = app(ExportService::class);
$airports = $this->airportRepo->all();
$path = $exporter->exportAirports($airports);
return response()
->download($path, 'airports.csv', [
'content-type' => 'text/csv',
])
->deleteFileAfterSend(true);
}
/** /**
* *
* @param Request $request * @param Request $request

View File

@ -9,6 +9,7 @@ Route::group([
], function () { ], function () {
Route::resource('airlines', 'AirlinesController'); Route::resource('airlines', 'AirlinesController');
Route::get('airports/export', 'AirportController@export')->name('airports.export');
Route::match(['get', 'post', 'put'], 'airports/fuel', 'AirportController@fuel'); Route::match(['get', 'post', 'put'], 'airports/fuel', 'AirportController@fuel');
Route::match(['get', 'post'], 'airports/import', 'AirportController@import')->name('airports.import'); Route::match(['get', 'post'], 'airports/import', 'AirportController@import')->name('airports.import');
Route::match(['get', 'post', 'put', 'delete'], 'airports/{id}/expenses', 'AirportController@expenses'); Route::match(['get', 'post', 'put', 'delete'], 'airports/{id}/expenses', 'AirportController@expenses');

View File

@ -4,8 +4,8 @@ namespace App\Services;
use App\Interfaces\ImportExport; use App\Interfaces\ImportExport;
use App\Interfaces\Service; use App\Interfaces\Service;
use App\Repositories\FlightRepository;
use App\Services\ImportExport\AircraftExporter; use App\Services\ImportExport\AircraftExporter;
use App\Services\ImportExport\AirportExporter;
use App\Services\ImportExport\FlightExporter; use App\Services\ImportExport\FlightExporter;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use League\Csv\CharsetConverter; use League\Csv\CharsetConverter;
@ -19,16 +19,6 @@ use Log;
*/ */
class ExportService extends Service class ExportService extends Service
{ {
protected $flightRepo;
/**
* ImporterService constructor.
* @param FlightRepository $flightRepo
*/
public function __construct(FlightRepository $flightRepo) {
$this->flightRepo = $flightRepo;
}
/** /**
* @param string $path * @param string $path
* @return Writer * @return Writer
@ -55,6 +45,8 @@ class ExportService extends Service
Storage::makeDirectory('import'); Storage::makeDirectory('import');
$path = storage_path('/app/import/export_'.$filename.'.csv'); $path = storage_path('/app/import/export_'.$filename.'.csv');
Log::info('Exporting "'.$exporter->assetType.'" to ' . $path);
$writer = $this->openCsv($path); $writer = $this->openCsv($path);
// Write out the header first // Write out the header first
@ -80,6 +72,18 @@ class ExportService extends Service
return $this->runExport($aircraft, $exporter); return $this->runExport($aircraft, $exporter);
} }
/**
* Export all of the airports
* @param Collection $airports
* @return mixed
* @throws \League\Csv\CannotInsertRecord
*/
public function exportAirports($airports)
{
$exporter = new AirportExporter();
return $this->runExport($airports, $exporter);
}
/** /**
* Export all of the flights * Export all of the flights
* @param Collection $flights * @param Collection $flights

View File

@ -0,0 +1,39 @@
<?php
namespace App\Services\ImportExport;
use App\Interfaces\ImportExport;
use App\Models\Airport;
/**
* The flight importer can be imported or export. Operates on rows
*
* @package App\Services\Import
*/
class AirportExporter extends ImportExport
{
public $assetType = 'airport';
/**
* Set the current columns and other setup
*/
public function __construct()
{
self::$columns = AirportImporter::$columns;
}
/**
* Import a flight, parse out the different rows
* @param Airport $airport
* @return array
*/
public function export(Airport $airport): array
{
$ret = [];
foreach(self::$columns as $column) {
$ret[$column] = $airport->{$column};
}
return $ret;
}
}

View File

@ -2,6 +2,7 @@
@section('title', 'Airports') @section('title', 'Airports')
@section('actions') @section('actions')
<li><a href="{{ route('admin.airports.export') }}"><i class="ti-plus"></i>Export to CSV</a></li>
<li><a href="{{ route('admin.airports.import') }}"><i class="ti-plus"></i>Import from CSV</a></li> <li><a href="{{ route('admin.airports.import') }}"><i class="ti-plus"></i>Import from CSV</a></li>
<li><a href="{{ route('admin.airports.create') }}"><i class="ti-plus"></i>Add New</a></li> <li><a href="{{ route('admin.airports.create') }}"><i class="ti-plus"></i>Add New</a></li>
@endsection @endsection