Add checkbox to clear previous data when importing #490 (#546)

* Add checkbox to clear previous data when importing #490
This commit is contained in:
Nabeel S 2020-02-11 10:51:18 -05:00 committed by GitHub
parent 99118daad9
commit be6c164f03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 122 additions and 84 deletions

View File

@ -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', [

View File

@ -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', [

View File

@ -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', [

View File

@ -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', [

View File

@ -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', [

View File

@ -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', [

View File

@ -0,0 +1,59 @@
<?php
namespace App\Http\Controllers\Admin\Traits;
use App\Http\Requests\ImportRequest;
use App\Models\Enums\ImportExportType;
use App\Services\ImportService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use InvalidArgumentException;
trait Importable
{
/**
* Import a file, passing in the import/export type
*
* @param Request $request Request object
* @param int $importType Refer to \App\Models\Enums\ImportExportType
*
* @throws \Illuminate\Validation\ValidationException
*
* @return mixed
*/
public function importFile(Request $request, int $importType)
{
ImportRequest::validate($request);
$path = Storage::putFileAs(
'import',
$request->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!');
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Models\Enums;
use App\Contracts\Enum;
class ImportExportType extends Enum
{
public const AIRLINE = 1;
public const AIRCRAFT = 2;
public const AIRPORT = 3;
public const EXPENSES = 4;
public const FARES = 5;
public const FLIGHTS = 6;
public const SUBFLEETS = 7;
public static $labels = [
self::AIRLINE => 'airline',
self::AIRCRAFT => 'aircraft',
self::AIRPORT => 'airport',
self::EXPENSES => 'expense',
self::FARES => 'fare',
self::FLIGHTS => 'flight',
self::SUBFLEETS => 'subfleet',
];
}

View File

@ -7,6 +7,13 @@
{{ Form::label('csv_file', 'Choose a CSV file to import') }}
{{ Form::file('csv_file', ['accept' => '.csv']) }}
<p class="text-danger">{{ $errors->first('csv_file') }}</p>
<div class="checkbox">
<label class="checkbox-inline">
{{ Form::label('delete', 'Delete existing data:') }}
{{ Form::hidden('delete', 0, false) }}
{{ Form::checkbox('delete') }}
</label>
</div>
</div>
<div class="form-group col-md-12">