diff --git a/app/Enums/AdminStatus.php b/app/Enums/AdminStatus.php index 805d71b..257b7b2 100644 --- a/app/Enums/AdminStatus.php +++ b/app/Enums/AdminStatus.php @@ -5,6 +5,7 @@ namespace App\Enums; enum AdminStatus:int { case DISABLE = 0; + case ENABLE = 1; } diff --git a/app/Http/Controllers/Admin/AdminController.php b/app/Http/Controllers/Admin/AdminController.php new file mode 100644 index 0000000..bb2b4c6 --- /dev/null +++ b/app/Http/Controllers/Admin/AdminController.php @@ -0,0 +1,152 @@ +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' => 'عملیات با موفقیت انجام شد' + ]); + } +} + diff --git a/app/Http/Requests/Admin/AdminStoreRequest.php b/app/Http/Requests/Admin/AdminStoreRequest.php new file mode 100644 index 0000000..41baee8 --- /dev/null +++ b/app/Http/Requests/Admin/AdminStoreRequest.php @@ -0,0 +1,64 @@ +|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'], + + + ]; + } + +} diff --git a/app/Http/Requests/Admin/AdminUpdateRequest.php b/app/Http/Requests/Admin/AdminUpdateRequest.php new file mode 100644 index 0000000..5ed1f06 --- /dev/null +++ b/app/Http/Requests/Admin/AdminUpdateRequest.php @@ -0,0 +1,57 @@ +|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'], + ]; + } +} diff --git a/app/Models/Admin.php b/app/Models/Admin.php index a56f672..7abeef2 100644 --- a/app/Models/Admin.php +++ b/app/Models/Admin.php @@ -5,6 +5,7 @@ */ namespace App\Models; +use App\Enums\AdminStatus; use Illuminate\Foundation\Auth\User as Authenticatable; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; @@ -31,7 +32,7 @@ class Admin extends Authenticatable protected $casts = [ 'file_id' => 'int', - 'status' => 'int' + 'status' => AdminStatus::class, ]; protected $hidden = [ @@ -47,4 +48,9 @@ class Admin extends Authenticatable 'password', 'status' ]; + + public function file() + { + return $this->belongsTo(File::class, 'file_id'); + } } diff --git a/routes/admin.php b/routes/admin.php index 653655a..4c7d8f9 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -28,12 +28,29 @@ Route::prefix('admin')->name('admin.')->group(function () { 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'); + }); }); - - }); -