Skip to content

Commit b4fe5d2

Browse files
committed
updated tutorial
1 parent 01884d6 commit b4fe5d2

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

tutorial-building-todo-app.html

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,15 @@ <h3 class="text-lg font-semibold mb-3">Creating the Migration</h3>
244244
use App\Modules\ForgeDatabaseSQL\DB\Attributes\Column;
245245
use App\Modules\ForgeDatabaseSQL\DB\Attributes\Index;
246246
use App\Modules\ForgeDatabaseSQL\DB\Attributes\Table;
247+
use App\Modules\ForgeDatabaseSQL\DB\Attributes\Timestamps;
247248
use App\Modules\ForgeDatabaseSQL\DB\Enums\ColumnType;
248249
use App\Modules\ForgeDatabaseSQL\DB\Migrations\Migration;
249250

250251
#[Table(name: 'todos')]
251252
#[BelongsTo(related: User::class)]
252253
#[Index(columns: ['user_id'], name: 'idx_todos_user_id')]
253254
#[Index(columns: ['completed'], name: 'idx_todos_completed')]
255+
#[Timestamps]
254256
class CreateTodosTable extends Migration
255257
{
256258
#[Column(name: 'id', type: ColumnType::INTEGER, primaryKey: true, autoIncrement: true)]
@@ -340,13 +342,24 @@ <h3 class="text-lg font-semibold mb-3 mt-6">Creating a Repository</h3>
340342

341343
namespace App\Repositories;
342344

345+
use App\Dto\CreateTodoDTO;
343346
use App\Models\Todo;
344347
use App\Modules\ForgeSqlOrm\ORM\RecordRepository;
345348

346349
class TodoRepository extends RecordRepository
347350
{
348351
protected string $model = Todo::class;
349352

353+
public function create(CreateTodoDTO $dto, int $userId): Todo
354+
{
355+
return parent::create([
356+
'user_id' => $userId,
357+
'title' => $dto->title,
358+
'description' => $dto->description,
359+
'completed' => $dto->completed,
360+
]);
361+
}
362+
350363
public function findByUserId(int $userId): array
351364
{
352365
return $this->query()
@@ -374,7 +387,7 @@ <h3 class="text-lg font-semibold mb-3 mt-6">Creating a Repository</h3>
374387
}
375388
}</code></pre>
376389
<p class="text-gray-600 mb-4">
377-
The repository extends <code class="bg-gray-100 px-2 py-1 rounded">RecordRepository</code>, which provides a <code class="bg-gray-100 px-2 py-1 rounded">create()</code> method that accepts an array of data and returns the created model instance. This method handles model instantiation, property assignment, saving, and cache invalidation automatically.
390+
The repository extends <code class="bg-gray-100 px-2 py-1 rounded">RecordRepository</code>, which provides a base <code class="bg-gray-100 px-2 py-1 rounded">create()</code> method that accepts an array. We've added a type-safe <code class="bg-gray-100 px-2 py-1 rounded">create()</code> method that accepts a <code class="bg-gray-100 px-2 py-1 rounded">CreateTodoDTO</code> and user ID, which internally calls the parent method with the properly structured data. This provides better type safety and ensures consistent data structure.
378391
</p>
379392
</section>
380393

@@ -567,12 +580,7 @@ <h3 class="text-lg font-semibold mb-3">Creating TodoController</h3>
567580

568581
$dto = CreateTodoDTO::fromArray($data);
569582

570-
$todo = $this->repository->create([
571-
'user_id' => $user->id,
572-
'title' => $dto->title,
573-
'description' => $dto->description,
574-
'completed' => $dto->completed,
575-
]);
583+
$todo = $this->repository->create($dto, $user->id);
576584

577585
Flash::set("success", "Todo created successfully");
578586
return Redirect::to("/todos");
@@ -809,6 +817,7 @@ <h3 class="text-lg font-semibold mb-3">Creating the TodoList Wire Component</h3>
809817

810818
namespace App\Components\Wire;
811819

820+
use App\Dto\CreateTodoDTO;
812821
use App\Models\Todo;
813822
use App\Modules\ForgeAuth\Services\ForgeAuthService;
814823
use App\Modules\ForgeWire\Attributes\Action;
@@ -849,12 +858,13 @@ <h3 class="text-lg font-semibold mb-3">Creating the TodoList Wire Component</h3>
849858
return;
850859
}
851860

852-
$this->repository->create([
853-
'user_id' => $user->id,
854-
'title' => $this->newTodoTitle,
855-
'description' => $this->newTodoDescription ?: null,
856-
'completed' => false,
857-
]);
861+
$dto = new CreateTodoDTO(
862+
title: $this->newTodoTitle,
863+
description: $this->newTodoDescription ?: null,
864+
completed: false,
865+
);
866+
867+
$this->repository->create($dto, $user->id);
858868

859869
$this->newTodoTitle = '';
860870
$this->newTodoDescription = '';

0 commit comments

Comments
 (0)