This commit adds update functionality to user profiles. New routes, views, and controller methods have been created to facilitate this along with form requests for validation. Significant changes include new methods in the ProfileController, addition of an UpdateRequest class for validation purposes and the creation of a profile update view. These changes allow users to edit and update their profile information on the application.

This commit is contained in:
Leonid Nikitin 2023-07-07 00:07:19 +06:00
parent 55cd927f12
commit 00a0624eea
Signed by: kor-elf
GPG Key ID: 7DE8F80C5CEC2C0D
9 changed files with 162 additions and 7 deletions

View File

@ -0,0 +1,17 @@
<?php declare(strict_types=1);
namespace App\Dto\Request\Private\Profile;
use App\Dto\Request\Dto;
final readonly class Update extends Dto
{
public function __construct(
private string $name
) { }
public function getName(): string
{
return $this->name;
}
}

View File

@ -0,0 +1,13 @@
<?php declare(strict_types=1);
namespace App\Http\Controllers\Private;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

View File

@ -2,22 +2,41 @@
namespace App\Http\Controllers\Private; namespace App\Http\Controllers\Private;
use Illuminate\Support\Facades\Auth; use App\Http\Requests\Private\Profile\UpdateRequest;
use App\Services\Private\ProfileService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View; use Illuminate\View\View;
final class ProfileController extends Controller final class ProfileController extends Controller
{ {
public function profile(): View public function __construct(
private readonly ProfileService $profileService
) { }
public function profile(Request $request): View
{ {
return view('private/profile/profile', [ return view('private/profile/profile', [
'user' => Auth::user() 'user' => $request->user()
]); ]);
} }
public function settings(): View public function settings(Request $request): View
{ {
return view('private/profile/settings', [ return view('private/profile/settings', [
'user' => Auth::user() 'user' => $request->user()
]); ]);
} }
public function update(UpdateRequest $request): RedirectResponse
{
$data = $request->getDto();
$user = $request->user();
$result = $this->profileService->update($data, $user);
if ($result->isError()) {
return redirect()->back()->withInput()->withErrors($result->getMessage());
}
return redirect()->route('profile.edit')->withSuccess($result->getMessage());
}
} }

View File

@ -0,0 +1,25 @@
<?php declare(strict_types=1);
namespace App\Http\Requests\Private\Profile;
use App\Contracts\FormRequestDto;
use App\Dto\Request\Private\Profile\Update;
use Illuminate\Foundation\Http\FormRequest;
final class UpdateRequest extends FormRequest implements FormRequestDto
{
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'name' => ['required', 'max:255'],
];
}
public function getDto(): Update
{
return new Update(name: $this->input('name'));
}
}

View File

@ -0,0 +1,31 @@
<?php declare(strict_types=1);
namespace App\Services\Private;
use App\Dto\Request\Private\Profile\Update;
use App\Models\User;
use App\ServiceResults\ServiceResultError;
use App\ServiceResults\ServiceResultSuccess;
use App\Services\Service;
use App\Services\User\UserCommandHandler;
final class ProfileService extends Service
{
public function __construct(
private readonly UserCommandHandler $userCommandHandler
) { }
public function update(Update $update, User $user): ServiceResultError | ServiceResultSuccess
{
try {
$data = [
'name' => $update->getName()
];
$this->userCommandHandler->handleUpdate($user, $data);
} catch (\Throwable $e) {
report($e->getMessage());
return $this->errService($e->getMessage());
}
return $this->ok(__('Profile saved successfully'));
}
}

View File

@ -37,5 +37,12 @@
"Your Email": "Your Email", "Your Email": "Your Email",
"Your Password": "Your Password", "Your Password": "Your Password",
"Remember me": "Remember me", "Remember me": "Remember me",
"Sign in": "Sign in" "Sign in": "Sign in",
"Toggle navigation": "Toggle navigation",
"Hello": "Hello",
"My Profile": "My Profile",
"Settings": "Settings",
"Dashboard": "Dashboard",
"Save": "Save",
"Profile saved successfully": "Profile saved successfully"
} }

View File

@ -37,5 +37,12 @@
"Your Email": "Ваш Email", "Your Email": "Ваш Email",
"Your Password": "Ваш пароль", "Your Password": "Ваш пароль",
"Remember me": "Запомнить", "Remember me": "Запомнить",
"Sign in": "Войти" "Sign in": "Войти",
"Toggle navigation": "Переключение навигации",
"Hello": "Здравствуйте",
"My Profile": "Мой профиль",
"Settings": "Настройки",
"Dashboard": "Dashboard",
"Save": "Сохранить",
"Profile saved successfully": "Профиль успешно сохранен"
} }

View File

@ -0,0 +1,34 @@
@section('meta_title', __('My Profile'))
@section('h1', __('My Profile'))
<x-private.layout>
<div class="row">
<div class="col-12 mb-4">
<div class="card border-0 shadow components-section">
<div class="card-body">
<form method="post" action="{{ route('profile.update') }}">
@csrf
@method('PUT')
<x-private.forms.input :title="__('validation.attributes.email')" name="email" type="email" :value="$user->email" disabled required />
<x-private.forms.input :title="__('validation.attributes.name')" name="name" type="text" :value="$user->name" required autofocus />
<button class="btn btn-primary" type="submit">{{ __('Save') }}</button>
</form>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12 mb-4">
<div class="card border-0 shadow components-section">
<div class="card-body">
<form method="post" {{ route('profile.update-password') }}>
@csrf
@method('PUT')
<x-private.forms.input :title="__('validation.attributes.password')" name="password" type="password" required />
<x-private.forms.input :title="__('validation.attributes.password_confirmation')" name="password_confirmation" type="password" required />
<button class="btn btn-primary" type="submit">{{ __('Save') }}</button>
</form>
</div>
</div>
</div>
</div>
</x-private.layout>

View File

@ -23,6 +23,8 @@ Route::middleware(['auth', 'verified'])->group(function () {
Route::prefix('profile')->as('profile.') Route::prefix('profile')->as('profile.')
->group(function () { ->group(function () {
Route::get('/', [\App\Http\Controllers\Private\ProfileController::class, 'profile'])->name('edit'); Route::get('/', [\App\Http\Controllers\Private\ProfileController::class, 'profile'])->name('edit');
Route::put('/', [\App\Http\Controllers\Private\ProfileController::class, 'update'])->name('update');
Route::put('password', [\App\Http\Controllers\Private\ProfileController::class, 'updatePassword'])->name('update-password');
Route::get('settings', [\App\Http\Controllers\Private\ProfileController::class, 'settings'])->name('settings'); Route::get('settings', [\App\Http\Controllers\Private\ProfileController::class, 'settings'])->name('settings');
}); });
}); });