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

feat: all names #4

Merged
merged 1 commit into from
Dec 15, 2023
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
5 changes: 5 additions & 0 deletions app/Console/Commands/FetchMetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Jobs\ProcessElficTraits;
use App\Jobs\ProcessKlingonName;
use App\Jobs\ProcessLitteratureReferences;
use App\Jobs\ProcessMixte;
use App\Jobs\ProcessOrigins;
use App\Jobs\ProcessPersonality;
use App\Jobs\ProcessSimilarNames;
Expand Down Expand Up @@ -70,6 +71,10 @@ public function handle(): void
if (is_null($name->klingon_translation)) {
//ProcessKlingonName::dispatch($name);
}

if (is_null($name->unisex)) {
ProcessMixte::dispatch($name);
}
}
}
}
96 changes: 96 additions & 0 deletions app/Http/Controllers/FemaleNameController.php
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),
]);
}
}
95 changes: 95 additions & 0 deletions app/Http/Controllers/MaleNameController.php
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),
]);
}
}
81 changes: 81 additions & 0 deletions app/Http/Controllers/NameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,94 @@

namespace App\Http\Controllers;

use App\Helpers\StringHelper;
use App\Http\ViewModels\Names\AllNamesViewModel;
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 NameController 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', 604800, function () {
return AllNamesViewModel::index();
});

Paginator::currentPageResolver(function () use ($requestedPage) {
return $requestedPage;
});

$namesPagination = Cache::remember('all-names-page-'.$requestedPage, 604800, function () {
return Name::where('name', '!=', '_PRENOMS_RARES')
->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.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', 604800, function () {
return AllNamesViewModel::index();
});

Paginator::currentPageResolver(function () use ($requestedPage) {
return $requestedPage;
});

$namesPagination = Cache::remember('letter-'.$requestedLetter.'-page-' . $requestedPage, 604800, function () use ($requestedLetter) {
return Name::where('name', '!=', '_PRENOMS_RARES')
->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.letter', [
'letters' => $letters,
'names' => $names,
'namesPagination' => $namesPagination,
'activeLetter' => Str::ucfirst($requestedLetter),
]);
}

public function show(Request $request): View
{
$requestedName = $request->attributes->get('name');
Expand Down
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ class Kernel extends HttpKernel
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'administrator' => \App\Http\Middleware\CheckAdministratorRole::class,
'name' => \App\Http\Middleware\CheckName::class,
'letter' => \App\Http\Middleware\CheckLetter::class,
];
}
37 changes: 37 additions & 0 deletions app/Http/Middleware/CheckLetter.php
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);
}
}
2 changes: 1 addition & 1 deletion app/Http/ViewModels/Home/HomeViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static function nameSpotlight(): array

public static function serverStats(): array
{
$totalNames = Name::count();
$totalNames = Name::where('name', '!=', '_PRENOMS_RARES')->count();

return [
'total_names' => $totalNames,
Expand Down
37 changes: 37 additions & 0 deletions app/Http/ViewModels/Names/AllNamesViewModel.php
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;
}
}
Loading
Loading