Skip to content

Commit

Permalink
refactor: refactor controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Feb 13, 2024
1 parent ed80448 commit 1f24299
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 226 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/Auth/NewPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class NewPasswordController extends Controller
{
use PasswordValidationRules;

/**
* Display the password reset view.
*/
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Auth/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

class PasswordController extends Controller
{
use PasswordValidationRules;

/**
* Update the user's password.
*/
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([
Expand Down
18 changes: 18 additions & 0 deletions app/Http/Controllers/Auth/PasswordValidationRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Validation\Rules\Password;

trait PasswordValidationRules
{
/**
* Get the validation rules used to validate passwords.
*
* @return array<int, \Illuminate\Contracts\Validation\Rule|array|string>
*/
protected function passwordRules(): array
{
return ['required', 'string', Password::default(), 'confirmed'];
}
}
13 changes: 11 additions & 2 deletions app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/FavoriteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
52 changes: 18 additions & 34 deletions app/Http/Controllers/FemaleNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
22 changes: 6 additions & 16 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Controllers/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/ListNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/ListSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down
52 changes: 18 additions & 34 deletions app/Http/Controllers/MaleNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 1f24299

Please sign in to comment.