NotificationController.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\NotificationRequest;
  4. use App\Http\Resources\NotificationResource;
  5. use App\Services\NotificationService;
  6. use Illuminate\Http\JsonResponse;
  7. class NotificationController extends Controller
  8. {
  9. public function __construct(
  10. protected NotificationService $service,
  11. ) {}
  12. public function index(): JsonResponse
  13. {
  14. $items = $this->service->getAll();
  15. return $this->successResponse(payload: NotificationResource::collection($items));
  16. }
  17. public function store(NotificationRequest $request): JsonResponse
  18. {
  19. $item = $this->service->create($request->validated());
  20. return $this->successResponse(payload: new NotificationResource($item), message: __('messages.created'), code: 201);
  21. }
  22. public function show(int $id): JsonResponse
  23. {
  24. $item = $this->service->findById($id);
  25. return $this->successResponse(payload: new NotificationResource($item));
  26. }
  27. public function update(NotificationRequest $request, int $id): JsonResponse
  28. {
  29. $item = $this->service->update($id, $request->validated());
  30. return $this->successResponse(payload: new NotificationResource($item), message: __('messages.updated'));
  31. }
  32. public function destroy(int $id): JsonResponse
  33. {
  34. $this->service->delete($id);
  35. return $this->successResponse(message: __('messages.deleted'), code: 204);
  36. }
  37. /** Notificações do usuário logado (sino). */
  38. public function me(): JsonResponse
  39. {
  40. $items = $this->service->forUser(auth()->id());
  41. return $this->successResponse(payload: NotificationResource::collection($items));
  42. }
  43. /** Marca uma notificação como lida para o usuário logado. */
  44. public function markRead(int $id): JsonResponse
  45. {
  46. $this->service->markRead($id, auth()->id());
  47. return $this->successResponse(message: __('messages.updated'));
  48. }
  49. /** Marca todas as notificações do usuário logado como lidas. */
  50. public function markAllRead(): JsonResponse
  51. {
  52. $this->service->markAllRead(auth()->id());
  53. return $this->successResponse(message: __('messages.updated'));
  54. }
  55. /** Contador de não lidas do usuário logado. */
  56. public function unreadCount(): JsonResponse
  57. {
  58. return $this->successResponse(
  59. payload: ['count' => $this->service->unreadCount(auth()->id())]
  60. );
  61. }
  62. }