-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add checkbox to clear previous data when importing #490
- Loading branch information
Showing
9 changed files
with
122 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters