From 6824f15e00a1094fac80e5152be02c6ff1b5808e Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Mon, 12 Feb 2024 22:32:08 +0100 Subject: [PATCH 1/2] refactor: some refactors --- app/Http/Kernel.php | 23 +++++++++---------- app/Http/ViewModels/Home/HomeViewModel.php | 2 +- .../ViewModels/Names/AllNamesViewModel.php | 4 ++-- .../ViewModels/Names/FemaleNamesViewModel.php | 4 ++-- .../ViewModels/Names/MaleNamesViewModel.php | 4 ++-- .../ViewModels/Names/MixteNamesViewModel.php | 4 ++-- app/Http/ViewModels/Names/NameViewModel.php | 10 ++------ .../ViewModels/Search/SearchViewModel.php | 4 +--- app/Http/ViewModels/User/ListViewModel.php | 4 ++-- app/Http/ViewModels/User/UserViewModel.php | 2 +- app/Models/NameList.php | 6 +++++ app/Providers/AppServiceProvider.php | 4 +++- app/Services/ToggleNameToFavorites.php | 4 ++-- app/Services/ToggleNameToNameList.php | 5 ++-- 14 files changed, 39 insertions(+), 41 deletions(-) diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 6bac23a..05df454 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -14,12 +14,11 @@ class Kernel extends HttpKernel * @var array */ protected $middleware = [ - // \App\Http\Middleware\TrustHosts::class, - Middleware\TrustProxies::class, + \App\Http\Middleware\TrustProxies::class, \Illuminate\Http\Middleware\HandleCors::class, - Middleware\PreventRequestsDuringMaintenance::class, + \App\Http\Middleware\PreventRequestsDuringMaintenance::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, - Middleware\TrimStrings::class, + \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, ]; @@ -30,11 +29,11 @@ class Kernel extends HttpKernel */ protected $middlewareGroups = [ 'web' => [ - Middleware\EncryptCookies::class, + \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, - Middleware\VerifyCsrfToken::class, + \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], @@ -53,19 +52,19 @@ class Kernel extends HttpKernel * @var array */ protected $middlewareAliases = [ - 'auth' => Middleware\Authenticate::class, + 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, - 'guest' => Middleware\RedirectIfAuthenticated::class, + 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class, 'precognitive' => \Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class, - 'signed' => Middleware\ValidateSignature::class, + 'signed' => \App\Http\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, - 'name' => Middleware\CheckName::class, - 'letter' => Middleware\CheckLetter::class, - 'list' => Middleware\CheckList::class, + 'name' => \App\Http\Middleware\CheckName::class, + 'letter' => \App\Http\Middleware\CheckLetter::class, + 'list' => \App\Http\Middleware\CheckList::class, ]; } diff --git a/app/Http/ViewModels/Home/HomeViewModel.php b/app/Http/ViewModels/Home/HomeViewModel.php index 13fd113..4a7d7a4 100644 --- a/app/Http/ViewModels/Home/HomeViewModel.php +++ b/app/Http/ViewModels/Home/HomeViewModel.php @@ -73,7 +73,7 @@ public static function serverStats(): array $totalNames = Name::where('name', '!=', '_PRENOMS_RARES')->count(); return [ - 'total_names' => Number::format($totalNames, locale: 'fr'), + 'total_names' => Number::format($totalNames), ]; } } diff --git a/app/Http/ViewModels/Names/AllNamesViewModel.php b/app/Http/ViewModels/Names/AllNamesViewModel.php index 000d9c9..848e930 100644 --- a/app/Http/ViewModels/Names/AllNamesViewModel.php +++ b/app/Http/ViewModels/Names/AllNamesViewModel.php @@ -15,7 +15,7 @@ public static function index(): Collection $allNames = Name::where('name', '!=', '_PRENOMS_RARES')->count(); $letters->push([ 'letter' => 'Tous', - 'count' => Number::format($allNames, locale: 'fr'), + 'count' => Number::format($allNames), 'url' => route('name.index'), ]); @@ -25,7 +25,7 @@ public static function index(): Collection $letters->push([ 'letter' => $letter, - 'count' => Number::format($count, locale: 'fr'), + 'count' => Number::format($count), 'url' => route('name.letter', ['letter' => Str::lcfirst($letter)]), ]); } diff --git a/app/Http/ViewModels/Names/FemaleNamesViewModel.php b/app/Http/ViewModels/Names/FemaleNamesViewModel.php index e1e53cd..af2ad08 100644 --- a/app/Http/ViewModels/Names/FemaleNamesViewModel.php +++ b/app/Http/ViewModels/Names/FemaleNamesViewModel.php @@ -20,7 +20,7 @@ public static function index(): Collection $letters = collect(); $letters->push([ 'letter' => 'Tous', - 'count' => Number::format($total, locale: 'fr'), + 'count' => Number::format($total), 'url' => route('name.fille.index'), ]); @@ -29,7 +29,7 @@ public static function index(): Collection ->where('name', 'like', $letter . '%')->count(); $letters->push([ 'letter' => $letter, - 'count' => Number::format($total, locale: 'fr'), + 'count' => Number::format($total), 'url' => route('name.fille.letter', [ 'letter' => Str::lcfirst($letter), ]), diff --git a/app/Http/ViewModels/Names/MaleNamesViewModel.php b/app/Http/ViewModels/Names/MaleNamesViewModel.php index 9d5ce54..27cb799 100644 --- a/app/Http/ViewModels/Names/MaleNamesViewModel.php +++ b/app/Http/ViewModels/Names/MaleNamesViewModel.php @@ -20,7 +20,7 @@ public static function index(): Collection $letters = collect(); $letters->push([ 'letter' => 'Tous', - 'count' => Number::format($total, locale: 'fr'), + 'count' => Number::format($total), 'url' => route('name.garcon.index'), ]); @@ -29,7 +29,7 @@ public static function index(): Collection ->where('name', 'like', $letter . '%')->count(); $letters->push([ 'letter' => $letter, - 'count' => Number::format($total, locale: 'fr'), + 'count' => Number::format($total), 'url' => route('name.garcon.letter', [ 'letter' => Str::lcfirst($letter), ]), diff --git a/app/Http/ViewModels/Names/MixteNamesViewModel.php b/app/Http/ViewModels/Names/MixteNamesViewModel.php index 8e8f538..d85e829 100644 --- a/app/Http/ViewModels/Names/MixteNamesViewModel.php +++ b/app/Http/ViewModels/Names/MixteNamesViewModel.php @@ -20,7 +20,7 @@ public static function index(): Collection $letters = collect(); $letters->push([ 'letter' => 'Tous', - 'count' => Number::format($total, locale: 'fr'), + 'count' => Number::format($total), 'url' => route('name.mixte.index'), ]); @@ -29,7 +29,7 @@ public static function index(): Collection ->where('name', 'like', $letter . '%')->count(); $letters->push([ 'letter' => $letter, - 'count' => Number::format($total, locale: 'fr'), + 'count' => Number::format($total), 'url' => route('name.mixte.letter', [ 'letter' => Str::lcfirst($letter), ]), diff --git a/app/Http/ViewModels/Names/NameViewModel.php b/app/Http/ViewModels/Names/NameViewModel.php index 210c81f..74d0a9b 100644 --- a/app/Http/ViewModels/Names/NameViewModel.php +++ b/app/Http/ViewModels/Names/NameViewModel.php @@ -30,8 +30,6 @@ public static function summary(mixed $name): array public static function details(Name $name): array { - Carbon::setLocale('fr'); - return [ 'id' => $name->id, 'name' => StringHelper::formatNameFromDB($name->name), @@ -84,11 +82,7 @@ public static function popularity(Name $name): array // now we need to add the percentage of popularity for each decade $total = $decadesCollection->sum('popularity'); $decadesCollection = $decadesCollection->map(function ($decade) use ($total) { - if ($total > 0) { - $decade['percentage'] = Number::format(round($decade['popularity'] / $total * 100), locale: 'fr'); - } else { - $decade['percentage'] = 0; - } + $decade['percentage'] = $total > 0 ? Number::format(round($decade['popularity'] / $total * 100)) : 0; return $decade; }); @@ -98,7 +92,7 @@ public static function popularity(Name $name): array return [ 'decades' => $decadesCollection, - 'total' => Number::format($total, locale: 'fr'), + 'total' => Number::format($total), ]; } diff --git a/app/Http/ViewModels/Search/SearchViewModel.php b/app/Http/ViewModels/Search/SearchViewModel.php index 0d35512..df9193e 100644 --- a/app/Http/ViewModels/Search/SearchViewModel.php +++ b/app/Http/ViewModels/Search/SearchViewModel.php @@ -27,9 +27,7 @@ public static function names(?string $term = null, int $limit = 20): array ]), ], ]) - ->filter(function ($name) { - return strpos($name['name'], '_prenoms_rares') === false; - }); + ->filter(fn ($name) => strpos($name['name'], '_prenoms_rares') === false); return [ 'names' => $names, diff --git a/app/Http/ViewModels/User/ListViewModel.php b/app/Http/ViewModels/User/ListViewModel.php index bc3bfc0..e6a3a8e 100644 --- a/app/Http/ViewModels/User/ListViewModel.php +++ b/app/Http/ViewModels/User/ListViewModel.php @@ -22,7 +22,7 @@ public static function index(): array ->map(fn (NameList $list) => [ 'id' => $list->id, 'name' => $list->name, - 'total' => Number::format($list->names_count, locale: 'fr'), + 'total' => Number::format($list->names_count), 'url' => [ 'show' => route('list.show', [ 'liste' => $list->id, @@ -42,7 +42,7 @@ public static function show(NameList $list): array ->map(fn (Name $name) => [ 'id' => $name->id, 'name' => StringHelper::formatNameFromDB($name->name), - 'total' => Number::format($name->total, locale: 'fr'), + 'total' => Number::format($name->total), 'url' => [ 'show' => route('name.show', [ 'id' => $name->id, diff --git a/app/Http/ViewModels/User/UserViewModel.php b/app/Http/ViewModels/User/UserViewModel.php index 6f6307f..032363c 100644 --- a/app/Http/ViewModels/User/UserViewModel.php +++ b/app/Http/ViewModels/User/UserViewModel.php @@ -34,7 +34,7 @@ public static function index(): array ->map(fn (Name $name) => [ 'id' => $name->id, 'name' => StringHelper::formatNameFromDB($name->name), - 'total' => Number::format($name->total, locale: 'fr'), + 'total' => Number::format($name->total), 'note' => $name->getNoteForUser(), 'url' => [ 'show' => route('name.show', [ diff --git a/app/Models/NameList.php b/app/Models/NameList.php index f621954..5aace04 100644 --- a/app/Models/NameList.php +++ b/app/Models/NameList.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -43,4 +44,9 @@ public function names(): BelongsToMany { return $this->belongsToMany(Name::class, 'list_name', 'list_id', 'name_id'); } + + public function scopeFavorite(Builder $query): void + { + $query->where('is_list_of_favorites', 1); + } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 452e6b6..2c02b69 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,8 @@ namespace App\Providers; +use Illuminate\Support\Facades\App; +use Illuminate\Support\Number; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -19,6 +21,6 @@ public function register(): void */ public function boot(): void { - // + Number::useLocale(App::currentLocale()); } } diff --git a/app/Services/ToggleNameToFavorites.php b/app/Services/ToggleNameToFavorites.php index 06393b5..66e967e 100644 --- a/app/Services/ToggleNameToFavorites.php +++ b/app/Services/ToggleNameToFavorites.php @@ -27,8 +27,8 @@ public function execute(): bool private function findFavoritesList(): void { - $this->list = NameList::where('user_id', auth()->id()) - ->where('is_list_of_favorites', true) + $this->list = auth()->user()->lists() + ->favorite() ->firstOrFail(); } diff --git a/app/Services/ToggleNameToNameList.php b/app/Services/ToggleNameToNameList.php index 5a8ce5a..ccb1e3a 100644 --- a/app/Services/ToggleNameToNameList.php +++ b/app/Services/ToggleNameToNameList.php @@ -23,9 +23,8 @@ public function execute(): void private function addOrRemove(): void { - $list = NameList::where('user_id', auth()->id()) - ->where('id', $this->listId) - ->firstOrFail(); + $list = auth()->user()->lists() + ->findOrFail($this->listId); $list->names()->toggle([$this->nameId]); } From d15c049aff5ebeda740a77c18433b6947165968b Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Tue, 13 Feb 2024 22:25:04 +0100 Subject: [PATCH 2/2] fix --- tests/Unit/Services/ToggleNameToFavoritesTest.php | 2 ++ tests/Unit/Services/ToggleNameToNameListTest.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/Unit/Services/ToggleNameToFavoritesTest.php b/tests/Unit/Services/ToggleNameToFavoritesTest.php index 21e229b..bac34f7 100644 --- a/tests/Unit/Services/ToggleNameToFavoritesTest.php +++ b/tests/Unit/Services/ToggleNameToFavoritesTest.php @@ -55,6 +55,8 @@ public function it_fails_if_the_list_of_favorites_doesnt_exist(): void $user = User::factory()->create(); $name = Name::factory()->create(); + $this->actingAs($user); + $this->expectException(ModelNotFoundException::class); (new ToggleNameToFavorites( nameId: $name->id, diff --git a/tests/Unit/Services/ToggleNameToNameListTest.php b/tests/Unit/Services/ToggleNameToNameListTest.php index 3b13611..fd4678f 100644 --- a/tests/Unit/Services/ToggleNameToNameListTest.php +++ b/tests/Unit/Services/ToggleNameToNameListTest.php @@ -44,6 +44,8 @@ public function it_fails_if_the_list_of_favorites_doesnt_exist(): void $name = Name::factory()->create(); $list = NameList::factory()->create(); + $this->actingAs($user); + $this->expectException(ModelNotFoundException::class); (new ToggleNameToNameList(