feat:implement admin/admins for admin side
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Enums;
|
|||||||
enum AdminStatus:int
|
enum AdminStatus:int
|
||||||
{
|
{
|
||||||
case DISABLE = 0;
|
case DISABLE = 0;
|
||||||
|
|
||||||
case ENABLE = 1;
|
case ENABLE = 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,152 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Requests\Admin\AdminStoreRequest;
|
||||||
|
use App\Http\Requests\Admin\AdminUpdateRequest;
|
||||||
|
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(AdminUpdateRequest $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,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Admin;
|
||||||
|
|
||||||
|
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',
|
||||||
|
'persian_alpha',
|
||||||
|
'min:2',
|
||||||
|
'max:100',
|
||||||
|
],
|
||||||
|
'last_name' => [
|
||||||
|
'required',
|
||||||
|
'persian_alpha',
|
||||||
|
'min:3',
|
||||||
|
'max:100',
|
||||||
|
],
|
||||||
|
'username' => [
|
||||||
|
'required',
|
||||||
|
'string',
|
||||||
|
'min:3',
|
||||||
|
'max:100',
|
||||||
|
'unique:admins,username',
|
||||||
|
],
|
||||||
|
'email' => [
|
||||||
|
'required',
|
||||||
|
'email',
|
||||||
|
'max:255',
|
||||||
|
'unique:admins,email'
|
||||||
|
],
|
||||||
|
|
||||||
|
'password' =>
|
||||||
|
['required',
|
||||||
|
'string',
|
||||||
|
'min:8'
|
||||||
|
],
|
||||||
|
|
||||||
|
'status' => ['required'],
|
||||||
|
|
||||||
|
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Admin;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class AdminUpdateRequest 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',
|
||||||
|
'persian_alpha',
|
||||||
|
'min:2',
|
||||||
|
'max:100',
|
||||||
|
],
|
||||||
|
'last_name' => [
|
||||||
|
'required',
|
||||||
|
'persian_alpha',
|
||||||
|
'min:3',
|
||||||
|
'max:100',
|
||||||
|
],
|
||||||
|
|
||||||
|
'username' => [
|
||||||
|
'required',
|
||||||
|
'unique:admins,username,' . $this->route('admin')->id,
|
||||||
|
],
|
||||||
|
|
||||||
|
'email' => [
|
||||||
|
'required',
|
||||||
|
'email',
|
||||||
|
'unique:admins,email,' . $this->route('admin')->id,
|
||||||
|
],
|
||||||
|
|
||||||
|
'password' => ['nullable', 'min:8'],
|
||||||
|
|
||||||
|
'status' => ['required'],
|
||||||
|
|
||||||
|
'image' => ['nullable', 'image'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
use App\Enums\AdminStatus;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
@@ -31,7 +32,7 @@ class Admin extends Authenticatable
|
|||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'file_id' => 'int',
|
'file_id' => 'int',
|
||||||
'status' => 'int'
|
'status' => AdminStatus::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $hidden = [
|
protected $hidden = [
|
||||||
@@ -47,4 +48,9 @@ class Admin extends Authenticatable
|
|||||||
'password',
|
'password',
|
||||||
'status'
|
'status'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function file()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(File::class, 'file_id');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-3
@@ -28,12 +28,29 @@ Route::prefix('admin')->name('admin.')->group(function () {
|
|||||||
Route::get('dashboard', [DashboardController::class, 'index'])->name('dashboard.index');
|
Route::get('dashboard', [DashboardController::class, 'index'])->name('dashboard.index');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::prefix('admins')->name('admins.')->controller(AdminController::class)
|
||||||
|
->group(function () {
|
||||||
|
|
||||||
|
Route::get('/', 'index')->name('index');
|
||||||
|
|
||||||
|
Route::get('create', 'create')->name('create');
|
||||||
|
Route::post('store', 'store')->name('store');
|
||||||
|
|
||||||
|
Route::prefix('{admin}')->group(function () {
|
||||||
|
|
||||||
|
Route::get('edit', 'edit')->name('edit');
|
||||||
|
Route::put('update', 'update')->name('update');
|
||||||
|
|
||||||
|
Route::delete('destroy', 'destroy')->name('destroy');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user