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: some refactors #30

Merged
merged 4 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 11 additions & 12 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ class Kernel extends HttpKernel
* @var array<int, class-string|string>
*/
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,
];

Expand All @@ -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,
],

Expand All @@ -53,19 +52,19 @@ class Kernel extends HttpKernel
* @var array<string, class-string|string>
*/
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,
];
}
2 changes: 1 addition & 1 deletion app/Http/ViewModels/Home/HomeViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,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),
];
}

Expand Down
4 changes: 2 additions & 2 deletions app/Http/ViewModels/Names/AllNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]);

Expand All @@ -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)]),
]);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/ViewModels/Names/FemaleNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]);

Expand All @@ -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),
]),
Expand Down
4 changes: 2 additions & 2 deletions app/Http/ViewModels/Names/MaleNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]);

Expand All @@ -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),
]),
Expand Down
4 changes: 2 additions & 2 deletions app/Http/ViewModels/Names/MixteNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]);

Expand All @@ -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),
]),
Expand Down
10 changes: 2 additions & 8 deletions app/Http/ViewModels/Names/NameViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
});
Expand All @@ -98,7 +92,7 @@ public static function popularity(Name $name): array

return [
'decades' => $decadesCollection,
'total' => Number::format($total, locale: 'fr'),
'total' => Number::format($total),
];
}

Expand Down
4 changes: 1 addition & 3 deletions app/Http/ViewModels/Search/SearchViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions app/Http/ViewModels/User/ListViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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,
Expand All @@ -44,7 +44,7 @@ public static function show(NameList $list): array
'id' => $name->id,
'name' => StringHelper::formatNameFromDB($name->name),
'origins' => Str::words($name->origins, 50, '...'),
'total' => Number::format($name->total, locale: 'fr'),
'total' => Number::format($name->total),
'url' => [
'show' => route('name.show', [
'id' => $name->id,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/ViewModels/User/UserViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down
6 changes: 6 additions & 0 deletions app/Models/NameList.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\BelongsTo;
Expand Down Expand Up @@ -42,4 +43,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);
}
}
4 changes: 3 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Number;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -19,6 +21,6 @@ public function register(): void
*/
public function boot(): void
{
//
Number::useLocale(App::currentLocale());
}
}
4 changes: 2 additions & 2 deletions app/Services/ToggleNameToFavorites.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
5 changes: 2 additions & 3 deletions app/Services/ToggleNameToNameList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: it must be First or Fail, not find or fail. We need to get the first one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly the same actually ...


$list->names()->toggle([$this->nameId]);
}
Expand Down
Loading