Files
sccs_laravel/app/Http/Controllers/NotificationController.php
2025-11-04 16:23:40 +05:00

142 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Notification;
use App\Traits\OneSignalHelper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class NotificationController extends Controller
{
use OneSignalHelper;
public function index()
{
$pageConfigs = [
'pageHeader' => true,
'showMenu' => true
];
$breadcrumbs = [['link' => '/', 'name' => 'Home'], ['name' => 'Notification']];
$createUrl = [['url' => 'create', 'name' => "Notification", 'slug' => 'notification']];
$notifications = Notification::all();
return view('content.notifications.index', [
'breadcrumbs' => $breadcrumbs,
'pageConfigs' => $pageConfigs,
'createUrl' => $createUrl,
'notifications' => $notifications
]);
}
public function create()
{
$pageConfigs = [
'pageHeader' => true,
'showMenu' => true
];
$breadcrumbs = [['link' => '/admin/notification', 'name' => 'Notification'], ['name' => 'Add']];
return view('content.notifications.create', [
'pageConfigs' => $pageConfigs,
'breadcrumbs' => $breadcrumbs,
]);
}
public function store(Request $request)
{
$request->validate([
'title' => ['required'],
'body' => ['required'],
]);
try {
DB::beginTransaction();
$notification = new Notification();
$notification->title = $request->title;
$notification->body = $request->body;
$notification->save();
DB::commit();
$this->sendAppNotification($request->title, $request->body);
return redirect()->route('notification.index')->with([
'success' => "Notification created successfully"
]);
} catch (\Throwable $exception) {
DB::rollBack();
return redirect()->back()->with([
'error' => $exception->getMessage()
]);
}
}
public function edit(Notification $notification)
{
$pageConfigs = [
'pageHeader' => true,
'showMenu' => true
];
$breadcrumbs = [['link' => '/admin/notification', 'name' => 'Notification'], ['name' => 'Edit']];
return view('content.notifications.edit', [
'pageConfigs' => $pageConfigs,
'breadcrumbs' => $breadcrumbs,
'notification' => $notification
]);
}
public function update(Request $request, Notification $notification)
{
$request->validate([
'title' => ['required'],
'body' => ['required'],
]);
try {
DB::beginTransaction();
$notification->title = $request->title;
$notification->body = $request->body;
$notification->update();
DB::commit();
return redirect()->route('notification.index')->with([
'success' => "Notification updated successfully"
]);
} catch (\Throwable $exception) {
DB::rollBack();
return redirect()->back()->with([
'error' => $exception->getMessage()
]);
}
}
public function destroy(Notification $notification)
{
try {
DB::beginTransaction();
$notification->delete();
DB::commit();
return redirect()->back()->with([
'success' => "Notification deleted successfully"
]);
} catch (\Throwable $exception) {
DB::rollBack();
return redirect()->back()->with([
'error' => $exception->getMessage()
]);
}
}
}