From be6c164f0350111b2b14b2c1b08fff8d8bcc3b9c Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Tue, 11 Feb 2020 10:51:18 -0500 Subject: [PATCH] Add checkbox to clear previous data when importing #490 (#546) * Add checkbox to clear previous data when importing #490 --- .../Controllers/Admin/AircraftController.php | 23 ++------ .../Controllers/Admin/AirportController.php | 18 ++---- .../Controllers/Admin/ExpenseController.php | 18 ++---- app/Http/Controllers/Admin/FareController.php | 18 ++---- .../Controllers/Admin/FlightController.php | 18 ++---- .../Controllers/Admin/SubfleetController.php | 19 ++---- .../Controllers/Admin/Traits/Importable.php | 59 +++++++++++++++++++ app/Models/Enums/ImportExportType.php | 26 ++++++++ resources/views/admin/common/import.blade.php | 7 +++ 9 files changed, 122 insertions(+), 84 deletions(-) create mode 100644 app/Http/Controllers/Admin/Traits/Importable.php create mode 100644 app/Models/Enums/ImportExportType.php diff --git a/app/Http/Controllers/Admin/AircraftController.php b/app/Http/Controllers/Admin/AircraftController.php index db7660ae..cef73151 100644 --- a/app/Http/Controllers/Admin/AircraftController.php +++ b/app/Http/Controllers/Admin/AircraftController.php @@ -3,11 +3,12 @@ namespace App\Http\Controllers\Admin; use App\Contracts\Controller; +use App\Http\Controllers\Admin\Traits\Importable; use App\Http\Requests\CreateAircraftRequest; -use App\Http\Requests\ImportRequest; use App\Http\Requests\UpdateAircraftRequest; use App\Models\Aircraft; use App\Models\Enums\AircraftStatus; +use App\Models\Enums\ImportExportType; use App\Models\Expense; use App\Models\Subfleet; use App\Repositories\AircraftRepository; @@ -15,15 +16,12 @@ use App\Repositories\AirportRepository; use App\Services\ExportService; use App\Services\ImportService; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Storage; use Laracasts\Flash\Flash; -/** - * Class AircraftController - */ class AircraftController extends Controller { + use Importable; + private $aircraftRepo; private $airportRepo; private $importSvc; @@ -218,8 +216,6 @@ class AircraftController extends Controller /** * @param Request $request * - * @throws \Illuminate\Validation\ValidationException - * * @return mixed */ public function import(Request $request) @@ -230,16 +226,7 @@ class AircraftController extends Controller ]; if ($request->isMethod('post')) { - ImportRequest::validate($request); - $path = Storage::putFileAs( - 'import', - $request->file('csv_file'), - 'import_aircraft.csv' - ); - - $path = storage_path('app/'.$path); - Log::info('Uploaded aircraft import file to '.$path); - $logs = $this->importSvc->importAircraft($path); + $logs = $this->importFile($request, ImportExportType::AIRCRAFT); } return view('admin.aircraft.import', [ diff --git a/app/Http/Controllers/Admin/AirportController.php b/app/Http/Controllers/Admin/AirportController.php index 4d5c0d21..ca611667 100644 --- a/app/Http/Controllers/Admin/AirportController.php +++ b/app/Http/Controllers/Admin/AirportController.php @@ -3,10 +3,11 @@ namespace App\Http\Controllers\Admin; use App\Contracts\Controller; +use App\Http\Controllers\Admin\Traits\Importable; use App\Http\Requests\CreateAirportRequest; -use App\Http\Requests\ImportRequest; use App\Http\Requests\UpdateAirportRequest; use App\Models\Airport; +use App\Models\Enums\ImportExportType; use App\Models\Expense; use App\Repositories\AirportRepository; use App\Repositories\Criteria\WhereCriteria; @@ -14,12 +15,12 @@ use App\Services\ExportService; use App\Services\ImportService; use App\Support\Timezonelist; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Storage; use Laracasts\Flash\Flash; class AirportController extends Controller { + use Importable; + private $airportRepo; private $importSvc; @@ -223,16 +224,7 @@ class AirportController extends Controller ]; if ($request->isMethod('post')) { - ImportRequest::validate($request); - $path = Storage::putFileAs( - 'import', - $request->file('csv_file'), - 'import_airports.csv' - ); - - $path = storage_path('app/'.$path); - Log::info('Uploaded airport import file to '.$path); - $logs = $this->importSvc->importAirports($path); + $logs = $this->importFile($request, ImportExportType::AIRPORT); } return view('admin.airports.import', [ diff --git a/app/Http/Controllers/Admin/ExpenseController.php b/app/Http/Controllers/Admin/ExpenseController.php index 184c5ca9..79fc218f 100644 --- a/app/Http/Controllers/Admin/ExpenseController.php +++ b/app/Http/Controllers/Admin/ExpenseController.php @@ -3,21 +3,22 @@ namespace App\Http\Controllers\Admin; use App\Contracts\Controller; -use App\Http\Requests\ImportRequest; +use App\Http\Controllers\Admin\Traits\Importable; use App\Models\Enums\ExpenseType; +use App\Models\Enums\ImportExportType; use App\Models\Expense; use App\Repositories\AirlineRepository; use App\Repositories\ExpenseRepository; use App\Services\ExportService; use App\Services\ImportService; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Storage; use Laracasts\Flash\Flash; use Prettus\Repository\Criteria\RequestCriteria; class ExpenseController extends Controller { + use Importable; + private $airlineRepo; private $expenseRepo; private $importSvc; @@ -223,16 +224,7 @@ class ExpenseController extends Controller ]; if ($request->isMethod('post')) { - ImportRequest::validate($request); - $path = Storage::putFileAs( - 'import', - $request->file('csv_file'), - 'import_expenses.csv' - ); - - $path = storage_path('app/'.$path); - Log::info('Uploaded expenses import file to '.$path); - $logs = $this->importSvc->importExpenses($path); + $logs = $this->importFile($request, ImportExportType::EXPENSES); } return view('admin.expenses.import', [ diff --git a/app/Http/Controllers/Admin/FareController.php b/app/Http/Controllers/Admin/FareController.php index 23216f0c..a12934d0 100644 --- a/app/Http/Controllers/Admin/FareController.php +++ b/app/Http/Controllers/Admin/FareController.php @@ -3,20 +3,21 @@ namespace App\Http\Controllers\Admin; use App\Contracts\Controller; +use App\Http\Controllers\Admin\Traits\Importable; use App\Http\Requests\CreateFareRequest; -use App\Http\Requests\ImportRequest; use App\Http\Requests\UpdateFareRequest; +use App\Models\Enums\ImportExportType; use App\Repositories\FareRepository; use App\Services\ExportService; use App\Services\ImportService; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Storage; use Laracasts\Flash\Flash; use Prettus\Repository\Criteria\RequestCriteria; class FareController extends Controller { + use Importable; + private $fareRepo; private $importSvc; @@ -196,16 +197,7 @@ class FareController extends Controller ]; if ($request->isMethod('post')) { - ImportRequest::validate($request); - $path = Storage::putFileAs( - 'import', - $request->file('csv_file'), - 'import_fares.csv' - ); - - $path = storage_path('app/'.$path); - Log::info('Uploaded fares import file to '.$path); - $logs = $this->importSvc->importFares($path); + $logs = $this->importFile($request, ImportExportType::FARES); } return view('admin.fares.import', [ diff --git a/app/Http/Controllers/Admin/FlightController.php b/app/Http/Controllers/Admin/FlightController.php index 0cef3c46..5b5263fb 100644 --- a/app/Http/Controllers/Admin/FlightController.php +++ b/app/Http/Controllers/Admin/FlightController.php @@ -3,9 +3,11 @@ namespace App\Http\Controllers\Admin; use App\Contracts\Controller; +use App\Http\Controllers\Admin\Traits\Importable; use App\Http\Requests\CreateFlightRequest; use App\Http\Requests\UpdateFlightRequest; use App\Models\Enums\FlightType; +use App\Models\Enums\ImportExportType; use App\Models\Flight; use App\Models\FlightField; use App\Models\FlightFieldValue; @@ -23,14 +25,12 @@ use App\Services\ImportService; use App\Support\Units\Time; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Storage; use Laracasts\Flash\Flash; -/** - * Class FlightController - */ class FlightController extends Controller { + use Importable; + private $airlineRepo; private $airportRepo; private $fareRepo; @@ -325,15 +325,7 @@ class FlightController extends Controller ]; if ($request->isMethod('post')) { - $path = Storage::putFileAs( - 'import', - $request->file('csv_file'), - 'import_flights.csv' - ); - - $path = storage_path('app/'.$path); - Log::info('Uploaded flights import file to '.$path); - $logs = $this->importSvc->importFlights($path); + $logs = $this->importFile($request, ImportExportType::FLIGHTS); } return view('admin.flights.import', [ diff --git a/app/Http/Controllers/Admin/SubfleetController.php b/app/Http/Controllers/Admin/SubfleetController.php index 8253980a..ba85390c 100644 --- a/app/Http/Controllers/Admin/SubfleetController.php +++ b/app/Http/Controllers/Admin/SubfleetController.php @@ -3,11 +3,12 @@ namespace App\Http\Controllers\Admin; use App\Contracts\Controller; +use App\Http\Controllers\Admin\Traits\Importable; use App\Http\Requests\CreateSubfleetRequest; -use App\Http\Requests\ImportRequest; use App\Http\Requests\UpdateSubfleetRequest; use App\Models\Airline; use App\Models\Enums\FuelType; +use App\Models\Enums\ImportExportType; use App\Models\Expense; use App\Models\Subfleet; use App\Repositories\AircraftRepository; @@ -19,13 +20,13 @@ use App\Services\FareService; use App\Services\FleetService; use App\Services\ImportService; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Storage; use Laracasts\Flash\Flash; use Prettus\Repository\Criteria\RequestCriteria; class SubfleetController extends Controller { + use Importable; + private $aircraftRepo; private $fareRepo; private $fareSvc; @@ -293,17 +294,7 @@ class SubfleetController extends Controller ]; if ($request->isMethod('post')) { - ImportRequest::validate($request); - - $path = Storage::putFileAs( - 'import', - $request->file('csv_file'), - 'import_subfleets.csv' - ); - - $path = storage_path('app/'.$path); - Log::info('Uploaded subfleets import file to '.$path); - $logs = $this->importSvc->importSubfleets($path); + $logs = $this->importFile($request, ImportExportType::SUBFLEETS); } return view('admin.subfleets.import', [ diff --git a/app/Http/Controllers/Admin/Traits/Importable.php b/app/Http/Controllers/Admin/Traits/Importable.php new file mode 100644 index 00000000..a4a38c68 --- /dev/null +++ b/app/Http/Controllers/Admin/Traits/Importable.php @@ -0,0 +1,59 @@ +file('csv_file'), + 'import_'.ImportExportType::label($importType).'.csv' + ); + + /** @var ImportService */ + $importSvc = app(ImportService::class); + + $path = storage_path('app/'.$path); + Log::info('Uploaded airport import file to '.$path); + + $delete_previous = get_truth_state($request->get('delete')); + + switch ($importType) { + case ImportExportType::AIRCRAFT: + return $importSvc->importAircraft($path, $delete_previous); + case ImportExportType::AIRPORT: + return $importSvc->importAirports($path, $delete_previous); + case ImportExportType::EXPENSES: + return $importSvc->importExpenses($path, $delete_previous); + case ImportExportType::FARES: + return $importSvc->importFares($path, $delete_previous); + case ImportExportType::FLIGHTS: + return $importSvc->importFlights($path, $delete_previous); + case ImportExportType::SUBFLEETS: + return $importSvc->importSubfleets($path, $delete_previous); + } + + throw new InvalidArgumentException('Unknown import type!'); + } +} diff --git a/app/Models/Enums/ImportExportType.php b/app/Models/Enums/ImportExportType.php new file mode 100644 index 00000000..aa4ee363 --- /dev/null +++ b/app/Models/Enums/ImportExportType.php @@ -0,0 +1,26 @@ + 'airline', + self::AIRCRAFT => 'aircraft', + self::AIRPORT => 'airport', + self::EXPENSES => 'expense', + self::FARES => 'fare', + self::FLIGHTS => 'flight', + self::SUBFLEETS => 'subfleet', + ]; +} diff --git a/resources/views/admin/common/import.blade.php b/resources/views/admin/common/import.blade.php index c58f9dc8..1ad7fa01 100644 --- a/resources/views/admin/common/import.blade.php +++ b/resources/views/admin/common/import.blade.php @@ -7,6 +7,13 @@ {{ Form::label('csv_file', 'Choose a CSV file to import') }} {{ Form::file('csv_file', ['accept' => '.csv']) }}

{{ $errors->first('csv_file') }}

+
+ +