From dfaedd0fd76fb47403609467d19db317f972e3b7 Mon Sep 17 00:00:00 2001 From: AslanAV Date: Fri, 12 Jan 2024 16:42:16 +0300 Subject: [PATCH] REST users --- app/Http/Controllers/UserController.php | 58 +++++++++++----- app/Http/Requests/StoreUserRequest.php | 32 +++++++++ app/Http/Requests/UpdateUserRequest.php | 32 +++++++++ resources/js/app.js | 2 + resources/views/users/edit.blade.php | 50 ++++++++++++++ resources/views/users/index.blade.php | 88 +++++++------------------ 6 files changed, 182 insertions(+), 80 deletions(-) create mode 100644 app/Http/Requests/StoreUserRequest.php create mode 100644 app/Http/Requests/UpdateUserRequest.php create mode 100644 resources/views/users/edit.blade.php diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 63add16..160e8b5 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -2,45 +2,73 @@ namespace App\Http\Controllers; +use App\Http\Requests\UpdateUserRequest; use App\Models\User; -use Illuminate\Http\Request; +use Illuminate\Contracts\View\Factory; +use Illuminate\Contracts\View\View; +use Illuminate\Foundation\Application; +use Illuminate\Http\RedirectResponse; use Illuminate\Support\Facades\Auth; class UserController extends Controller { - public function index() + public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application { $users = User::all(); return view('users.index', compact('users')); } - public function store($request) + public function store(UpdateUserRequest $request): RedirectResponse { if (Auth::guest()) { abort(403, 'Вы не авторизованы!'); } $validated = $request->validated(); - $createdById = Auth::id(); - $data = [...$validated, 'created_by_id' => $createdById]; - $task = new User(); - $task->fill($data); - $task->save(); + $user = new User(); + $user->name = $validated['name']; + $user->password = $validated['password']; + $user->email = $validated['email']; + $user->save(); - if (array_key_exists('labels', $validated)) { - $task->labels()->attach($validated['labels']); - } - - $message = __('controllers.tasks_create'); - return redirect()->route('tasks.index'); + return redirect()->route('users.index'); } - public function create() + public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application { if (Auth::guest()) { abort(403, 'Вы не авторизованы!'); } return view('users.create'); } + + public function edit(User $user): View|Application|Factory|\Illuminate\Contracts\Foundation\Application + { + if (Auth::guest()) { + abort(403, 'Вы не авторизованы!'); + } + return view('users.edit', compact('user')); + } + + public function update(UpdateUserRequest $request, User $user): RedirectResponse + { + if (Auth::guest()) { + abort(403, 'Вы не авторизованы!'); + } + + $validated = $request->validated(); + + $user->fill($validated); + $user->save(); + + return redirect()->route('users.index'); + } + + public function destroy(User $user): RedirectResponse + { + $user->delete(); + + return redirect()->route('users.index'); + } } diff --git a/app/Http/Requests/StoreUserRequest.php b/app/Http/Requests/StoreUserRequest.php new file mode 100644 index 0000000..a17419b --- /dev/null +++ b/app/Http/Requests/StoreUserRequest.php @@ -0,0 +1,32 @@ + + */ + public function rules() + { + return [ + 'name' => 'required|unique:users,name|max:255', + 'email' => 'email:rfc,dns', + 'password' => 'required' + ]; + } +} diff --git a/app/Http/Requests/UpdateUserRequest.php b/app/Http/Requests/UpdateUserRequest.php new file mode 100644 index 0000000..38d84c2 --- /dev/null +++ b/app/Http/Requests/UpdateUserRequest.php @@ -0,0 +1,32 @@ + + */ + public function rules() + { + return [ + 'name' => 'required|unique:users,name|max:255', + 'email' => 'email:rfc,dns', + 'password' => 'required' + ]; + } +} diff --git a/resources/js/app.js b/resources/js/app.js index a8093be..ccb88a8 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -1,7 +1,9 @@ import './bootstrap'; import Alpine from 'alpinejs'; +import ujs from '@rails/ujs'; window.Alpine = Alpine; Alpine.start(); +ujs.start(); diff --git a/resources/views/users/edit.blade.php b/resources/views/users/edit.blade.php new file mode 100644 index 0000000..d61f84b --- /dev/null +++ b/resources/views/users/edit.blade.php @@ -0,0 +1,50 @@ +@auth() +
+

Изменить + пользователя

+ + {{ Form::open(['url' => route('users.update', $user), 'method' => 'PATCH', 'class' => 'w-50']) }} +
+
+ {{ Form::label('name', 'Логин') }} +
+
+ {{ Form::text('name', $user->name, ['class' => 'form-control rounded border-gray-300 w-1/3']) }} +
+
+ @if ($errors->any()) + {{ $errors->first('name') }} + @endif +
+ +
+ {{ Form::label('email', 'электронная почта') }} +
+
+ {{ Form::text('email', $user->email, ['class' => 'form-control rounded border-gray-300 w-1/3']) }} +
+
+ @if ($errors->any()) + {{ $errors->first('email') }} + @endif +
+ +
+ {{ Form::label('password', 'Пароль') }} +
+
+ {{ Form::text('password', '', ['class' => 'form-control rounded border-gray-300 w-1/3']) }} +
+
+ @if ($errors->any()) + {{ $errors->first('password') }} + @endif +
+ +
+ {{ Form::submit('изменить', ['class' => 'bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded']) }} +
+
+ {{ Form::close() }} +
+@endauth diff --git a/resources/views/users/index.blade.php b/resources/views/users/index.blade.php index 3acc6e1..739ed4e 100644 --- a/resources/views/users/index.blade.php +++ b/resources/views/users/index.blade.php @@ -1,75 +1,33 @@ + + +@vite(['resources/css/app.css', 'resources/js/app.js'])

список пользователей

создать пользователя

- - - - - - - - - - @foreach($users as $user) +
idnameemail
+ + + + + + + + + + @foreach($users as $user) + + - @endforeach - -
idnameemailдействия
{{ $user->id }} {{ $user->name }} {{ $user->email }}редактировать + удалить + +
- - - -{{----}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- @auth()--}} -{{-- --}} -{{-- @endauth--}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- @foreach($tasks as $task)--}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- @auth()--}} -{{-- --}} -{{-- @endauth--}} -{{-- --}} -{{-- @endforeach--}} -{{-- --}} -{{--
{{ __('layout.table_id') }}{{ __('layout.table_task_status') }}{{ __('layout.table_name') }}{{ __('layout.table_creater') }}{{ __('layout.table_assigned') }}{{ __('layout.table_date_of_creation') }}{{ __('layout.table_actions') }}
{{ $task->id }}{{ $taskStatuses[$task->status_id] }}{{ $task->name }}{{ $users[$task->created_by_id] }}{{ $users[$task->assigned_to_id] }}{{ date_format($task->created_at, 'd.m.Y') }}--}} -{{-- @can('delete', $task)--}} -{{-- --}} -{{-- {{ __('layout.table_delete') }}--}} -{{-- --}} -{{-- @endcan--}} -{{-- @can('update', $task)--}} -{{-- --}} -{{-- {{ __('layout.table_edit') }}--}} -{{-- --}} -{{-- @endcan--}} -{{--
--}} + @endforeach + +