#118 - wip
This commit is contained in:
37
app/Eloquent/Models/Key.php
Normal file
37
app/Eloquent/Models/Key.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Eloquent\Models;
|
||||
|
||||
use Database\Factories\KeyFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property User $owner
|
||||
* @property User $previousOwner
|
||||
*/
|
||||
class Key extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
public function owner(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, "owner_id");
|
||||
}
|
||||
|
||||
public function previousOwner(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, "previous_owner_id");
|
||||
}
|
||||
|
||||
protected static function newFactory(): KeyFactory
|
||||
{
|
||||
return KeyFactory::new();
|
||||
}
|
||||
}
|
23
app/Infrastructure/Http/Controllers/KeysController.php
Normal file
23
app/Infrastructure/Http/Controllers/KeysController.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Infrastructure\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Response;
|
||||
use Toby\Eloquent\Helpers\YearPeriodRetriever;
|
||||
use Toby\Eloquent\Models\Key;
|
||||
use Toby\Infrastructure\Http\Resources\KeyResource;
|
||||
|
||||
class KeysController extends Controller
|
||||
{
|
||||
public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever): Response
|
||||
{
|
||||
$keys = Key::query()->get();
|
||||
|
||||
return inertia("Keys/Index", [
|
||||
"keys" => KeyResource::collection($keys),
|
||||
]);
|
||||
}
|
||||
}
|
22
app/Infrastructure/Http/Resources/KeyResource.php
Normal file
22
app/Infrastructure/Http/Resources/KeyResource.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Infrastructure\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class KeyResource extends JsonResource
|
||||
{
|
||||
public static $wrap = null;
|
||||
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
"id" => $this->id,
|
||||
"owner" => new UserResource($this->owner),
|
||||
"previousOwner" => new UserResource($this->previousOwner),
|
||||
"updatedAt" => $this->updated_at->toDatetimeString(),
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user