Archived
1
0
Fork 0
This repository has been archived on 2026-06-16. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
wow-raid-bingo/routes/console.php
2021-10-18 11:56:52 +02:00

183 lines
4.6 KiB
PHP

<?php
use App\Models\Card;
use App\Models\Raid;
use App\Models\Character;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Artisan;
/*
|--------------------------------------------------------------------------
| Console Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of your Closure based console
| commands. Each Closure is bound to a command instance allowing a
| simple approach to interacting with each command's IO methods.
|
*/
// Characters
// --------------------------
Artisan::command('character:add {name}', function ($name) {
Character::create([
'name' => Str::ucfirst($name),
]);
$this->info("Created character");
})->purpose('Add a character');
Artisan::command('character:edit {id} {name}', function ($id, $name) {
$character = Character::find($id);
if (!$character) {
$this->warn("Could not find character.");
return;
}
$character->name = Str::ucfirst($name);
$character->save();
$this->info("Updated character");
})->purpose('Edit a character');
Artisan::command('character:list {--trashed}', function ($trashed) {
$cols = ['id', 'name' ];
if ($trashed) {
$cols[] = 'deleted_at';
$q = Character::onlyTrashed();
} else {
$q = Character::query();
}
$this->table($cols, $q->select($cols)->get());
})->purpose('list characters');
Artisan::command('character:delete {id}', function ($id) {
$char = Character::find($id);
if ($char) {
$char->delete();
$this->info(sprintf("Character #%s deleted", $id));
} else {
$this->warn(sprintf("Could not find character with id %s", $id));
}
})->purpose('Delete a character');
// Raids
// --------------------------
Artisan::command('raid:add {name}', function ($name) {
Raid::create([
'name' => $name,
]);
$this->info("Created raid");
})->purpose('Add a raid');
Artisan::command('raid:edit {id} {name}', function ($id, $name) {
$raid = Raid::find($id);
if (!$raid ) {
$this->warn("Could not find raid.");
return;
}
$raid->name = $name;
$raid->save();
$this->info("Updated raid");
})->purpose('Edit a raid');
Artisan::command('raid:list {--trashed}', function ($trashed) {
$cols = ['id', 'name' ];
if ($trashed) {
$cols[] = 'deleted_at';
$q = Raid::onlyTrashed();
} else {
$q = Raid::query();
}
$this->table($cols, $q->select($cols)->get());
})->purpose('list raids');
Artisan::command('raid:delete {id}', function ($id) {
$raid = Raid::find($id);
if ($raid) {
$raid->delete();
$this->info(sprintf("Raid #%s deleted", $id));
} else {
$this->warn(sprintf("Could not find raid with id %s", $id));
}
})->purpose('Delete a raid');
// Cards
// --------------------------
Artisan::command('card:add {body} {--column=} {--value=}', function ($body, $column=null, $value=null) {
$data = [ 'body' => $body ];
if ($value && !in_array($column, ['character_id', 'raid_id', 'class', 'role'])) {
$this->warn(sprintf("Invalid column '%s'", $column));
return;
}
if ($value && $column) {
$data[$column] = $value;
}
Card::create($data);
$this->info("Created card");
})->purpose('Create a new card');
Artisan::command('card:edit {id} {--body=} {--column=} {--value=}', function ($id, $body=null, $column=null, $value=null) {
$card = Card::find($id);
if (!$card) {
$this->warn("Could not find card.");
return;
}
if ($body) {
$card->body = $body;
}
if ($value && in_array($column, ['character_id', 'raid_id', 'class', 'role'])) {
$card->character_id = null;
$card->raid_id = null;
$card->class = null;
$card->role = null;
$card->{$column} = $value;
}
$card->save();
$this->info("Updated card");
})->purpose('Update a card');
Artisan::command('card:list {--trashed}', function ($trashed) {
$cols = ['id', 'character_id', 'raid_id', 'role', 'class', 'body' ];
$q = Card::query();
if ($trashed) {
$cols[] = 'deleted_at';
$q->onlyTrashed();
}
$data = $q->select($cols)->get()->makeHidden('subject');
$this->table($cols, $data);
})->purpose('list cards');
Artisan::command('card:delete {id}', function ($id) {
$record = Card::find($id);
if ($record) {
$record->delete();
$this->info(sprintf("Card #%s deleted", $id));
} else {
$this->warn(sprintf("Could not find card with id %s", $id));
}
})->purpose('Delete a card');