From 5663e6c2b829b79f849bb6703c27487c532ad5c2 Mon Sep 17 00:00:00 2001 From: Mazarin Date: Mon, 19 Feb 2024 20:02:55 -0500 Subject: [PATCH] feat: add a public note on a name on a public list (#55) --- .../StoreNoteForNameInListController.php | 80 +++++++++++++++++++ app/Http/ViewModels/User/ListViewModel.php | 6 ++ app/Models/Name.php | 2 +- app/Models/NameList.php | 2 +- app/Services/UpdateNoteToNameInList.php | 28 +++++++ ...24_02_19_182155_update_list_name_table.php | 15 ++++ phpstan.neon | 1 + .../views/user/lists/edit-note.blade.php | 62 ++++++++++++++ .../views/user/lists/partials/names.blade.php | 21 ++++- routes/web.php | 4 + .../ViewModels/User/ListViewModelTest.php | 2 + 11 files changed, 217 insertions(+), 6 deletions(-) create mode 100644 app/Http/Controllers/StoreNoteForNameInListController.php create mode 100644 app/Services/UpdateNoteToNameInList.php create mode 100644 database/migrations/2024_02_19_182155_update_list_name_table.php create mode 100644 resources/views/user/lists/edit-note.blade.php diff --git a/app/Http/Controllers/StoreNoteForNameInListController.php b/app/Http/Controllers/StoreNoteForNameInListController.php new file mode 100644 index 0000000..2930c44 --- /dev/null +++ b/app/Http/Controllers/StoreNoteForNameInListController.php @@ -0,0 +1,80 @@ +attributes->get('list'); + + $name = Name::findOrFail($nameId); + + $record = DB::table('list_name') + ->where('name_id', $nameId) + ->where('list_id', $listId) + ->first(); + + return view('user.lists.edit-note', [ + 'list' => [ + 'name' => $requestedList->name, + 'url' => [ + 'show' => route('list.show', ['liste' => $listId]), + 'update' => route('name.list.update', [ + 'liste' => $listId, + 'id' => $nameId, + ]), + ], + ], + 'name' => $name->name, + 'note' => is_null($record->public_note) ? '' : $record->public_note, + ]); + } + + /** + * Add a note for the given name in the given list. + */ + public function update(Request $request, int $listId, int $nameId): RedirectResponse + { + $requestedList = $request->attributes->get('list'); + + (new UpdateNoteToNameInList( + nameListId: $listId, + nameId: $nameId, + note: $request->input('note'), + ))->execute(); + + Cache::forget('route-list-' . $requestedList->id); + Cache::forget('user-lists-' . auth()->id()); + Cache::forget('list-details-' . $requestedList->id); + Cache::forget('admin-lists'); + + return Redirect::route('list.show', [ + 'liste' => $listId, + ]); + } + + public function destroy(Request $request, int $listId, int $nameId): void + { + $requestedList = $request->attributes->get('list'); + + (new ToggleNameToNameList( + nameId: $nameId, + listId: $requestedList->id, + ))->execute(); + + Cache::forget('route-list-' . $requestedList->id); + Cache::forget('user-lists-' . auth()->id()); + Cache::forget('list-details-' . $requestedList->id); + } +} diff --git a/app/Http/ViewModels/User/ListViewModel.php b/app/Http/ViewModels/User/ListViewModel.php index 1c15fef..26779c1 100644 --- a/app/Http/ViewModels/User/ListViewModel.php +++ b/app/Http/ViewModels/User/ListViewModel.php @@ -39,12 +39,14 @@ public static function index(): array public static function show(NameList $list): array { $names = $list->names() + ->withPivot('public_note') ->orderBy('name')->get() ->map(fn (Name $name) => [ 'id' => $name->id, 'name' => StringHelper::formatNameFromDB($name->name), 'origins' => Str::words($name->origins, 50, '...'), 'total' => Number::format($name->total), + 'public_note' => $name->pivot->public_note, 'url' => [ 'show' => route('name.show', [ 'id' => $name->id, @@ -57,6 +59,10 @@ public static function show(NameList $list): array 'liste' => $list->id, 'id' => $name->id, ]), + 'note' => route('name.list.edit', [ + 'liste' => $list->id, + 'id' => $name->id, + ]), ], ]); diff --git a/app/Models/Name.php b/app/Models/Name.php index 2f1fb6a..905b6de 100644 --- a/app/Models/Name.php +++ b/app/Models/Name.php @@ -67,7 +67,7 @@ public function mainCharacteristics(): BelongsToMany public function lists(): BelongsToMany { - return $this->belongsToMany(NameList::class, 'list_name', 'name_id', 'list_id'); + return $this->belongsToMany(NameList::class, 'list_name', 'name_id', 'list_id')->withPivot('public_note'); } public function toSitemapTag(): Url|string|array diff --git a/app/Models/NameList.php b/app/Models/NameList.php index 8ae2b24..b14fb5a 100644 --- a/app/Models/NameList.php +++ b/app/Models/NameList.php @@ -43,7 +43,7 @@ public function user(): BelongsTo public function names(): BelongsToMany { - return $this->belongsToMany(Name::class, 'list_name', 'list_id', 'name_id'); + return $this->belongsToMany(Name::class, 'list_name', 'list_id', 'name_id')->withPivot('public_note'); } public function scopeFavorite(Builder $query): void diff --git a/app/Services/UpdateNoteToNameInList.php b/app/Services/UpdateNoteToNameInList.php new file mode 100644 index 0000000..1c5a8b5 --- /dev/null +++ b/app/Services/UpdateNoteToNameInList.php @@ -0,0 +1,28 @@ +create(); + } + + private function create(): void + { + DB::table('list_name') + ->where('list_id', $this->nameListId) + ->where('name_id', $this->nameId) + ->update(['public_note' => $this->note]); + } +} diff --git a/database/migrations/2024_02_19_182155_update_list_name_table.php b/database/migrations/2024_02_19_182155_update_list_name_table.php new file mode 100644 index 0000000..9b79214 --- /dev/null +++ b/database/migrations/2024_02_19_182155_update_list_name_table.php @@ -0,0 +1,15 @@ +text('public_note')->nullable(); + }); + } +}; diff --git a/phpstan.neon b/phpstan.neon index a693457..f580ea0 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -24,6 +24,7 @@ parameters: - app/Http/Controllers/Auth/VerifyEmailController.php - app/Http/ViewModels/Names/NameViewModel.php - app/Http/ViewModels/Search/SearchViewModel.php + - app/Http/ViewModels/User/ListViewModel.php # # checkMissingIterableValueType: false checkGenericClassInNonGenericObjectType: false diff --git a/resources/views/user/lists/edit-note.blade.php b/resources/views/user/lists/edit-note.blade.php new file mode 100644 index 0000000..ac1476a --- /dev/null +++ b/resources/views/user/lists/edit-note.blade.php @@ -0,0 +1,62 @@ + +
+
+ @include('layouts.unlogged-navigation') +
+ +
+
+ +
+
+
+ +
+
+
+ @csrf + @method('PUT') + +
+

Ajout d'une note au prénom {{ $name }}

+
+ + +
+ + + {{ old('note', $note) }} + + +
+ + +
+ Retour + +
+ + Sauvegarder + +
+
+
+ +
+ +
diff --git a/resources/views/user/lists/partials/names.blade.php b/resources/views/user/lists/partials/names.blade.php index 94a73a7..1af7c33 100644 --- a/resources/views/user/lists/partials/names.blade.php +++ b/resources/views/user/lists/partials/names.blade.php @@ -1,16 +1,29 @@
@foreach ($list['names'] as $name)
-
-
{!! \App\Helpers\NameHelper::getAvatar($name['name']) !!}
+
+
{!! \App\Helpers\NameHelper::getAvatar($name['name']) !!}
{{ $name['name'] }} -

{{ $name['total'] }} utilisations depuis 1900

+ + @if ($name['public_note'] != '') +

{{ $name['public_note'] }}

+ @endif + + @if (auth()->user()->is_administrator) +

+ + ajouter une note +

+ @endif +

+ + {{ $name['total'] }} utilisations depuis 1900

diff --git a/routes/web.php b/routes/web.php index 7415b7b..431d0a4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -15,6 +15,7 @@ use App\Http\Controllers\PublicListController; use App\Http\Controllers\SearchController; use App\Http\Controllers\ShareController; +use App\Http\Controllers\StoreNoteForNameInListController; use App\Http\Controllers\TermsController; use App\Http\Controllers\UserNameController; use Illuminate\Support\Facades\Route; @@ -78,6 +79,9 @@ Route::delete('listes/{liste}/prenoms/{id}', [ListNameController::class, 'destroy'])->name('list.name.destroy'); Route::put('listes/{liste}/prenoms/{id}/set', [NameController::class, 'storeNameInList'])->name('name.list.store'); + + Route::get('listes/{liste}/prenoms/{id}/note/update', [StoreNoteForNameInListController::class, 'edit'])->name('name.list.edit'); + Route::put('listes/{liste}/prenoms/{id}/note', [StoreNoteForNameInListController::class, 'update'])->name('name.list.update'); }); Route::get('profil', [ProfileController::class, 'show'])->name('profile.show'); diff --git a/tests/Unit/ViewModels/User/ListViewModelTest.php b/tests/Unit/ViewModels/User/ListViewModelTest.php index 5e9c286..d6650f2 100644 --- a/tests/Unit/ViewModels/User/ListViewModelTest.php +++ b/tests/Unit/ViewModels/User/ListViewModelTest.php @@ -81,10 +81,12 @@ public function it_gets_the_list_of_names_in_the_list(): void 'total' => '1 000', 'origins' => 'test', 'name' => 'Test', + 'public_note' => null, 'url' => [ 'show' => env('APP_URL') . '/prenoms/' . $name->id . '/test', 'favorite' => env('APP_URL') . '/prenoms/' . $name->id . '/favorite', 'destroy' => env('APP_URL') . '/listes/' . $nameList->id . '/prenoms/' . $name->id, + 'note' => env('APP_URL') . '/listes/' . $nameList->id . '/prenoms/' . $name->id . '/note/update', ], ], ],