feat: implement admin management
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\AdminStoreRequest;
|
||||
use App\Models\Admin;
|
||||
use App\Models\AdminAvatar;
|
||||
use App\Models\AdminPermission;
|
||||
use App\Models\File;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@@ -72,6 +73,8 @@ class AdminController extends Controller
|
||||
->paginate()
|
||||
->withQueryString();
|
||||
|
||||
|
||||
|
||||
return view('admin.admins.index', compact('title', 'admins'));
|
||||
}
|
||||
|
||||
@@ -132,19 +135,20 @@ class AdminController extends Controller
|
||||
'extension' => $image->extension(),
|
||||
]);
|
||||
|
||||
// AdminAvatar::create([
|
||||
// 'admin_id' => $admin->id,
|
||||
// 'file_id' => $file->id,
|
||||
// ]);
|
||||
AdminAvatar::create([
|
||||
'admin_id' => $admin->id,
|
||||
'file_id' => $file->id,
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($data['permissions'] as $permission) {
|
||||
foreach ($data['permissions'] ?? [] as $permission) {
|
||||
AdminPermission::create([
|
||||
'admin_id' => $admin->id,
|
||||
'permission' => $permission,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
DB::commit();
|
||||
|
||||
return redirect()
|
||||
@@ -177,23 +181,80 @@ class AdminController extends Controller
|
||||
}
|
||||
|
||||
|
||||
public function destroy()
|
||||
public function destroy(Admin $admin)
|
||||
{
|
||||
if ($admin->id === auth('admin')->id()) {
|
||||
return back()->with('error', 'امکان حذف حساب کاربری خودتان وجود ندارد.');
|
||||
}
|
||||
|
||||
try {
|
||||
$admin->delete();
|
||||
|
||||
return redirect()
|
||||
->route('admin.admins.index')
|
||||
->with('success', 'مدیر با موفقیت به سطل زباله منتقل شد.');
|
||||
|
||||
} catch (\Exception $exception) {
|
||||
Log::error('خطا در حذف مدیر', [
|
||||
'admin_id' => $admin->id,
|
||||
'message' => $exception->getMessage(),
|
||||
]);
|
||||
|
||||
return back()->with('error', 'خطایی هنگام حذف مدیر رخ داد.');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function trash()
|
||||
{
|
||||
|
||||
$title = 'مدیران حذف شده';
|
||||
$admins = Admin::onlyTrashed()
|
||||
->paginate();
|
||||
return view('admin.admins.trash' , compact('title' , 'admins'));
|
||||
}
|
||||
|
||||
public function restore()
|
||||
public function restore($id)
|
||||
{
|
||||
$admin = Admin::onlyTrashed()->findOrFail($id);
|
||||
|
||||
$admin->restore();
|
||||
|
||||
return redirect()
|
||||
->route('admin.admins.trash.index')
|
||||
->with('success', 'مدیر با موفقیت بازیابی شد.');
|
||||
}
|
||||
|
||||
public function forceDelete()
|
||||
public function forceDelete($id)
|
||||
{
|
||||
$admin = Admin::onlyTrashed()->findOrFail($id);
|
||||
|
||||
$avatar = $admin->avatar()->with('file')->first();
|
||||
$file = $avatar?->file;
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$avatar?->delete();
|
||||
|
||||
deleteFile($file);
|
||||
|
||||
$admin->forceDelete();
|
||||
|
||||
DB::commit();
|
||||
|
||||
return redirect()
|
||||
->route('admin.admins.trash.index')
|
||||
->with('success', 'مدیر و آواتار او برای همیشه حذف شدند.');
|
||||
|
||||
} catch (\Exception $exception) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::error('خطا در حذف دائمی مدیر', [
|
||||
'admin_id' => $admin->id,
|
||||
'message' => $exception->getMessage(),
|
||||
]);
|
||||
|
||||
return back()->with('error', 'حذف دائمی مدیر با خطا مواجه شد.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user