REST users

This commit is contained in:
AslanAV 2024-01-12 16:42:16 +03:00
parent 08ce25f11e
commit dfaedd0fd7
6 changed files with 182 additions and 80 deletions

View File

@ -2,45 +2,73 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Http\Requests\UpdateUserRequest;
use App\Models\User; 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; use Illuminate\Support\Facades\Auth;
class UserController extends Controller class UserController extends Controller
{ {
public function index() public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{ {
$users = User::all(); $users = User::all();
return view('users.index', compact('users')); return view('users.index', compact('users'));
} }
public function store($request) public function store(UpdateUserRequest $request): RedirectResponse
{ {
if (Auth::guest()) { if (Auth::guest()) {
abort(403, 'Вы не авторизованы!'); abort(403, 'Вы не авторизованы!');
} }
$validated = $request->validated(); $validated = $request->validated();
$createdById = Auth::id();
$data = [...$validated, 'created_by_id' => $createdById];
$task = new User(); $user = new User();
$task->fill($data); $user->name = $validated['name'];
$task->save(); $user->password = $validated['password'];
$user->email = $validated['email'];
$user->save();
if (array_key_exists('labels', $validated)) { return redirect()->route('users.index');
$task->labels()->attach($validated['labels']);
} }
$message = __('controllers.tasks_create'); public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
return redirect()->route('tasks.index');
}
public function create()
{ {
if (Auth::guest()) { if (Auth::guest()) {
abort(403, 'Вы не авторизованы!'); abort(403, 'Вы не авторизованы!');
} }
return view('users.create'); 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');
}
} }

View File

@ -0,0 +1,32 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'name' => 'required|unique:users,name|max:255',
'email' => 'email:rfc,dns',
'password' => 'required'
];
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'name' => 'required|unique:users,name|max:255',
'email' => 'email:rfc,dns',
'password' => 'required'
];
}
}

View File

@ -1,7 +1,9 @@
import './bootstrap'; import './bootstrap';
import Alpine from 'alpinejs'; import Alpine from 'alpinejs';
import ujs from '@rails/ujs';
window.Alpine = Alpine; window.Alpine = Alpine;
Alpine.start(); Alpine.start();
ujs.start();

View File

@ -0,0 +1,50 @@
@auth()
<div class="grid col-span-full">
<h1 class="max-w-2xl mb-4 text-4xl leading-none tracking-tight md:text-5xl xl:text-6xl dark:text-white"> Изменить
пользователя</h1>
{{ Form::open(['url' => route('users.update', $user), 'method' => 'PATCH', 'class' => 'w-50']) }}
<div class="flex flex-col">
<div>
{{ Form::label('name', 'Логин') }}
</div>
<div class="mt-2">
{{ Form::text('name', $user->name, ['class' => 'form-control rounded border-gray-300 w-1/3']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('name') }}
@endif
</div>
<div>
{{ Form::label('email', 'электронная почта') }}
</div>
<div class="mt-2">
{{ Form::text('email', $user->email, ['class' => 'form-control rounded border-gray-300 w-1/3']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('email') }}
@endif
</div>
<div>
{{ Form::label('password', 'Пароль') }}
</div>
<div class="mt-2">
{{ Form::text('password', '', ['class' => 'form-control rounded border-gray-300 w-1/3']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('password') }}
@endif
</div>
<div class="mt-4">
{{ Form::submit('изменить', ['class' => 'bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded']) }}
</div>
</div>
{{ Form::close() }}
</div>
@endauth

View File

@ -1,14 +1,18 @@
<meta name="csrf-token" content="{{ csrf_token() }}">
<meta name="csrf-param" content="_token" />
@vite(['resources/css/app.css', 'resources/js/app.js'])
<h2>список пользователей</h2> <h2>список пользователей</h2>
<a href="{{ route('users.create') }}"> создать пользователя</a> <a href="{{ route('users.create') }}"> создать пользователя</a>
<br> <br>
<br> <br>
<table class="mt-4"> <table class="mt-4">
<thead class="border-b-2 border-solid border-black text-left" style="text-align: left"> <thead class="border-b-2 border-solid border-black text-left" style="text-align: left">
<tr> <tr>
<th>id</th> <th>id</th>
<td>name</td> <td>name</td>
<td>email</td> <td>email</td>
<td>действия</td>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -17,59 +21,13 @@
<td>{{ $user->id }}</td> <td>{{ $user->id }}</td>
<td>{{ $user->name }}</td> <td>{{ $user->name }}</td>
<td>{{ $user->email }}</td> <td>{{ $user->email }}</td>
<td><a href="{{ route("users.edit", $user) }}">редактировать</a></td>
<td><a rel="nofollow" data-method="delete" data-confirm="Вы действительно хотите удалить?"
href="{{ route('users.destroy', $user) }}">
удалить
</a>
</td>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
</table> </table>
{{--<table class="mt-4">--}}
{{-- <thead class="border-b-2 border-solid border-black text-left" style="text-align: left">--}}
{{-- <tr>--}}
{{-- <th>{{ __('layout.table_id') }}</th>--}}
{{-- <th>{{ __('layout.table_task_status') }}</th>--}}
{{-- <th>{{ __('layout.table_name') }}</th>--}}
{{-- <th>{{ __('layout.table_creater') }}</th>--}}
{{-- <th>{{ __('layout.table_assigned') }}</th>--}}
{{-- <th>{{ __('layout.table_date_of_creation') }}</th>--}}
{{-- @auth()--}}
{{-- <th>{{ __('layout.table_actions') }}</th>--}}
{{-- @endauth--}}
{{-- </tr>--}}
{{-- </thead>--}}
{{-- <tbody>--}}
{{-- @foreach($tasks as $task)--}}
{{-- <tr class="border-b border-dashed text-left">--}}
{{-- <td>{{ $task->id }}</td>--}}
{{-- <td>{{ $taskStatuses[$task->status_id] }}</td>--}}
{{-- <td><a href="{{ route('tasks.show', $task) }}">{{ $task->name }}</a></td>--}}
{{-- <td>{{ $users[$task->created_by_id] }}</td>--}}
{{-- <td>{{ $users[$task->assigned_to_id] }}</td>--}}
{{-- <td>{{ date_format($task->created_at, 'd.m.Y') }}</td>--}}
{{-- @auth()--}}
{{-- <td>--}}
{{-- @can('delete', $task)--}}
{{-- <a--}}
{{-- class="text-red-600 hover:text-red-900"--}}
{{-- rel="nofollow"--}}
{{-- data-method="delete"--}}
{{-- data-confirm="{{ __('layout.table_delete_question') }}"--}}
{{-- href="{{ route('tasks.destroy', $task) }}"--}}
{{-- >--}}
{{-- {{ __('layout.table_delete') }}--}}
{{-- </a>--}}
{{-- @endcan--}}
{{-- @can('update', $task)--}}
{{-- <a class="text-blue-600 hover:text-blue-900"--}}
{{-- href="{{ route("tasks.edit", $task) }}"--}}
{{-- >--}}
{{-- {{ __('layout.table_edit') }}--}}
{{-- </a>--}}
{{-- @endcan--}}
{{-- </td>--}}
{{-- @endauth--}}
{{-- </tr>--}}
{{-- @endforeach--}}
{{-- </tbody>--}}
{{--</table>--}}