Compare commits

..

2 Commits

Author SHA1 Message Date
AslanAV 146038cbba add rails/ujs 2024-01-12 16:42:28 +03:00
AslanAV dfaedd0fd7 REST users 2024-01-12 16:42:16 +03:00
8 changed files with 193 additions and 80 deletions

View File

@ -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']);
return redirect()->route('users.index');
}
$message = __('controllers.tasks_create');
return redirect()->route('tasks.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');
}
}

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'
];
}
}

8
package-lock.json generated
View File

@ -4,6 +4,9 @@
"requires": true,
"packages": {
"": {
"dependencies": {
"@rails/ujs": "^7.1.2"
},
"devDependencies": {
"@tailwindcss/forms": "^0.5.2",
"alpinejs": "^3.4.2",
@ -505,6 +508,11 @@
"node": ">=14"
}
},
"node_modules/@rails/ujs": {
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/@rails/ujs/-/ujs-7.1.2.tgz",
"integrity": "sha512-c5x02djEKEVVE4qfN4XgElJS4biM0xxtIVpcJ0ZHLK116U19rowTtmD0AJ/RCb3Xaewa4GPIWLlwgeC0dCQqzw=="
},
"node_modules/@rollup/rollup-android-arm-eabi": {
"version": "4.9.4",
"resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.9.4.tgz",

View File

@ -14,5 +14,8 @@
"postcss": "^8.4.31",
"tailwindcss": "^3.1.0",
"vite": "^5.0.0"
},
"dependencies": {
"@rails/ujs": "^7.1.2"
}
}

View File

@ -1,7 +1,9 @@
import './bootstrap';
import Alpine from 'alpinejs';
import ujs from '@rails/ujs';
window.Alpine = Alpine;
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,3 +1,6 @@
<meta name="csrf-token" content="{{ csrf_token() }}">
<meta name="csrf-param" content="_token" />
@vite(['resources/css/app.css', 'resources/js/app.js'])
<h2>список пользователей</h2>
<a href="{{ route('users.create') }}"> создать пользователя</a>
@ -9,6 +12,7 @@
<th>id</th>
<td>name</td>
<td>email</td>
<td>действия</td>
</tr>
</thead>
<tbody>
@ -17,59 +21,13 @@
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</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>
@endforeach
</tbody>
</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>--}}