#4 - wip
This commit is contained in:
24
app/Console/Commands/CreateUserCommand.php
Normal file
24
app/Console/Commands/CreateUserCommand.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Toby\Models\User;
|
||||
|
||||
class CreateUserCommand extends Command
|
||||
{
|
||||
protected $signature = "user:create
|
||||
{email : an email for the user}";
|
||||
protected $description = "Creates user";
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$email = $this->argument("email");
|
||||
|
||||
User::factory(["email" => $email])->create();
|
||||
|
||||
$this->info("User has been created");
|
||||
}
|
||||
}
|
18
app/Enums/FormOfEmployment.php
Normal file
18
app/Enums/FormOfEmployment.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Enums;
|
||||
|
||||
enum FormOfEmployment: string
|
||||
{
|
||||
case EMPLOYMENT_CONTRACT = "employment_contract";
|
||||
case COMMISSION_CONTRACT = "commission_contract";
|
||||
case B2B_CONTRACT = "b2b_contract";
|
||||
case BOARD_MEMBER_CONTRACT = "board_member_contract";
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return __($this->value);
|
||||
}
|
||||
}
|
@@ -4,16 +4,23 @@ declare(strict_types=1);
|
||||
|
||||
namespace Toby\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Response;
|
||||
use Toby\Http\Resources\UserResource;
|
||||
use Toby\Models\User;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function index(): Response
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$users = User::query()
|
||||
->search($request->query("search"))
|
||||
->paginate()
|
||||
->withQueryString();
|
||||
|
||||
return inertia("Users/Index", [
|
||||
"users" => UserResource::collection(User::all()),
|
||||
"users" => UserResource::collection($users),
|
||||
"filters" => $request->only("search"),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -15,7 +15,9 @@ class UserResource extends JsonResource
|
||||
"name" => $this->name,
|
||||
"email" => $this->email,
|
||||
"role" => "Human Resources Manager",
|
||||
"avatar" => "https://images.unsplash.com/photo-1550525811-e5869dd03032?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80",
|
||||
"avatar" => asset($this->avatar),
|
||||
"employmentForm" => $this->employment_form->label(),
|
||||
"employmentStartDate" => $this->employment_start_date->translatedFormat("j F Y"),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@@ -4,26 +4,53 @@ declare(strict_types=1);
|
||||
|
||||
namespace Toby\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Toby\Enums\FormOfEmployment;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $email
|
||||
* @property string $avatar
|
||||
* @property FormOfEmployment $employment_form
|
||||
* @property Carbon $empoyment_start_date
|
||||
*/
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasFactory;
|
||||
use Notifiable;
|
||||
|
||||
protected $perPage = 10;
|
||||
|
||||
protected $fillable = [
|
||||
"name",
|
||||
"email",
|
||||
"avatar",
|
||||
"employment_form",
|
||||
"employment_start_date",
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
"employment_form" => FormOfEmployment::class,
|
||||
"employment_start_date" => "datetime",
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
"remember_token",
|
||||
];
|
||||
|
||||
public function scopeSearch(Builder $query, ?string $text): Builder
|
||||
{
|
||||
if ($text == null) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
return $query
|
||||
->where("name", "LIKE", "%{$text}%")
|
||||
->orWhere("email", "LIKE", "%{$text}%");
|
||||
}
|
||||
}
|
||||
|
52
app/Observers/UserObserver.php
Normal file
52
app/Observers/UserObserver.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Observers;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Toby\Models\User;
|
||||
use LasseRafn\InitialAvatarGenerator\InitialAvatar;
|
||||
|
||||
class UserObserver
|
||||
{
|
||||
public function __construct(protected InitialAvatar $generator)
|
||||
{
|
||||
}
|
||||
|
||||
public function created(User $user): void
|
||||
{
|
||||
$user->avatar = $this->generateAvatar($user);
|
||||
|
||||
$user->save();
|
||||
}
|
||||
|
||||
public function updating(User $user): void
|
||||
{
|
||||
if ($user->isDirty("name")) {
|
||||
$user->avatar = $this->generateAvatar($user);
|
||||
}
|
||||
}
|
||||
|
||||
public function deleted(User $user): void
|
||||
{
|
||||
Storage::delete($user->avatar);
|
||||
}
|
||||
|
||||
protected function generateAvatar(User $user): string
|
||||
{
|
||||
$path = "avatars/{$user->id}.svg";
|
||||
|
||||
Storage::put($path, $this->generator->rounded()->background($this->getRandomColor())->generateSvg($user->name));
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
protected function getRandomColor(): string
|
||||
{
|
||||
$colors = config("colors");
|
||||
|
||||
return Arr::random($colors);
|
||||
}
|
||||
}
|
@@ -5,7 +5,13 @@ declare(strict_types=1);
|
||||
namespace Toby\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Toby\Models\User;
|
||||
use Toby\Observers\UserObserver;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
User::observe(UserObserver::class);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user