NotificationController.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\NotificationRequest;
  4. use App\Http\Resources\NotificationResource;
  5. use App\Http\Resources\NotificationSendResource;
  6. use App\Services\NotificationService;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Support\Facades\Auth;
  9. class NotificationController extends Controller
  10. {
  11. public function __construct(protected NotificationService $service) {}
  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(
  21. payload: new NotificationResource($item),
  22. message: __('messages.created'),
  23. code: 201,
  24. );
  25. }
  26. public function show(int $id): JsonResponse
  27. {
  28. $item = $this->service->findById($id);
  29. return $this->successResponse(payload: new NotificationResource($item));
  30. }
  31. public function destroy(int $id): JsonResponse
  32. {
  33. $this->service->delete($id);
  34. return $this->successResponse(message: __('messages.deleted'), code: 204);
  35. }
  36. public function myUnread(): JsonResponse
  37. {
  38. $items = $this->service->getUnreadByUser(Auth::id());
  39. return $this->successResponse(payload: NotificationSendResource::collection($items));
  40. }
  41. public function markAsRead(int $sendId): JsonResponse
  42. {
  43. $this->service->markAsRead($sendId, Auth::id());
  44. return $this->successResponse(message: __('messages.updated'));
  45. }
  46. }