-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #548 from DnD-Montreal/186-adventure-import
Add Adventure importer based of CSV files
- Loading branch information
Showing
9 changed files
with
659 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Imports\AdvenutreImport; | ||
use App\Models\Adventure; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Storage; | ||
use Maatwebsite\Excel\Facades\Excel; | ||
|
||
class ImportAdventures extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'adventures:import'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create entries for all adventures from CSVs in the storage/app/adventures directory'; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
$files = Storage::disk('local')->files('adventures'); | ||
$fileCount = count($files); | ||
$bar = $this->output->createProgressBar($fileCount); | ||
$this->info("Importing Adventures from {$fileCount} CSVs."); | ||
$bar->start(); | ||
|
||
foreach ($files as $file) { | ||
Excel::import(new AdvenutreImport(), $file); | ||
$bar->advance(); | ||
} | ||
|
||
$bar->finish(); | ||
|
||
$adventureCount = Adventure::count(); | ||
$this->info("\nImport Complete, {$adventureCount} adventures created / Updated."); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
namespace App\Imports; | ||
|
||
use App\Models\Adventure; | ||
use Illuminate\Support\Str; | ||
use Maatwebsite\Excel\Concerns\ToModel; | ||
use Maatwebsite\Excel\Concerns\WithHeadingRow; | ||
use Maatwebsite\Excel\Concerns\WithUpserts; | ||
|
||
class AdvenutreImport implements ToModel, WithUpserts | ||
{ | ||
/** | ||
* @param array $row | ||
* | ||
* @return \Illuminate\Database\Eloquent\Model|null | ||
*/ | ||
public function model(array $row) | ||
{ | ||
if (!isset($row[0]) || Str::contains($row[0], 'Adventure')) { | ||
return null; | ||
} | ||
|
||
return new Adventure([ | ||
'code' => trim($row[0]), | ||
'title' => trim($row[1]), | ||
'tier' => $this->determineTier($row[3]) | ||
]); | ||
} | ||
|
||
public function uniqueBy(): string | ||
{ | ||
return "code"; | ||
} | ||
|
||
private function determineTier($level): int | ||
{ | ||
return 1 + intval($level >= 5) + intval($level >= 11) + intval($level >= 16); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -22,6 +22,7 @@ class Adventure extends Model | |
'title', | ||
'code', | ||
'description', | ||
'tier' | ||
]; | ||
|
||
/** | ||
|
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
Oops, something went wrong.