Skip to content

Commit cfedec0

Browse files
committed
refactor: update notes feature
1 parent 2e15107 commit cfedec0

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Personal project to learn [Tailwindcss](https://tailwindcss.com/) and [Laravel 8
88

99
Due to I have another project with Jetstream [Laravel Livewire Publications](https://github.com/mascam97/laravel-livewire-publications), this project is no longer maintained.
1010

11-
### Achievements :star2:
11+
### Achievements 2021 :star2:
1212

1313
- Install all the package from Jetstream and Sail.
1414
- Learn better practices with Docker compose.
@@ -17,6 +17,11 @@ Due to I have another project with Jetstream [Laravel Livewire Publications](htt
1717
- Create a Middleware to allow request about notes just from its owner.
1818
- Tested with PHPUnit (**Test-Driven Development**) to Models, CRUD implemented and Middleware.
1919

20+
### Achievements 2023 :star2:
21+
22+
- Refactored some code with best practices
23+
- Implemented some design patters: QueryBuilder, Data Transfer Object and Actions
24+
2025
---
2126

2227
## Getting Started :rocket:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Actions\Notes;
4+
5+
use App\Dtos\Notes\UpdateNoteData;
6+
use App\Models\Note;
7+
8+
class UpdateNoteAction
9+
{
10+
public function handle(Note $note, UpdateNoteData $data): Note
11+
{
12+
if ($data->title){
13+
$note->title = $data->title;
14+
}
15+
16+
if ($data->content){
17+
$note->content = $data->content;
18+
}
19+
20+
$note->update();
21+
22+
return $note;
23+
}
24+
}

app/Dtos/Notes/UpdateNoteData.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Dtos\Notes;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class UpdateNoteData
8+
{
9+
public function __construct(
10+
public ?string $title = null,
11+
public ?string $content = null,
12+
) {
13+
}
14+
15+
public static function fromRequest(FormRequest $request): self
16+
{
17+
return new static(
18+
title: $request->input('title'),
19+
content: $request->input('content'),
20+
);
21+
}
22+
}

app/Http/Controllers/NoteController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace App\Http\Controllers;
44

55
use App\Actions\Notes\StoreNoteAction;
6+
use App\Actions\Notes\UpdateNoteAction;
67
use App\Dtos\Notes\StoreNoteData;
8+
use App\Dtos\Notes\UpdateNoteData;
79
use App\Exceptions\NoteExceptions;
810
use App\Models\Note;
911
use App\Models\User;
@@ -67,7 +69,9 @@ public function edit(Note $note): Response
6769

6870
public function update(NoteRequest $request, Note $note): RedirectResponse
6971
{
70-
$note->update($request->all());
72+
$data = UpdateNoteData::fromRequest($request);
73+
74+
(new UpdateNoteAction())->handle($note, $data);
7175

7276
return redirect()->route('notes.index')->with('status', 'Note updated!!');
7377
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Tests\Unit\Actions\Notes;
4+
5+
use App\Actions\Notes\UpdateNoteAction;
6+
use App\Dtos\Notes\UpdateNoteData;
7+
use App\Models\Note;
8+
use App\Models\User;
9+
use Illuminate\Foundation\Testing\RefreshDatabase;
10+
use Tests\TestCase;
11+
12+
class UpdateNoteActionTest extends TestCase
13+
{
14+
use RefreshDatabase;
15+
16+
private User $user;
17+
18+
private Note $note;
19+
20+
protected function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
$this->user = User::factory()->create();
25+
$this->note = Note::factory()->create([
26+
'user_id' => $this->user->getKey(),
27+
'title' => 'Old title',
28+
'content' => 'Old content',
29+
]);
30+
}
31+
32+
public function test_can_update_the_title_note()
33+
{
34+
$data = new UpdateNoteData(title: 'New title');
35+
36+
$note = (new UpdateNoteAction())->handle($this->note, $data);
37+
38+
$this->assertEquals('New title', $note->title);
39+
$this->assertEquals('Old content', $note->content);
40+
}
41+
42+
public function test_can_update_the_content_note()
43+
{
44+
$data = new UpdateNoteData(content: 'New content');
45+
46+
$note = (new UpdateNoteAction())->handle($this->note, $data);
47+
48+
$this->assertEquals('Old title', $note->title);
49+
$this->assertEquals('New content', $note->content);
50+
}
51+
}

0 commit comments

Comments
 (0)