NotificationController.php 2.4 KB

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