GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/api.vaspayment.com/app/Http/Controllers/Admin/NotificationController.php
<?php

namespace App\Http\Controllers\Admin;

use App\Models\Notification;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

class NotificationController extends Controller
{
    public function index()
    {
        try {
            $notifications = Notification::latest()->get();
            return response()->json([
                'status' => 'success',
                'data' => $notifications,
            ], 200);
        } catch (\Throwable $th) {
            return response()->json([
                'status' => 'error',
                'message' => $th->getMessage(),
            ], 401);
        }
    }

    public function storeOrUpdate(Request $request, Notification $notification = null)
    {
        try {
            $request->validate([
                'title' => 'required|string|max:100|unique:notifications,message' . ($notification && $request->has('id') ? ',' . $notification->id : ''),
                'message' => 'required|max:255',
                'target_audience' => 'required|string|max:100',
                'image' => 'required|image|max:512',
                'status' => 'required',
            ]);
        } catch (ValidationException $th) {
            return response()->json([
                'status' => 'error',
                'message' => $th->getMessage(),
            ], 401);
        }

        try {
            if (!$request->has('id')) {
                $notification = new Notification;
            }

            $notification->title = $request->input('title');
            $notification->message = $request->input('message');
            $notification->target_audience = $request->input('target_audience');
            $notification->status = $request->input('status');
            if ($request->hasFile('image')) {
                $notification->image = '/storage/' . $request->file('image')->store('notificationImages', 'public');
            }
            $notification->save();

            $message = $notification->wasRecentlyCreated ? 'Notification added' : 'Notification updated';
            return response()->json([
                'status' => 'success',
                'message' => $message,
                'data' => $notification,
            ], 200);
        } catch (\Throwable $th) {
            return response()->json([
                'status' => 'error',
                'message' => $th->getMessage(),
            ], 422);
        }
    }



    public function destroy(Request $request)
    {
        try {
            $validated = $request->validate([
                'id' => 'required|string|exists:notifications,id',
            ]);
        } catch (ValidationException $th) {
            return response()->json([
                'status' => 'error',
                'message' => $th->getMessage(),
            ], 422);
        }

        try {
            $notification = Notification::findOrFail($validated['id']);
            $notification->delete();
            return response()->json([
                'status' => 'success',
                'message' => 'Notification deleted successfully.'
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => $e->getMessage(),
            ], 500);
        }
    }
    public function userNotifications(Request $request)
    {
        try {
            $validated = $request->validate([
                'user_id' => 'required|exists:notifications,user_id',
            ]);

            $notifications = Notification::where('user_id', $validated['user_id'])->latest()->limit(10)->get();
            return response()->json([
                'status' => 'success',
                'message' => 'Notifications fetched successfully.',
                'data' => $notifications ?? [],
            ], 200);

        } catch (\Exception $e) {
            info($e->getMessage());
            return response()->json([
                'status' => 'error',
                'message' => 'You currently do not have notifications',
            ], 500);
        }
    }

  
}