From 2f60cbd6427e727f1903dbcb5edd30439215d314 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Sat, 17 Feb 2024 12:05:12 +0100 Subject: [PATCH] refactor: refactor controllers (#33) --- .../Auth/NewPasswordController.php | 4 +- .../Controllers/Auth/PasswordController.php | 4 +- .../Auth/PasswordValidationRules.php | 18 +++++ .../Auth/RegisteredUserController.php | 13 +++- app/Http/Controllers/FavoriteController.php | 4 +- app/Http/Controllers/FemaleNameController.php | 52 +++++-------- app/Http/Controllers/HomeController.php | 22 ++---- app/Http/Controllers/ListController.php | 8 +- app/Http/Controllers/ListNameController.php | 4 +- app/Http/Controllers/ListSearchController.php | 4 +- app/Http/Controllers/MaleNameController.php | 52 +++++-------- app/Http/Controllers/MixteNameController.php | 52 +++++-------- app/Http/Controllers/NameController.php | 74 ++++++------------- app/Http/Controllers/PublicListController.php | 17 +---- app/Http/Controllers/SearchController.php | 24 ++---- app/Http/Controllers/TermsController.php | 8 -- 16 files changed, 134 insertions(+), 226 deletions(-) create mode 100644 app/Http/Controllers/Auth/PasswordValidationRules.php diff --git a/app/Http/Controllers/Auth/NewPasswordController.php b/app/Http/Controllers/Auth/NewPasswordController.php index b632291..f2ec8c2 100644 --- a/app/Http/Controllers/Auth/NewPasswordController.php +++ b/app/Http/Controllers/Auth/NewPasswordController.php @@ -14,6 +14,8 @@ class NewPasswordController extends Controller { + use PasswordValidationRules; + /** * Display the password reset view. */ @@ -32,7 +34,7 @@ public function store(Request $request): RedirectResponse $request->validate([ 'token' => ['required'], 'email' => ['required', 'email'], - 'password' => ['required', 'confirmed', Rules\Password::defaults()], + 'password' => $this->passwordRules(), ]); // Here we will attempt to reset the user's password. If it is successful we diff --git a/app/Http/Controllers/Auth/PasswordController.php b/app/Http/Controllers/Auth/PasswordController.php index 6916409..b106c21 100644 --- a/app/Http/Controllers/Auth/PasswordController.php +++ b/app/Http/Controllers/Auth/PasswordController.php @@ -10,6 +10,8 @@ class PasswordController extends Controller { + use PasswordValidationRules; + /** * Update the user's password. */ @@ -17,7 +19,7 @@ public function update(Request $request): RedirectResponse { $validated = $request->validateWithBag('updatePassword', [ 'current_password' => ['required', 'current_password'], - 'password' => ['required', Password::defaults(), 'confirmed'], + 'password' => $this->passwordRules(), ]); $request->user()->update([ diff --git a/app/Http/Controllers/Auth/PasswordValidationRules.php b/app/Http/Controllers/Auth/PasswordValidationRules.php new file mode 100644 index 0000000..1444d70 --- /dev/null +++ b/app/Http/Controllers/Auth/PasswordValidationRules.php @@ -0,0 +1,18 @@ + + */ + protected function passwordRules(): array + { + return ['required', 'string', Password::default(), 'confirmed']; + } +} diff --git a/app/Http/Controllers/Auth/RegisteredUserController.php b/app/Http/Controllers/Auth/RegisteredUserController.php index 95ab112..a5238d9 100644 --- a/app/Http/Controllers/Auth/RegisteredUserController.php +++ b/app/Http/Controllers/Auth/RegisteredUserController.php @@ -10,11 +10,14 @@ use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use Illuminate\Validation\Rule; use Illuminate\Validation\Rules; use Illuminate\View\View; class RegisteredUserController extends Controller { + use PasswordValidationRules; + /** * Display the registration view. */ @@ -31,8 +34,14 @@ public function create(): View public function store(Request $request): RedirectResponse { $request->validate([ - 'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:' . User::class], - 'password' => ['required', 'confirmed', Rules\Password::defaults()], + 'email' => [ + 'required', + 'string', + 'email', + 'max:255', + Rule::unique(User::class) + ], + 'password' => $this->passwordRules(), ]); $user = (new CreateAccount( diff --git a/app/Http/Controllers/FavoriteController.php b/app/Http/Controllers/FavoriteController.php index 1e7a001..554a292 100644 --- a/app/Http/Controllers/FavoriteController.php +++ b/app/Http/Controllers/FavoriteController.php @@ -13,9 +13,7 @@ class FavoriteController extends Controller { public function index(): View { - $favorites = Cache::remember('user-favorites-details-' . auth()->id(), 604800, function () { - return UserViewModel::index(); - }); + $favorites = Cache::remember('user-favorites-details-' . auth()->id(), 604800, fn () => UserViewModel::index()); return view('user.index', [ 'names' => $favorites['names'], diff --git a/app/Http/Controllers/FemaleNameController.php b/app/Http/Controllers/FemaleNameController.php index 488b281..6924a36 100644 --- a/app/Http/Controllers/FemaleNameController.php +++ b/app/Http/Controllers/FemaleNameController.php @@ -19,31 +19,23 @@ 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(); - }); + $letters = Cache::remember('all-letters-female', 604800, fn () => FemaleNamesViewModel::index()); - Paginator::currentPageResolver(function () use ($requestedPage) { - return $requestedPage; - }); + Paginator::currentPageResolver(fn () => $requestedPage); - $namesPagination = Cache::remember('all-names-female-page-' . $requestedPage, 604800, function () { - return Name::where('name', '!=', '_PRENOMS_RARES') + $namesPagination = Cache::remember('all-names-female-page-' . $requestedPage, 604800, fn () => + Name::where('name', '!=', '_PRENOMS_RARES') ->where('gender', 'female') ->orderBy('total', 'desc') - ->paginate(40); - }); + ->paginate(40) + ); $names = $namesPagination ->map(fn (Name $name) => NameViewModel::summary($name)); - if (! auth()->check()) { - $favoritedNamesForLoggedUser = collect(); - } else { - $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () { - return UserViewModel::favorites(); - }); - } + $favoritedNamesForLoggedUser = auth()->check() + ? Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites()) + : collect(); return view('names.female.index', [ 'letters' => $letters, @@ -58,32 +50,24 @@ 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(); - }); + $letters = Cache::remember('all-letters-female', 604800, fn () => FemaleNamesViewModel::index()); - Paginator::currentPageResolver(function () use ($requestedPage) { - return $requestedPage; - }); + Paginator::currentPageResolver(fn () => $requestedPage); - $namesPagination = Cache::remember('female-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, function () use ($requestedLetter) { - return Name::where('name', '!=', '_PRENOMS_RARES') + $namesPagination = Cache::remember('female-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => + Name::where('name', '!=', '_PRENOMS_RARES') ->where('gender', 'female') ->where('name', 'like', $requestedLetter . '%') ->orderBy('total', 'desc') - ->paginate(40); - }); + ->paginate(40) + ); $names = $namesPagination ->map(fn (Name $name) => NameViewModel::summary($name)); - if (! auth()->check()) { - $favoritedNamesForLoggedUser = collect(); - } else { - $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () { - return UserViewModel::favorites(); - }); - } + $favoritedNamesForLoggedUser = auth()->check() + ? Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites()) + : collect(); return view('names.female.letter', [ 'letters' => $letters, diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 9c64923..e43e8a8 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -11,25 +11,15 @@ class HomeController extends Controller { public function index(): View { - $popularNames = Cache::remember('popular-names', 86400, function () { - return HomeViewModel::twentyMostPopularNames(); - }); + $popularNames = Cache::remember('popular-names', 86400, fn () => HomeViewModel::twentyMostPopularNames()); - $stats = Cache::remember('stats', 604800, function () { - return HomeViewModel::serverStats(); - }); + $stats = Cache::remember('stats', 604800, fn () => HomeViewModel::serverStats()); - if (! auth()->check()) { - $favoritedNamesForLoggedUser = collect(); - } else { - $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () { - return UserViewModel::favorites(); - }); - } + $favoritedNamesForLoggedUser = auth()->check() + ? Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites()) + : collect(); - $lists = Cache::remember('admin-lists', 604800, function () { - return HomeViewModel::adminLists(); - }); + $lists = Cache::remember('admin-lists', 604800, fn () => HomeViewModel::adminLists()); return view('home.index', [ 'twentyMostPopularNames' => $popularNames, diff --git a/app/Http/Controllers/ListController.php b/app/Http/Controllers/ListController.php index ad7798a..5c94ba1 100644 --- a/app/Http/Controllers/ListController.php +++ b/app/Http/Controllers/ListController.php @@ -14,9 +14,7 @@ class ListController extends Controller { public function index(): View { - $lists = Cache::remember('user-lists-' . auth()->id(), 604800, function () { - return ListViewModel::index(); - }); + $lists = Cache::remember('user-lists-' . auth()->id(), 604800, fn () => ListViewModel::index()); return view('user.lists.index', [ 'lists' => $lists['lists'], @@ -44,9 +42,7 @@ public function show(Request $request): View { $requestedList = $request->attributes->get('list'); - $details = Cache::remember('list-details-' . $requestedList->id, 604800, function () use ($requestedList) { - return ListViewModel::show($requestedList); - }); + $details = Cache::remember('list-details-' . $requestedList->id, 604800, fn () => ListViewModel::show($requestedList)); return view('user.lists.show', [ 'term' => null, diff --git a/app/Http/Controllers/ListNameController.php b/app/Http/Controllers/ListNameController.php index 690e8f7..0066b04 100644 --- a/app/Http/Controllers/ListNameController.php +++ b/app/Http/Controllers/ListNameController.php @@ -16,9 +16,7 @@ public function index(Request $request): View { $requestedList = $request->attributes->get('list'); - $details = Cache::remember('list-details-' . $requestedList->id, 604800, function () use ($requestedList) { - return ListViewModel::show($requestedList); - }); + $details = Cache::remember('list-details-' . $requestedList->id, 604800, fn () => ListViewModel::show($requestedList)); return view('user.lists.show', [ 'list' => $details, diff --git a/app/Http/Controllers/ListSearchController.php b/app/Http/Controllers/ListSearchController.php index 4a4fe99..b0e378b 100644 --- a/app/Http/Controllers/ListSearchController.php +++ b/app/Http/Controllers/ListSearchController.php @@ -14,9 +14,7 @@ public function index(Request $request): View { $requestedList = $request->attributes->get('list'); - $details = Cache::remember('list-details-' . $requestedList->id, 604800, function () use ($requestedList) { - return ListViewModel::show($requestedList); - }); + $details = Cache::remember('list-details-' . $requestedList->id, 604800, fn () => ListViewModel::show($requestedList)); $names = SearchViewModel::names($request->input('term')); diff --git a/app/Http/Controllers/MaleNameController.php b/app/Http/Controllers/MaleNameController.php index c6fcc83..567e008 100644 --- a/app/Http/Controllers/MaleNameController.php +++ b/app/Http/Controllers/MaleNameController.php @@ -19,31 +19,23 @@ 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(); - }); + $letters = Cache::remember('all-letters-male', 604800, fn () => MaleNamesViewModel::index()); - Paginator::currentPageResolver(function () use ($requestedPage) { - return $requestedPage; - }); + Paginator::currentPageResolver(fn () => $requestedPage); - $namesPagination = Cache::remember('all-names-male-page-' . $requestedPage, 604800, function () { - return Name::where('name', '!=', '_PRENOMS_RARES') + $namesPagination = Cache::remember('all-names-male-page-' . $requestedPage, 604800, fn () => + Name::where('name', '!=', '_PRENOMS_RARES') ->where('gender', 'male') ->orderBy('total', 'desc') - ->paginate(40); - }); + ->paginate(40) + ); $names = $namesPagination ->map(fn (Name $name) => NameViewModel::summary($name)); - if (! auth()->check()) { - $favoritedNamesForLoggedUser = collect(); - } else { - $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () { - return UserViewModel::favorites(); - }); - } + $favoritedNamesForLoggedUser = auth()->check() + ? Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites()) + : collect(); return view('names.male.index', [ 'letters' => $letters, @@ -58,32 +50,24 @@ 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(); - }); + $letters = Cache::remember('all-letters-male', 604800, fn () => MaleNamesViewModel::index()); - Paginator::currentPageResolver(function () use ($requestedPage) { - return $requestedPage; - }); + Paginator::currentPageResolver(fn () => $requestedPage); - $namesPagination = Cache::remember('male-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, function () use ($requestedLetter) { - return Name::where('name', '!=', '_PRENOMS_RARES') + $namesPagination = Cache::remember('male-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => + Name::where('name', '!=', '_PRENOMS_RARES') ->where('gender', 'male') ->where('name', 'like', $requestedLetter . '%') ->orderBy('total', 'desc') - ->paginate(40); - }); + ->paginate(40) + ); $names = $namesPagination ->map(fn (Name $name) => NameViewModel::summary($name)); - if (! auth()->check()) { - $favoritedNamesForLoggedUser = collect(); - } else { - $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () { - return UserViewModel::favorites(); - }); - } + $favoritedNamesForLoggedUser = auth()->check() + ? Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites()) + : collect(); return view('names.male.letter', [ 'letters' => $letters, diff --git a/app/Http/Controllers/MixteNameController.php b/app/Http/Controllers/MixteNameController.php index b0b5e8e..59d138d 100644 --- a/app/Http/Controllers/MixteNameController.php +++ b/app/Http/Controllers/MixteNameController.php @@ -19,31 +19,23 @@ public function index(Request $request): View // get the page parameter from the url $requestedPage = $request->query('page') ?? 1; - $letters = Cache::remember('all-letters-mixte', 604800, function () { - return MixteNamesViewModel::index(); - }); + $letters = Cache::remember('all-letters-mixte', 604800, fn () => MixteNamesViewModel::index()); - Paginator::currentPageResolver(function () use ($requestedPage) { - return $requestedPage; - }); + Paginator::currentPageResolver(fn () => $requestedPage); - $namesPagination = Cache::remember('all-names-mixte-page-' . $requestedPage, 604800, function () { - return Name::where('name', '!=', '_PRENOMS_RARES') + $namesPagination = Cache::remember('all-names-mixte-page-' . $requestedPage, 604800, fn () => + Name::where('name', '!=', '_PRENOMS_RARES') ->where('unisex', 1) ->orderBy('total', 'desc') - ->paginate(40); - }); + ->paginate(40) + ); $names = $namesPagination ->map(fn (Name $name) => NameViewModel::summary($name)); - if (! auth()->check()) { - $favoritedNamesForLoggedUser = collect(); - } else { - $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () { - return UserViewModel::favorites(); - }); - } + $favoritedNamesForLoggedUser = auth()->check() + ? Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites()) + : collect(); return view('names.mixte.index', [ 'letters' => $letters, @@ -58,32 +50,24 @@ public function letter(Request $request): View $requestedLetter = $request->attributes->get('letter'); $requestedPage = $request->query('page') ?? 1; - $letters = Cache::remember('all-letters-mixte', 604800, function () { - return MixteNamesViewModel::index(); - }); + $letters = Cache::remember('all-letters-mixte', 604800, fn () => MixteNamesViewModel::index()); - Paginator::currentPageResolver(function () use ($requestedPage) { - return $requestedPage; - }); + Paginator::currentPageResolver(fn () => $requestedPage); - $namesPagination = Cache::remember('mixte-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, function () use ($requestedLetter) { - return Name::where('name', '!=', '_PRENOMS_RARES') + $namesPagination = Cache::remember('mixte-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => + Name::where('name', '!=', '_PRENOMS_RARES') ->where('unisex', 1) ->where('name', 'like', $requestedLetter . '%') ->orderBy('total', 'desc') - ->paginate(40); - }); + ->paginate(40) + ); $names = $namesPagination ->map(fn (Name $name) => NameViewModel::summary($name)); - if (! auth()->check()) { - $favoritedNamesForLoggedUser = collect(); - } else { - $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () { - return UserViewModel::favorites(); - }); - } + $favoritedNamesForLoggedUser = auth()->check() + ? Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites()) + : collect(); return view('names.mixte.letter', [ 'letters' => $letters, diff --git a/app/Http/Controllers/NameController.php b/app/Http/Controllers/NameController.php index 299be2d..196688c 100644 --- a/app/Http/Controllers/NameController.php +++ b/app/Http/Controllers/NameController.php @@ -21,30 +21,22 @@ 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(); - }); + $letters = Cache::remember('all-letters', 604800, fn () => AllNamesViewModel::index()); - Paginator::currentPageResolver(function () use ($requestedPage) { - return $requestedPage; - }); + Paginator::currentPageResolver(fn () => $requestedPage); - $namesPagination = Cache::remember('all-names-page-' . $requestedPage, 604800, function () { - return Name::where('name', '!=', '_PRENOMS_RARES') + $namesPagination = Cache::remember('all-names-page-' . $requestedPage, 604800, fn () => + Name::where('name', '!=', '_PRENOMS_RARES') ->orderBy('total', 'desc') - ->paginate(40); - }); + ->paginate(40) + ); $names = $namesPagination ->map(fn (Name $name) => NameViewModel::summary($name)); - if (! auth()->check()) { - $favoritedNamesForLoggedUser = collect(); - } else { - $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () { - return UserViewModel::favorites(); - }); - } + $favoritedNamesForLoggedUser = auth()->check() + ? Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites()) + : collect(); return view('names.index', [ 'letters' => $letters, @@ -58,33 +50,21 @@ public function show(Request $request): View { $requestedName = $request->attributes->get('name'); - $name = Cache::remember('name-' . $requestedName->name, 604800, function () use ($requestedName) { - return NameViewModel::details($requestedName); - }); + $name = Cache::remember('name-' . $requestedName->name, 604800, fn () => NameViewModel::details($requestedName)); - $relatedNames = Cache::remember('related-names-' . $requestedName->name, 60, function () use ($requestedName) { - return NameViewModel::relatedNames($requestedName); - }); + $relatedNames = Cache::remember('related-names-' . $requestedName->name, 60, fn () => NameViewModel::relatedNames($requestedName)); - $popularity = Cache::remember('popularity-' . $requestedName->name, 604800, function () use ($requestedName) { - return NameViewModel::popularity($requestedName); - }); + $popularity = Cache::remember('popularity-' . $requestedName->name, 604800, fn () => NameViewModel::popularity($requestedName)); - $numerology = Cache::remember('numerology-' . $requestedName->name, 604800, function () use ($requestedName) { - return NameViewModel::numerology($requestedName); - }); + $numerology = Cache::remember('numerology-' . $requestedName->name, 604800, fn () => NameViewModel::numerology($requestedName)); if (! auth()->check()) { $favoritedNamesForLoggedUser = collect(); $lists = []; $note = ''; } else { - $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () { - return UserViewModel::favorites(); - }); - + $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites()); $lists = ListViewModel::lists($requestedName); - $note = $requestedName->getNoteForUser(); } @@ -113,31 +93,23 @@ 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(); - }); + $letters = Cache::remember('all-letters', 604800, fn () => AllNamesViewModel::index()); - Paginator::currentPageResolver(function () use ($requestedPage) { - return $requestedPage; - }); + Paginator::currentPageResolver(fn () => $requestedPage); - $namesPagination = Cache::remember('letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, function () use ($requestedLetter) { - return Name::where('name', '!=', '_PRENOMS_RARES') + $namesPagination = Cache::remember('letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => + Name::where('name', '!=', '_PRENOMS_RARES') ->where('name', 'like', $requestedLetter . '%') ->orderBy('total', 'desc') - ->paginate(40); - }); + ->paginate(40) + ); $names = $namesPagination ->map(fn (Name $name) => NameViewModel::summary($name)); - if (! auth()->check()) { - $favoritedNamesForLoggedUser = collect(); - } else { - $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () { - return UserViewModel::favorites(); - }); - } + $favoritedNamesForLoggedUser = auth()->check() + ? Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites()) + : collect(); return view('names.letter', [ 'letters' => $letters, diff --git a/app/Http/Controllers/PublicListController.php b/app/Http/Controllers/PublicListController.php index a446d44..c5ebc80 100644 --- a/app/Http/Controllers/PublicListController.php +++ b/app/Http/Controllers/PublicListController.php @@ -4,11 +4,8 @@ use App\Http\ViewModels\User\ListViewModel; use App\Http\ViewModels\User\UserViewModel; -use App\Services\CreateList; -use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\Redirect; use Illuminate\View\View; class PublicListController extends Controller @@ -17,17 +14,11 @@ public function show(Request $request): View { $requestedList = $request->attributes->get('list'); - $details = Cache::remember('list-details-' . $requestedList->id, 604800, function () use ($requestedList) { - return ListViewModel::show($requestedList); - }); + $details = Cache::remember('list-details-' . $requestedList->id, 604800, fn () => ListViewModel::show($requestedList)); - if (! auth()->check()) { - $favoritedNamesForLoggedUser = collect(); - } else { - $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () { - return UserViewModel::favorites(); - }); - } + $favoritedNamesForLoggedUser = auth()->check() + ? Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites()) + : collect(); return view('user.lists.public.show', [ 'list' => $details, diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index b7c66f5..5848914 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -13,9 +13,7 @@ class SearchController extends Controller { public function index(): View { - $stats = Cache::remember('stats', 604800, function () { - return HomeViewModel::serverStats(); - }); + $stats = Cache::remember('stats', 604800, fn () => HomeViewModel::serverStats()); return view('search.index', [ 'stats' => $stats, @@ -27,23 +25,15 @@ public function index(): View public function post(Request $request): View { - $stats = Cache::remember('stats', 604800, function () { - return HomeViewModel::serverStats(); - }); + $stats = Cache::remember('stats', 604800, fn () => HomeViewModel::serverStats()); $term = trim($request->input('term')); - $names = Cache::remember('search-name-' . $term, 604800, function () use ($term) { - return SearchViewModel::names($term, 1000); - }); - - if (! auth()->check()) { - $favoritedNamesForLoggedUser = collect(); - } else { - $favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () { - return UserViewModel::favorites(); - }); - } + $names = Cache::remember('search-name-' . $term, 604800, fn () => SearchViewModel::names($term, 1000)); + + $favoritedNamesForLoggedUser = auth()->check() + ? Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites()) + : collect(); return view('search.index', [ 'stats' => $stats, diff --git a/app/Http/Controllers/TermsController.php b/app/Http/Controllers/TermsController.php index 07ed92b..8908bf6 100644 --- a/app/Http/Controllers/TermsController.php +++ b/app/Http/Controllers/TermsController.php @@ -2,14 +2,6 @@ namespace App\Http\Controllers; -use App\Http\ViewModels\Names\MaleNamesViewModel; -use App\Http\ViewModels\Names\NameViewModel; -use App\Http\ViewModels\User\UserViewModel; -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 TermsController extends Controller