-
Notifications
You must be signed in to change notification settings - Fork 0
KNekrasov/hw 18 #224
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
Open
KirillNekrasov21012
wants to merge
1
commit into
KNekrasov/hw17
Choose a base branch
from
KNekrasov/hw18
base: KNekrasov/hw17
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
KNekrasov/hw 18 #224
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
app/Interfaces/Http/Controllers/Api/v2/UserController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| <?php | ||
|
|
||
| namespace App\Interfaces\Http\Controllers\Api\v2; | ||
|
|
||
| use App\Interfaces\Http\Requests\Api\v2\UserController\UpdateEmailRequest; | ||
| use App\Interfaces\Http\Requests\Api\v2\UserController\UpdateNameRequest; | ||
| use App\Interfaces\Http\Requests\Api\v2\UserController\UpdatePasswordRequest; | ||
| use App\Interfaces\Response\WebResponse; | ||
| use Exception; | ||
| use Illuminate\Http\Request; | ||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Support\Facades\Hash; | ||
| use Illuminate\Support\Facades\Log; | ||
| use Throwable; | ||
|
|
||
| class UserController | ||
| { | ||
| public function showProfile(Request $request): JsonResponse | ||
| { | ||
| try { | ||
| $user = $request->user(); | ||
| $response = new WebResponse( | ||
| true, | ||
| [ | ||
| "name" => $user->getName(), | ||
| "role" => $user->getRole(), | ||
| "email" => $user->getEmail(), | ||
| "created_at" => $user->getCreatedAt(), | ||
| "updated_at" => $user->getUpdatedAt(), | ||
| ], | ||
| 'Сведения пользователя', | ||
| [], | ||
| 200 | ||
| ); | ||
| } catch (Throwable $th) { | ||
| $response = new WebResponse( | ||
| false, | ||
| null, | ||
| $th->getMessage(), | ||
| is_null($th->getPrevious()) ? [] : ['error' => $th->getPrevious()->getMessage()], | ||
| $th->getCode() | ||
| ); | ||
| Log::error(__METHOD__ . var_export($response, true)); | ||
| } finally { | ||
| return response()->json( | ||
| $response->toArray(), | ||
| $response->statusCode, | ||
| [ | ||
| 'Content-Type' => 'application/json; charset=utf-8', | ||
| 'JSON_UNESCAPED_UNICODE' => true | ||
| ], | ||
| JSON_UNESCAPED_UNICODE | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public function updateName(UpdateNameRequest $request): JsonResponse | ||
| { | ||
| try { | ||
| $user = $request->user(); | ||
| if ($user->getPassword() === $request->get('newName')) { | ||
| throw new Exception('Новое имя совпадает со старым'); | ||
| } | ||
| $user->update([ | ||
| 'name' => $request->get('newName'), | ||
| ]); | ||
| $response = new WebResponse( | ||
| true, | ||
| [ | ||
| "name" => $user->getName(), | ||
| "role" => $user->getRole(), | ||
| "email" => $user->getEmail(), | ||
| "created_at" => $user->getCreatedAt(), | ||
| "updated_at" => $user->getUpdatedAt(), | ||
| ], | ||
| 'Имя пользователя изменено', | ||
| [], | ||
| 200 | ||
| ); | ||
| } catch (Throwable $th) { | ||
| $response = new WebResponse( | ||
| false, | ||
| null, | ||
| $th->getMessage(), | ||
| is_null($th->getPrevious()) ? [] : ['error' => $th->getPrevious()->getMessage()], | ||
| $th->getCode() | ||
| ); | ||
| Log::error(__METHOD__ . var_export($response, true)); | ||
| } finally { | ||
| return response()->json( | ||
| $response->toArray(), | ||
| $response->statusCode, | ||
| [ | ||
| 'Content-Type' => 'application/json; charset=utf-8', | ||
| 'JSON_UNESCAPED_UNICODE' => true | ||
| ], | ||
| JSON_UNESCAPED_UNICODE | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public function updateEmail(UpdateEmailRequest $request): JsonResponse | ||
| { | ||
| try { | ||
| $user = $request->user(); | ||
| if ($user->getEmail() === $request->get('newEmail')) { | ||
| throw new Exception('Новый email совпадает со старым'); | ||
| } | ||
| $user->update([ | ||
| 'email' => $request->get('newEmail'), | ||
| ]); | ||
| $response = new WebResponse( | ||
| true, | ||
| [ | ||
| "name" => $user->getName(), | ||
| "role" => $user->getRole(), | ||
| "email" => $user->getEmail(), | ||
| "created_at" => $user->getCreatedAt(), | ||
| "updated_at" => $user->getUpdatedAt(), | ||
| ], | ||
| 'Email пользователя изменен', | ||
| [], | ||
| 200 | ||
| ); | ||
| } catch (Throwable $th) { | ||
| $response = new WebResponse( | ||
| false, | ||
| null, | ||
| $th->getMessage(), | ||
| is_null($th->getPrevious()) ? [] : ['error' => $th->getPrevious()->getMessage()], | ||
| $th->getCode() | ||
| ); | ||
| Log::error(__METHOD__ . var_export($response, true)); | ||
| } finally { | ||
| return response()->json( | ||
| $response->toArray(), | ||
| $response->statusCode, | ||
| [ | ||
| 'Content-Type' => 'application/json; charset=utf-8', | ||
| 'JSON_UNESCAPED_UNICODE' => true | ||
| ], | ||
| JSON_UNESCAPED_UNICODE | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public function updatePassword(UpdatePasswordRequest $request): JsonResponse | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. На каждое поле по экшну - получаетя не очень эффективно, не проще ли универсальный upate эклшн для всех полей сделать? |
||
| { | ||
| try { | ||
| $user = $request->user(); | ||
| if ($user->getPassword() === $request->get('newPassword')) { | ||
| throw new Exception('Новый пароль совпадает со старым'); | ||
| } | ||
| $user->update([ | ||
| 'password' => Hash::make($request->get('newPassword')), | ||
| ]); | ||
| $response = new WebResponse( | ||
| true, | ||
| [ | ||
| 'password' => $user->getPassword(), | ||
| ], | ||
| 'Пароль пользователя изменен', | ||
| [], | ||
| 200 | ||
| ); | ||
| } catch (Throwable $th) { | ||
| $response = new WebResponse( | ||
| false, | ||
| null, | ||
| $th->getMessage(), | ||
| is_null($th->getPrevious()) ? [] : ['error' => $th->getPrevious()->getMessage()], | ||
| $th->getCode() | ||
| ); | ||
| Log::error(__METHOD__ . var_export($response, true)); | ||
| } finally { | ||
| return response()->json( | ||
| $response->toArray(), | ||
| $response->statusCode, | ||
| [ | ||
| 'Content-Type' => 'application/json; charset=utf-8', | ||
| 'JSON_UNESCAPED_UNICODE' => true | ||
| ], | ||
| JSON_UNESCAPED_UNICODE | ||
| ); | ||
| } | ||
| } | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
app/Interfaces/Http/Requests/Api/v2/UserController/UpdateEmailRequest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| namespace App\Interfaces\Http\Requests\Api\v2\UserController; | ||
|
|
||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| class UpdateEmailRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
| public function authorize(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return [ | ||
| 'newEmail' => 'required|string|email|max:255', | ||
| ]; | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
app/Interfaces/Http/Requests/Api/v2/UserController/UpdateNameRequest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| namespace App\Interfaces\Http\Requests\Api\v2\UserController; | ||
|
|
||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| class UpdateNameRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
| public function authorize(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return [ | ||
| 'newName' => 'required|string', | ||
| ]; | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
app/Interfaces/Http/Requests/Api/v2/UserController/UpdatePasswordRequest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| namespace App\Interfaces\Http\Requests\Api\v2\UserController; | ||
|
|
||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| class UpdatePasswordRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
| public function authorize(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return [ | ||
| 'newPassword' => 'required|string', | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
password?