Skip to content

Commit

Permalink
Add checkbox to clear previous data when importing #490 (#546)
Browse files Browse the repository at this point in the history
* Add checkbox to clear previous data when importing #490
  • Loading branch information
nabeelio authored Feb 11, 2020
1 parent 99118da commit be6c164
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 84 deletions.
23 changes: 5 additions & 18 deletions app/Http/Controllers/Admin/AircraftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@
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;
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;
Expand Down Expand Up @@ -218,8 +216,6 @@ public function export(Request $request)
/**
* @param Request $request
*
* @throws \Illuminate\Validation\ValidationException
*
* @return mixed
*/
public function import(Request $request)
Expand All @@ -230,16 +226,7 @@ public function import(Request $request)
];

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', [
Expand Down
18 changes: 5 additions & 13 deletions app/Http/Controllers/Admin/AirportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
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;
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;

Expand Down Expand Up @@ -223,16 +224,7 @@ public function import(Request $request)
];

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', [
Expand Down
18 changes: 5 additions & 13 deletions app/Http/Controllers/Admin/ExpenseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -223,16 +224,7 @@ public function import(Request $request)
];

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', [
Expand Down
18 changes: 5 additions & 13 deletions app/Http/Controllers/Admin/FareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -196,16 +197,7 @@ public function import(Request $request)
];

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', [
Expand Down
18 changes: 5 additions & 13 deletions app/Http/Controllers/Admin/FlightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,14 +25,12 @@
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;
Expand Down Expand Up @@ -325,15 +325,7 @@ public function import(Request $request)
];

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', [
Expand Down
19 changes: 5 additions & 14 deletions app/Http/Controllers/Admin/SubfleetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,13 +20,13 @@
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;
Expand Down Expand Up @@ -293,17 +294,7 @@ public function import(Request $request)
];

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', [
Expand Down
59 changes: 59 additions & 0 deletions app/Http/Controllers/Admin/Traits/Importable.php
Original file line number Diff line number Diff line change
@@ -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!');
}
}
26 changes: 26 additions & 0 deletions app/Models/Enums/ImportExportType.php
Original file line number Diff line number Diff line change
@@ -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',
];
}
7 changes: 7 additions & 0 deletions resources/views/admin/common/import.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down

0 comments on commit be6c164

Please sign in to comment.