diff --git a/app/Console/Commands/FetchMetaData.php b/app/Console/Commands/FetchMetaData.php index a4b5f0f..0855c73 100644 --- a/app/Console/Commands/FetchMetaData.php +++ b/app/Console/Commands/FetchMetaData.php @@ -40,11 +40,7 @@ class FetchMetaData extends Command */ public function handle(): void { - foreach (Name::orderBy('total', 'desc')->lazy() as $name) { - if ($name->name === '_PRENOMS_RARES') { - continue; - } - + foreach (Name::nonRares()->orderBy('total', 'desc')->lazy() as $name) { if (is_null($name->origins)) { ProcessOrigins::dispatch($name); } diff --git a/app/Console/Commands/GenerateSitemap.php b/app/Console/Commands/GenerateSitemap.php index 9347ec1..f84c378 100644 --- a/app/Console/Commands/GenerateSitemap.php +++ b/app/Console/Commands/GenerateSitemap.php @@ -84,7 +84,7 @@ private function sitemap_root(SitemapIndex $sitemapIndex): void */ private function sitemap_names(SitemapIndex $sitemapIndex): void { - Name::where('name', '!=', '_PRENOMS_RARES') + Name::nonRares() ->chunkById(2000, function (Collection $names, int $key) use ($sitemapIndex) { $file = $this->file('sitemap_' . Str::padLeft("$key", 2, '0') . '.xml'); diff --git a/app/Http/Controllers/FemaleNameController.php b/app/Http/Controllers/FemaleNameController.php index 966364f..ac71ae8 100644 --- a/app/Http/Controllers/FemaleNameController.php +++ b/app/Http/Controllers/FemaleNameController.php @@ -21,8 +21,8 @@ public function index(Request $request): View $letters = Cache::remember('all-letters-female', 604800, fn () => FemaleNamesViewModel::index()); - $namesPagination = Cache::remember('all-names-female-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES') - ->where('gender', 'female') + $namesPagination = Cache::remember('all-names-female-page-' . $requestedPage, 604800, fn () => Name::nonRares() + ->female() ->orderBy('total', 'desc') ->paginate(40) ); @@ -49,8 +49,8 @@ public function letter(Request $request): View $letters = Cache::remember('all-letters-female', 604800, fn () => FemaleNamesViewModel::index()); - $namesPagination = Cache::remember('female-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES') - ->where('gender', 'female') + $namesPagination = Cache::remember('female-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::nonRares() + ->female() ->where('name', 'like', Str::upper($requestedLetter) . '%') ->orderBy('total', 'desc') ->paginate(40) diff --git a/app/Http/Controllers/MaleNameController.php b/app/Http/Controllers/MaleNameController.php index 63e0664..d0ab25f 100644 --- a/app/Http/Controllers/MaleNameController.php +++ b/app/Http/Controllers/MaleNameController.php @@ -21,8 +21,8 @@ public function index(Request $request): View $letters = Cache::remember('all-letters-male', 604800, fn () => MaleNamesViewModel::index()); - $namesPagination = Cache::remember('all-names-male-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES') - ->where('gender', 'male') + $namesPagination = Cache::remember('all-names-male-page-' . $requestedPage, 604800, fn () => Name::nonRares() + ->male() ->orderBy('total', 'desc') ->paginate(40) ); @@ -49,8 +49,8 @@ public function letter(Request $request): View $letters = Cache::remember('all-letters-male', 604800, fn () => MaleNamesViewModel::index()); - $namesPagination = Cache::remember('male-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES') - ->where('gender', 'male') + $namesPagination = Cache::remember('male-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::nonRares() + ->male() ->where('name', 'like', Str::upper($requestedLetter) . '%') ->orderBy('total', 'desc') ->paginate(40) diff --git a/app/Http/Controllers/MixteNameController.php b/app/Http/Controllers/MixteNameController.php index 192830d..9580aa9 100644 --- a/app/Http/Controllers/MixteNameController.php +++ b/app/Http/Controllers/MixteNameController.php @@ -21,8 +21,8 @@ public function index(Request $request): View $letters = Cache::remember('all-letters-mixte', 604800, fn () => MixteNamesViewModel::index()); - $namesPagination = Cache::remember('all-names-mixte-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES') - ->where('unisex', 1) + $namesPagination = Cache::remember('all-names-mixte-page-' . $requestedPage, 604800, fn () => Name::nonRares() + ->unisex() ->orderBy('total', 'desc') ->paginate(40) ); @@ -49,8 +49,8 @@ public function letter(Request $request): View $letters = Cache::remember('all-letters-mixte', 604800, fn () => MixteNamesViewModel::index()); - $namesPagination = Cache::remember('mixte-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES') - ->where('unisex', 1) + $namesPagination = Cache::remember('mixte-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::nonRares() + ->unisex() ->where('name', 'like', Str::upper($requestedLetter) . '%') ->orderBy('total', 'desc') ->paginate(40) diff --git a/app/Http/Controllers/NameController.php b/app/Http/Controllers/NameController.php index ee461b6..579fb0c 100644 --- a/app/Http/Controllers/NameController.php +++ b/app/Http/Controllers/NameController.php @@ -24,7 +24,7 @@ public function index(Request $request): View $letters = Cache::remember('all-letters', 604800, fn () => AllNamesViewModel::index()); - $namesPagination = Cache::remember('all-names-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES') + $namesPagination = Cache::remember('all-names-page-' . $requestedPage, 604800, fn () => Name::nonRares() ->orderBy('total', 'desc') ->paginate(40) ); @@ -95,7 +95,7 @@ public function letter(Request $request): View $letters = Cache::remember('all-letters', 604800, fn () => AllNamesViewModel::index()); - $namesPagination = Cache::remember('letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES') + $namesPagination = Cache::remember('letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::nonRares() ->where('name', 'like', Str::upper($requestedLetter) . '%') ->orderBy('total', 'desc') ->paginate(40) diff --git a/app/Http/ViewModels/Home/HomeViewModel.php b/app/Http/ViewModels/Home/HomeViewModel.php index e44c4e4..f247d9d 100644 --- a/app/Http/ViewModels/Home/HomeViewModel.php +++ b/app/Http/ViewModels/Home/HomeViewModel.php @@ -15,29 +15,29 @@ class HomeViewModel { public static function twentyMostPopularNames(): array { - $maleNames = Name::where('gender', 'male') - ->where('name', '!=', '_PRENOMS_RARES') + $maleNames = Name::male() + ->nonRares() ->orderBy('total', 'desc') ->take(10) ->get() ->map(fn (Name $name) => NameViewModel::summary($name)); - $femaleNames = Name::where('gender', 'female') - ->where('name', '!=', '_PRENOMS_RARES') + $femaleNames = Name::female() + ->nonRares() ->orderBy('total', 'desc') ->take(10) ->get() ->map(fn (Name $name) => NameViewModel::summary($name)); - $mixtedNames = Name::where('unisex', 1) - ->where('name', '!=', '_PRENOMS_RARES') + $mixtedNames = Name::unisex() + ->nonRares() ->orderBy('total', 'desc') ->take(10) ->get() ->map(fn (Name $name) => NameViewModel::summary($name)); $randomIds = Name::select('id') - ->where('name', '!=', '_PRENOMS_RARES') + ->nonRares() ->inRandomOrder() ->take(10) ->get(); @@ -79,7 +79,7 @@ public static function nameSpotlight(): array public static function serverStats(): array { - $totalNames = Name::where('name', '!=', '_PRENOMS_RARES')->count(); + $totalNames = Name::nonRares()->count(); return [ 'total_names' => Number::format($totalNames), diff --git a/app/Http/ViewModels/Names/AllNamesViewModel.php b/app/Http/ViewModels/Names/AllNamesViewModel.php index 5d7c233..5e5c818 100644 --- a/app/Http/ViewModels/Names/AllNamesViewModel.php +++ b/app/Http/ViewModels/Names/AllNamesViewModel.php @@ -12,7 +12,7 @@ class AllNamesViewModel public static function index(): Collection { $letters = collect(); - $allNames = Name::where('name', '!=', '_PRENOMS_RARES')->count(); + $allNames = Name::nonRares()->count(); $letters->push([ 'letter' => 'Tous', 'count' => Number::format($allNames), diff --git a/app/Http/ViewModels/Names/FemaleNamesViewModel.php b/app/Http/ViewModels/Names/FemaleNamesViewModel.php index af2ad08..1764c36 100644 --- a/app/Http/ViewModels/Names/FemaleNamesViewModel.php +++ b/app/Http/ViewModels/Names/FemaleNamesViewModel.php @@ -14,8 +14,8 @@ public static function index(): Collection // iterate over the alphabet $alphabet = range('A', 'Z'); - $total = Name::where('gender', 'female') - ->where('name', '!=', '_PRENOMS_RARES')->count(); + $total = Name::female() + ->nonRares()->count(); $letters = collect(); $letters->push([ @@ -25,7 +25,7 @@ public static function index(): Collection ]); foreach ($alphabet as $letter) { - $total = Name::where('gender', 'female') + $total = Name::female() ->where('name', 'like', $letter . '%')->count(); $letters->push([ 'letter' => $letter, diff --git a/app/Http/ViewModels/Names/MaleNamesViewModel.php b/app/Http/ViewModels/Names/MaleNamesViewModel.php index 27cb799..4055676 100644 --- a/app/Http/ViewModels/Names/MaleNamesViewModel.php +++ b/app/Http/ViewModels/Names/MaleNamesViewModel.php @@ -14,8 +14,8 @@ public static function index(): Collection // iterate over the alphabet $alphabet = range('A', 'Z'); - $total = Name::where('gender', 'male') - ->where('name', '!=', '_PRENOMS_RARES')->count(); + $total = Name::male() + ->nonRares()->count(); $letters = collect(); $letters->push([ @@ -25,7 +25,7 @@ public static function index(): Collection ]); foreach ($alphabet as $letter) { - $total = Name::where('gender', 'male') + $total = Name::male() ->where('name', 'like', $letter . '%')->count(); $letters->push([ 'letter' => $letter, diff --git a/app/Http/ViewModels/Names/MixteNamesViewModel.php b/app/Http/ViewModels/Names/MixteNamesViewModel.php index d85e829..e903d04 100644 --- a/app/Http/ViewModels/Names/MixteNamesViewModel.php +++ b/app/Http/ViewModels/Names/MixteNamesViewModel.php @@ -14,8 +14,8 @@ public static function index(): Collection // iterate over the alphabet $alphabet = range('A', 'Z'); - $total = Name::where('unisex', 1) - ->where('name', '!=', '_PRENOMS_RARES')->count(); + $total = Name::unisex() + ->nonRares()->count(); $letters = collect(); $letters->push([ @@ -25,7 +25,7 @@ public static function index(): Collection ]); foreach ($alphabet as $letter) { - $total = Name::where('unisex', 1) + $total = Name::unisex() ->where('name', 'like', $letter . '%')->count(); $letters->push([ 'letter' => $letter, diff --git a/app/Http/ViewModels/Names/NameViewModel.php b/app/Http/ViewModels/Names/NameViewModel.php index b4a9d59..ccf7251 100644 --- a/app/Http/ViewModels/Names/NameViewModel.php +++ b/app/Http/ViewModels/Names/NameViewModel.php @@ -129,7 +129,7 @@ public static function jsonLdSchema(Name $name): array public static function relatedNames(Name $name): Collection { $ids = Name::select('id') - ->where('name', '!=', '_PRENOMS_RARES') + ->nonRares() ->where('id', '!=', $name->id) ->where('gender', $name->gender) ->inRandomOrder() diff --git a/app/Models/Name.php b/app/Models/Name.php index 905b6de..312d55c 100644 --- a/app/Models/Name.php +++ b/app/Models/Name.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\BelongsToMany; @@ -97,4 +98,36 @@ public function getNoteForUser(): ?string return $note->content; } + + /** + * Scope a query to only include non rares names. + */ + public function scopeNonRares(Builder $query): void + { + $query->where('name', '!=', '_PRENOMS_RARES'); + } + + /** + * Scope a query to only include male names. + */ + public function scopeMale(Builder $query): void + { + $query->where('gender', 'male'); + } + + /** + * Scope a query to only include female names. + */ + public function scopeFemale(Builder $query): void + { + $query->where('gender', 'female'); + } + + /** + * Scope a query to only include unisex names. + */ + public function scopeUnisex(Builder $query): void + { + $query->where('unisex', 1); + } }