Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use scopes #70

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions app/Console/Commands/FetchMetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/GenerateSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/FemaleNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/MaleNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/MixteNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/NameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions app/Http/ViewModels/Home/HomeViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion app/Http/ViewModels/Names/AllNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions app/Http/ViewModels/Names/FemaleNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions app/Http/ViewModels/Names/MaleNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions app/Http/ViewModels/Names/MixteNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/ViewModels/Names/NameViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
33 changes: 33 additions & 0 deletions app/Models/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Loading