Archived
1
0
Fork 0

Initial Commit

This commit is contained in:
Henrik Hautakoski 2021-10-18 11:53:33 +02:00
commit ddf09fe00c
113 changed files with 187148 additions and 0 deletions

View file

@ -0,0 +1,74 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Character;
use App\Models\Raid;
use App\Models\Card;
use Illuminate\Support\Str;
class CardImport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'card:import {file}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import card data';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$file = $this->argument('file');
$data = file_get_contents($file);
try {
$json = json_decode($data, false, 8, JSON_THROW_ON_ERROR);
} catch(\JsonException $ex) {
$this->error("Json: " . $ex->getMessage());
return;
}
foreach($json as $item) {
$data = [
'body' => $item->message,
];
if (isset($item->character)) {
$character = Character::firstOrCreate(['name' => $item->character]);
$data['character_id'] = $character->id;
}
if (isset($item->raid)) {
$raid = Raid::firstOrCreate(['name' => $item->raid]);
$data['raid_id'] = $raid->id;
}
if (isset($item->class)) {
$data['class'] = Str::lower($item->class);
}
if (isset($item->role)) {
$data['role'] = $item->role;
}
Card::insert($data);
}
}
}