| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace App\Http\Controllers;
- use App\Services\NotificationService;
- use App\Http\Requests\NotificationRequest;
- use App\Http\Resources\NotificationResource;
- use Illuminate\Http\JsonResponse;
- class NotificationController extends Controller
- {
- public function __construct(
- protected NotificationService $service,
- ) {}
- public function index(): JsonResponse
- {
- $items = $this->service->getAll();
- return $this->successResponse(payload: NotificationResource::collection($items));
- }
- /** Notificações do usuário logado (sino). */
- public function me(): JsonResponse
- {
- $items = $this->service->forUser(auth()->id());
- return $this->successResponse(payload: NotificationResource::collection($items));
- }
- /** Contador de não lidas do usuário logado. */
- public function unreadCount(): JsonResponse
- {
- return $this->successResponse(
- payload: ['count' => $this->service->unreadCount(auth()->id())]
- );
- }
- /** Marca uma notificação como lida para o usuário logado. */
- public function markRead(int $id): JsonResponse
- {
- $this->service->markRead($id, auth()->id());
- return $this->successResponse(message: __('messages.updated'));
- }
- /** Marca todas as notificações do usuário logado como lidas. */
- public function markAllRead(): JsonResponse
- {
- $this->service->markAllRead(auth()->id());
- return $this->successResponse(message: __('messages.updated'));
- }
- public function store(NotificationRequest $request): JsonResponse
- {
- $item = $this->service->create($request->validated());
- return $this->successResponse(payload: new NotificationResource($item), message: __('messages.created'), code: 201);
- }
- public function show(int $id): JsonResponse
- {
- $item = $this->service->findById($id);
- return $this->successResponse(payload: new NotificationResource($item));
- }
- public function update(NotificationRequest $request, int $id): JsonResponse
- {
- $item = $this->service->update($id, $request->validated());
- return $this->successResponse(payload: new NotificationResource($item), message: __('messages.updated'));
- }
- public function destroy(int $id): JsonResponse
- {
- $this->service->delete($id);
- return $this->successResponse(message: __('messages.deleted'), code: 204);
- }
- }
|