Skip to content

Commit

Permalink
Merge pull request #548 from DnD-Montreal/186-adventure-import
Browse files Browse the repository at this point in the history
Add Adventure importer based of CSV files
  • Loading branch information
m-triassi authored Jun 15, 2022
2 parents 0561671 + 7eb284f commit 43dcf4b
Show file tree
Hide file tree
Showing 9 changed files with 659 additions and 3 deletions.
62 changes: 62 additions & 0 deletions app/Console/Commands/ImportAdventures.php
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;
}
}
40 changes: 40 additions & 0 deletions app/Imports/AdvenutreImport.php
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);
}
}
1 change: 1 addition & 0 deletions app/Models/Adventure.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Adventure extends Model
'title',
'code',
'description',
'tier'
];

/**
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"laravel/sanctum": "^2.14",
"laravel/tinker": "^2.5",
"lorisleiva/laravel-actions": "^2.1",
"maatwebsite/excel": "^3.1",
"tightenco/ziggy": "^1.0"
},
"require-dev": {
Expand Down
Loading

0 comments on commit 43dcf4b

Please sign in to comment.