NotificationController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\DiscordUser;
  5. use App\Models\User;
  6. use App\Notifications\DynamicNotification;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Http\Request;
  9. class NotificationController extends Controller
  10. {
  11. /**
  12. * Display all notifications of an user.
  13. * @param Request $request
  14. * @param int $userId
  15. * @return Response
  16. */
  17. public function index(Request $request, int $userId)
  18. {
  19. $discordUser = DiscordUser::find($userId);
  20. $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
  21. return $user->notifications()->paginate($request->query("per_page", 50));
  22. }
  23. /**
  24. * Display a specific notification
  25. *
  26. * @param int $userId
  27. * @param int $notificationId
  28. * @return JsonResponse
  29. */
  30. public function view(int $userId, $notificationId)
  31. {
  32. $discordUser = DiscordUser::find($userId);
  33. $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
  34. $notification = $user->notifications()->where("id", $notificationId)->get()->first();
  35. if (!$notification) {
  36. return response()->json(["message" => "Notification not found."], 404);
  37. }
  38. return $notification;
  39. }
  40. /**
  41. * Send a notification to an user.
  42. *
  43. * @param Request $request
  44. * @param int $userId
  45. * @return JsonResponse
  46. */
  47. public function send(Request $request, int $userId)
  48. {
  49. $discordUser = DiscordUser::find($userId);
  50. $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
  51. $body = $request->validate([
  52. "title" => "required:string|min:0",
  53. "content" => "required:string|min:0"
  54. ]);
  55. $user->notify(
  56. new DynamicNotification($body["title"], $body["content"])
  57. );
  58. return response()->json(["message" => "Notification successfully sent."]);
  59. }
  60. /**
  61. * Delete all notifications from an user
  62. *
  63. * @param int $userId
  64. * @return JsonResponse
  65. */
  66. public function delete(int $userId)
  67. {
  68. $discordUser = DiscordUser::find($userId);
  69. $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
  70. $count = $user->notifications()->delete();
  71. return response()->json(["message" => "All notifications have been successfully deleted.", "count" => $count]);
  72. }
  73. /**
  74. * Delete a specific notification
  75. *
  76. * @param int $userId
  77. * @param int $notificationId
  78. * @return JsonResponse
  79. */
  80. public function deleteOne(int $userId, $notificationid)
  81. {
  82. $discordUser = DiscordUser::find($userId);
  83. $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
  84. $notification = $user->notifications()->where("id", $notificationid)->get()->first();
  85. if (!$notification) {
  86. return response()->json(["message" => "Notification not found."], 404);
  87. }
  88. $notification->delete();
  89. return response()->json($notification);
  90. }
  91. }