-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,294 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Helpers\StringHelper; | ||
use App\Http\ViewModels\Names\AllNamesViewModel; | ||
use App\Http\ViewModels\Names\FemaleNamesViewModel; | ||
use App\Http\ViewModels\Names\MaleNamesViewModel; | ||
use App\Http\ViewModels\Names\NameViewModel; | ||
use App\Models\Name; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Pagination\Paginator; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Str; | ||
use Illuminate\View\View; | ||
|
||
class FemaleNameController extends Controller | ||
{ | ||
public function index(Request $request): View | ||
{ | ||
// get the page parameter from the url | ||
$requestedPage = $request->query('page') ?? 1; | ||
|
||
$letters = Cache::remember('all-letters-female', 604800, function () { | ||
return FemaleNamesViewModel::index(); | ||
}); | ||
|
||
Paginator::currentPageResolver(function () use ($requestedPage) { | ||
return $requestedPage; | ||
}); | ||
|
||
$namesPagination = Cache::remember('all-names-female-page-'.$requestedPage, 604800, function () { | ||
return Name::where('name', '!=', '_PRENOMS_RARES') | ||
->where('gender', 'female') | ||
->orderBy('name', 'asc') | ||
->paginate(40); | ||
}); | ||
|
||
$names = $namesPagination | ||
->map(fn (Name $name) => [ | ||
'id' => $name->id, | ||
'name' => StringHelper::getProperName($name->name), | ||
'avatar' => $name->avatar, | ||
'url' => route('name.show', [ | ||
'id' => $name->id, | ||
'name' => StringHelper::sanitizeNameForURL($name->name), | ||
]), | ||
]); | ||
|
||
return view('names.female.index', [ | ||
'letters' => $letters, | ||
'names' => $names, | ||
'namesPagination' => $namesPagination, | ||
]); | ||
} | ||
|
||
public function letter(Request $request): View | ||
{ | ||
$requestedLetter = $request->attributes->get('letter'); | ||
$requestedPage = $request->query('page') ?? 1; | ||
|
||
$letters = Cache::remember('all-letters-female', 604800, function () { | ||
return FemaleNamesViewModel::index(); | ||
}); | ||
|
||
Paginator::currentPageResolver(function () use ($requestedPage) { | ||
return $requestedPage; | ||
}); | ||
|
||
$namesPagination = Cache::remember('female-letter-'.$requestedLetter.'-page-' . $requestedPage, 604800, function () use ($requestedLetter) { | ||
return Name::where('name', '!=', '_PRENOMS_RARES') | ||
->where('gender', 'female') | ||
->where('name', 'like', $requestedLetter . '%') | ||
->orderBy('name', 'asc') | ||
->paginate(40); | ||
}); | ||
|
||
$names = $namesPagination | ||
->map(fn (Name $name) => [ | ||
'id' => $name->id, | ||
'name' => StringHelper::getProperName($name->name), | ||
'avatar' => $name->avatar, | ||
'url' => route('name.show', [ | ||
'id' => $name->id, | ||
'name' => StringHelper::sanitizeNameForURL($name->name), | ||
]), | ||
]); | ||
|
||
return view('names.female.letter', [ | ||
'letters' => $letters, | ||
'names' => $names, | ||
'namesPagination' => $namesPagination, | ||
'activeLetter' => Str::ucfirst($requestedLetter), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Helpers\StringHelper; | ||
use App\Http\ViewModels\Names\AllNamesViewModel; | ||
use App\Http\ViewModels\Names\MaleNamesViewModel; | ||
use App\Http\ViewModels\Names\NameViewModel; | ||
use App\Models\Name; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Pagination\Paginator; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Str; | ||
use Illuminate\View\View; | ||
|
||
class MaleNameController extends Controller | ||
{ | ||
public function index(Request $request): View | ||
{ | ||
// get the page parameter from the url | ||
$requestedPage = $request->query('page') ?? 1; | ||
|
||
$letters = Cache::remember('all-letters-male', 604800, function () { | ||
return MaleNamesViewModel::index(); | ||
}); | ||
|
||
Paginator::currentPageResolver(function () use ($requestedPage) { | ||
return $requestedPage; | ||
}); | ||
|
||
$namesPagination = Cache::remember('all-names-male-page-'.$requestedPage, 604800, function () { | ||
return Name::where('name', '!=', '_PRENOMS_RARES') | ||
->where('gender', 'male') | ||
->orderBy('name', 'asc') | ||
->paginate(40); | ||
}); | ||
|
||
$names = $namesPagination | ||
->map(fn (Name $name) => [ | ||
'id' => $name->id, | ||
'name' => StringHelper::getProperName($name->name), | ||
'avatar' => $name->avatar, | ||
'url' => route('name.show', [ | ||
'id' => $name->id, | ||
'name' => StringHelper::sanitizeNameForURL($name->name), | ||
]), | ||
]); | ||
|
||
return view('names.male.index', [ | ||
'letters' => $letters, | ||
'names' => $names, | ||
'namesPagination' => $namesPagination, | ||
]); | ||
} | ||
|
||
public function letter(Request $request): View | ||
{ | ||
$requestedLetter = $request->attributes->get('letter'); | ||
$requestedPage = $request->query('page') ?? 1; | ||
|
||
$letters = Cache::remember('all-letters-male', 604800, function () { | ||
return MaleNamesViewModel::index(); | ||
}); | ||
|
||
Paginator::currentPageResolver(function () use ($requestedPage) { | ||
return $requestedPage; | ||
}); | ||
|
||
$namesPagination = Cache::remember('male-letter-'.$requestedLetter.'-page-' . $requestedPage, 604800, function () use ($requestedLetter) { | ||
return Name::where('name', '!=', '_PRENOMS_RARES') | ||
->where('gender', 'male') | ||
->where('name', 'like', $requestedLetter . '%') | ||
->orderBy('name', 'asc') | ||
->paginate(40); | ||
}); | ||
|
||
$names = $namesPagination | ||
->map(fn (Name $name) => [ | ||
'id' => $name->id, | ||
'name' => StringHelper::getProperName($name->name), | ||
'avatar' => $name->avatar, | ||
'url' => route('name.show', [ | ||
'id' => $name->id, | ||
'name' => StringHelper::sanitizeNameForURL($name->name), | ||
]), | ||
]); | ||
|
||
return view('names.male.letter', [ | ||
'letters' => $letters, | ||
'names' => $names, | ||
'namesPagination' => $namesPagination, | ||
'activeLetter' => Str::ucfirst($requestedLetter), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use App\Models\Name; | ||
use Closure; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class CheckLetter | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next | ||
*/ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
$requestedLetter = $request->route()->parameter('letter'); | ||
|
||
// check if the requested letter is between A and Z and is exactly one character | ||
if (strlen($requestedLetter) > 1) { | ||
return redirect()->route('home.index'); | ||
} | ||
|
||
if (!preg_match('/^[A-Za-z]+$/', $requestedLetter)) { | ||
return redirect()->route('home.index'); | ||
} | ||
|
||
$request->attributes->add(['letter' => Str::lcfirst($requestedLetter)]); | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Http\ViewModels\Names; | ||
|
||
use App\Helpers\StringHelper; | ||
use App\Models\Name; | ||
use Carbon\Carbon; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Str; | ||
use Illuminate\Support\Number; | ||
|
||
class AllNamesViewModel | ||
{ | ||
public static function index(): Collection | ||
{ | ||
// iterate over the alphabet | ||
$alphabet = range('A', 'Z'); | ||
|
||
$letters = collect(); | ||
|
||
$letters->push([ | ||
'letter' => 'Tous', | ||
'count' => Number::format(Name::where('name', '!=', '_PRENOMS_RARES')->count(), locale: 'fr'), | ||
'url' => route('name.index'), | ||
]); | ||
|
||
foreach ($alphabet as $letter) { | ||
$letters->push([ | ||
'letter' => $letter, | ||
'count' => Number::format(Name::where('name', 'like', $letter . '%')->count(), locale: 'fr'), | ||
'url' => route('name.letter', ['letter' => Str::lcfirst($letter)]), | ||
]); | ||
} | ||
|
||
return $letters; | ||
} | ||
} |
Oops, something went wrong.