NotificationController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Str;
  5. use App\Models\Notification;
  6. use App\Http\Api\AuthApi;
  7. use App\Http\Resources\NotificationResource;
  8. class NotificationController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index(Request $request)
  16. {
  17. //
  18. $user = AuthApi::current($request);
  19. if(!$user){
  20. Log::error('notification auth failed {request}',['request'=>$request]);
  21. return $this->error(__('auth.failed'),401,401);
  22. }
  23. switch ($request->get('view')) {
  24. case 'to':
  25. $table = Notification::where('to',$user['user_uid']);
  26. $unread = Notification::where('to',$user['user_uid'])
  27. ->where('status','unread')->count();
  28. break;
  29. }
  30. if($request->has('status')){
  31. $table = $table->whereIn('status',explode(',',$request->get('status')) );
  32. }
  33. $count = $table->count();
  34. $table = $table->orderBy($request->get('order','created_at'),$request->get('dir','desc'));
  35. $table = $table->skip($request->get("offset",0))
  36. ->take($request->get('limit',10));
  37. $result = $table->get();
  38. return $this->ok(
  39. [
  40. "rows"=>NotificationResource::collection($result),
  41. "count"=>$count,
  42. 'unread'=>$unread,
  43. ]);
  44. }
  45. /**
  46. * Store a newly created resource in storage.
  47. *
  48. * @param \Illuminate\Http\Request $request
  49. * @return \Illuminate\Http\Response
  50. */
  51. public function store(Request $request)
  52. {
  53. //
  54. $user = AuthApi::current($request);
  55. if(!$user){
  56. Log::error('notification auth failed {request}',['request'=>$request]);
  57. return $this->error(__('auth.failed'),401,401);
  58. }
  59. $new = new Notification;
  60. $new->id = Str::uuid();
  61. $new->from = $user['user_uid'];
  62. $new->to = $request->get('to');
  63. $new->url = $request->get('url');
  64. $new->content = $request->get('content');
  65. $new->res_type = $request->get('res_type');
  66. $new->res_id = $request->get('res_id');
  67. $new->save();
  68. return $this->ok(new NotificationResource($new));
  69. }
  70. /**
  71. * Display the specified resource.
  72. *
  73. * @param Notification $notification
  74. * @return \Illuminate\Http\Response
  75. */
  76. public function show(Notification $notification)
  77. {
  78. //
  79. return $this->ok(new NotificationResource($notification));
  80. }
  81. /**
  82. * Update the specified resource in storage.
  83. *
  84. * @param \Illuminate\Http\Request $request
  85. * @param Notification $notification
  86. * @return \Illuminate\Http\Response
  87. */
  88. public function update(Request $request, Notification $notification)
  89. {
  90. //
  91. $notification->status = $request->get('status','read');
  92. $notification->save();
  93. $unread = Notification::where('to',$notification->to)
  94. ->where('status','unread')
  95. ->count();
  96. return $this->ok(['unread'=>$unread]);
  97. }
  98. /**
  99. * Remove the specified resource from storage.
  100. *
  101. * @param Notification $notification
  102. * @return \Illuminate\Http\Response
  103. */
  104. public function destroy(Notification $notification)
  105. {
  106. //
  107. $notification->delete();
  108. if($notification->trashed()){
  109. return $this->ok('ok');
  110. }else{
  111. return $this->error('fail',500,500);
  112. }
  113. }
  114. }