From 00a0624eea9ddbb9c6ad8ebece6fae904dd6b26d Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Fri, 7 Jul 2023 00:07:19 +0600 Subject: [PATCH] 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. --- app/Dto/Request/Private/Profile/Update.php | 17 ++++++++++ app/Http/Controllers/Private/Controller.php | 13 +++++++ .../Controllers/Private/ProfileController.php | 29 +++++++++++++--- .../Private/Profile/UpdateRequest.php | 25 ++++++++++++++ app/Services/Private/ProfileService.php | 31 +++++++++++++++++ lang/en.json | 9 ++++- lang/ru.json | 9 ++++- .../views/private/profile/profile.blade.php | 34 +++++++++++++++++++ routes/web.php | 2 ++ 9 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 app/Dto/Request/Private/Profile/Update.php create mode 100644 app/Http/Controllers/Private/Controller.php create mode 100644 app/Http/Requests/Private/Profile/UpdateRequest.php create mode 100644 app/Services/Private/ProfileService.php create mode 100644 resources/views/private/profile/profile.blade.php diff --git a/app/Dto/Request/Private/Profile/Update.php b/app/Dto/Request/Private/Profile/Update.php new file mode 100644 index 0000000..3ea10b4 --- /dev/null +++ b/app/Dto/Request/Private/Profile/Update.php @@ -0,0 +1,17 @@ +name; + } +} diff --git a/app/Http/Controllers/Private/Controller.php b/app/Http/Controllers/Private/Controller.php new file mode 100644 index 0000000..0d678f1 --- /dev/null +++ b/app/Http/Controllers/Private/Controller.php @@ -0,0 +1,13 @@ + Auth::user() + 'user' => $request->user() ]); } - public function settings(): View + public function settings(Request $request): View { 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()); + } } diff --git a/app/Http/Requests/Private/Profile/UpdateRequest.php b/app/Http/Requests/Private/Profile/UpdateRequest.php new file mode 100644 index 0000000..8dcaadb --- /dev/null +++ b/app/Http/Requests/Private/Profile/UpdateRequest.php @@ -0,0 +1,25 @@ + ['required', 'max:255'], + ]; + } + + public function getDto(): Update + { + return new Update(name: $this->input('name')); + } +} diff --git a/app/Services/Private/ProfileService.php b/app/Services/Private/ProfileService.php new file mode 100644 index 0000000..080c090 --- /dev/null +++ b/app/Services/Private/ProfileService.php @@ -0,0 +1,31 @@ + $update->getName() + ]; + $this->userCommandHandler->handleUpdate($user, $data); + } catch (\Throwable $e) { + report($e->getMessage()); + return $this->errService($e->getMessage()); + } + return $this->ok(__('Profile saved successfully')); + } +} diff --git a/lang/en.json b/lang/en.json index b7e847e..803e73b 100644 --- a/lang/en.json +++ b/lang/en.json @@ -37,5 +37,12 @@ "Your Email": "Your Email", "Your Password": "Your Password", "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" } diff --git a/lang/ru.json b/lang/ru.json index 16231f6..8602520 100644 --- a/lang/ru.json +++ b/lang/ru.json @@ -37,5 +37,12 @@ "Your Email": "Ваш Email", "Your Password": "Ваш пароль", "Remember me": "Запомнить", - "Sign in": "Войти" + "Sign in": "Войти", + "Toggle navigation": "Переключение навигации", + "Hello": "Здравствуйте", + "My Profile": "Мой профиль", + "Settings": "Настройки", + "Dashboard": "Dashboard", + "Save": "Сохранить", + "Profile saved successfully": "Профиль успешно сохранен" } diff --git a/resources/views/private/profile/profile.blade.php b/resources/views/private/profile/profile.blade.php new file mode 100644 index 0000000..608c13a --- /dev/null +++ b/resources/views/private/profile/profile.blade.php @@ -0,0 +1,34 @@ +@section('meta_title', __('My Profile')) +@section('h1', __('My Profile')) + +
+
+
+
+
+ @csrf + @method('PUT') + + + + +
+
+
+
+
+
+
+
+
+ @csrf + @method('PUT') + + + + +
+
+
+
+
diff --git a/routes/web.php b/routes/web.php index daac5b0..48ce5e0 100644 --- a/routes/web.php +++ b/routes/web.php @@ -23,6 +23,8 @@ Route::middleware(['auth', 'verified'])->group(function () { Route::prefix('profile')->as('profile.') ->group(function () { 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'); }); });