feat: complete admins controllers and requests
This commit is contained in:
@@ -0,0 +1,151 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Requests\Admin\Admins\adminEditRequest;
|
||||||
|
use App\Http\Requests\Admin\Admins\adminStoreRequest;
|
||||||
|
use App\Models\Admin;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
class AdminController extends Controller
|
||||||
|
{
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$admins = Admin::query()
|
||||||
|
->when($request->filled('search'), function (Builder $query) use ($request) {
|
||||||
|
$search = $request->input('search');
|
||||||
|
|
||||||
|
$query->whereAny([
|
||||||
|
'first_name',
|
||||||
|
'last_name',
|
||||||
|
'username',
|
||||||
|
'id',
|
||||||
|
'status',
|
||||||
|
'email',
|
||||||
|
'created_at'
|
||||||
|
], 'LIKE', "%$search%");
|
||||||
|
})
|
||||||
|
->when($request->filled('sort'), function (Builder $query) use ($request) {
|
||||||
|
switch ($request->input('sort')) {
|
||||||
|
case 'newest':
|
||||||
|
$query->orderByDesc('created_at');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'email':
|
||||||
|
$query->orderBy('email');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'name_desc':
|
||||||
|
$query
|
||||||
|
->orderByDesc('first_name')
|
||||||
|
->orderByDesc('last_name');
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
$query
|
||||||
|
->orderBy('first_name')
|
||||||
|
->orderBy('last_name');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->paginate();
|
||||||
|
|
||||||
|
return view('admin.admins.index', compact('admins'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
return view('admin.admins.create');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(adminStoreRequest $request)
|
||||||
|
{
|
||||||
|
$inputs = $request->validated();
|
||||||
|
|
||||||
|
try {
|
||||||
|
DB::beginTransaction();
|
||||||
|
|
||||||
|
$inputs['password'] = Hash::make($inputs['password']);
|
||||||
|
|
||||||
|
Admin::create($inputs);
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
|
||||||
|
DB::rollBack();
|
||||||
|
|
||||||
|
Log::error($exception);
|
||||||
|
|
||||||
|
return back()
|
||||||
|
->withErrors([
|
||||||
|
'general' => 'خطایی رخ داده است'
|
||||||
|
])
|
||||||
|
->withInput();
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirect()
|
||||||
|
->route('admin.admins.index')
|
||||||
|
->withErrors([
|
||||||
|
'success' => 'عملیات با موفقیت انجام شد'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function edit(Admin $admin)
|
||||||
|
{
|
||||||
|
return view('admin.admins.edit', compact('admin'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(adminEditRequest $request, Admin $admin)
|
||||||
|
{
|
||||||
|
$inputs = $request->validated();
|
||||||
|
|
||||||
|
try {
|
||||||
|
DB::beginTransaction();
|
||||||
|
|
||||||
|
if (!empty($inputs['password'])) {
|
||||||
|
$inputs['password'] = Hash::make($inputs['password']);
|
||||||
|
} else {
|
||||||
|
unset($inputs['password']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$admin->update($inputs);
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
|
||||||
|
DB::rollBack();
|
||||||
|
|
||||||
|
Log::error($exception);
|
||||||
|
|
||||||
|
return back()
|
||||||
|
->withErrors([
|
||||||
|
'general' => 'خطایی رخ داده است'
|
||||||
|
])
|
||||||
|
->withInput();
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirect()
|
||||||
|
->route('admin.admins.index')
|
||||||
|
->withErrors([
|
||||||
|
'success' => 'عملیات با موفقیت انجام شد'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(Admin $admin)
|
||||||
|
{
|
||||||
|
$admin->delete();
|
||||||
|
|
||||||
|
return redirect()
|
||||||
|
->route('admin.admins.index')
|
||||||
|
->withErrors([
|
||||||
|
'success' => 'عملیات با موفقیت انجام شد'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Admin\Admins;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class adminEditRequest 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, ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'first_name' => [
|
||||||
|
'required',
|
||||||
|
'min:2',
|
||||||
|
'max:100',
|
||||||
|
],
|
||||||
|
'last_name' => [
|
||||||
|
'required',
|
||||||
|
'min:3',
|
||||||
|
'max:100',
|
||||||
|
],
|
||||||
|
'username' => [
|
||||||
|
'required',
|
||||||
|
'min:4',
|
||||||
|
'max:10',
|
||||||
|
Rule::unique('users', 'mobile')->ignore(
|
||||||
|
$this->route('admin')?->id ?? auth()->id()
|
||||||
|
)
|
||||||
|
],
|
||||||
|
'email' => [
|
||||||
|
'required',
|
||||||
|
'max:150',
|
||||||
|
'email',
|
||||||
|
Rule::unique('users', 'mobile')->ignore(
|
||||||
|
$this->route('admin')?->id ?? auth()->id()
|
||||||
|
)
|
||||||
|
],
|
||||||
|
'password' => [
|
||||||
|
'nullable',
|
||||||
|
'min:6',
|
||||||
|
|
||||||
|
],
|
||||||
|
'image' => [
|
||||||
|
'nullable',
|
||||||
|
'image',
|
||||||
|
' mimes:jpg,jpeg,png,webp'
|
||||||
|
],
|
||||||
|
'status' => [
|
||||||
|
'required',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Admin\Admins;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class adminStoreRequest 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, ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'first_name' => [
|
||||||
|
'required',
|
||||||
|
'min:2',
|
||||||
|
'max:100',
|
||||||
|
'persian_alpha'
|
||||||
|
],
|
||||||
|
'last_name' => [
|
||||||
|
'required',
|
||||||
|
'min:3',
|
||||||
|
'max:100',
|
||||||
|
'persian_alpha'
|
||||||
|
],
|
||||||
|
'username' => [
|
||||||
|
'required',
|
||||||
|
'min:4',
|
||||||
|
'max:10',
|
||||||
|
'unique:App\Models\Admin',
|
||||||
|
],
|
||||||
|
'email' => [
|
||||||
|
'required',
|
||||||
|
'max:150',
|
||||||
|
'email',
|
||||||
|
'unique:App\Models\Admin'
|
||||||
|
],
|
||||||
|
'password' => [
|
||||||
|
'required',
|
||||||
|
'min:6',
|
||||||
|
|
||||||
|
],
|
||||||
|
'image' => [
|
||||||
|
'nullable',
|
||||||
|
'image',
|
||||||
|
' mimes:jpg,jpeg,png,webp'
|
||||||
|
],
|
||||||
|
'status' => [
|
||||||
|
'required',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user