init: commands

This commit is contained in:
b1ek 2024-09-17 22:50:06 +10:00
parent 86c582d273
commit 9ac92b9890
Signed by: blek
GPG Key ID: A622C22C9BC616B2
3 changed files with 123 additions and 0 deletions

View File

@ -0,0 +1,45 @@
<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
class AddBalance extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:add-balance {id} {balance}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Add balance to user';
/**
* Execute the console command.
*/
public function handle()
{
$balance = (int) $this->argument('balance');
$id = (int) $this->argument('id');
if ($balance == 0 || $id == 0) {
$this->fail('Balance or Id must be non zero value!');
}
/** @var User */
$user = User::find([ 'id' => $id ])->firstOrFail();
$user->balance += $balance;
$user->save();
$name = $user->name;
$balance = $user->balance;
$this->info("User $name has $balance balance now");
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
class MakeUser extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:user {name}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new user';
/**
* Execute the console command.
*/
public function handle()
{
$name = (string) $this->argument('name');
$user = new User();
$user->name = $name;
$user->save();
$id = $user->id;
$this->info("Created user with id $id");
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
class ShowUsers extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'show:users {page=1}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Show all users';
/**
* Execute the console command.
*/
public function handle()
{
$page = (int) $this->argument('page');
$users = User::all()->forPage($page, 20)->all();
$this->info("Listing all users for page $page");
$this->info("Id\tName\tBalance");
foreach ($users as $user) {
$this->info($user->id . ":\t" . $user->name . "\t" . $user->balance);
}
}
}