visuddhinanda 1 rok pred
rodič
commit
cc71f68105

+ 35 - 32
api-v8/app/Http/Controllers/NotificationController.php

@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
 
 use Illuminate\Http\Request;
 use Illuminate\Support\Str;
+use Illuminate\Support\Facades\Log;
 use App\Models\Notification;
 use App\Http\Api\AuthApi;
 use App\Http\Resources\NotificationResource;
@@ -19,36 +20,37 @@ class NotificationController extends Controller
     {
         //
         $user = AuthApi::current($request);
-        if(!$user){
-            Log::error('notification auth failed {request}',['request'=>$request]);
-            return $this->error(__('auth.failed'),401,401);
+        if (!$user) {
+            Log::error('notification auth failed {request}', ['request' => $request]);
+            return $this->error(__('auth.failed'), 401, 401);
         }
         switch ($request->get('view')) {
             case 'to':
-                $table = Notification::where('to',$user['user_uid']);
-                $unread = Notification::where('to',$user['user_uid'])
-                                    ->where('status','unread')->count();
+                $table = Notification::where('to', $user['user_uid']);
+                $unread = Notification::where('to', $user['user_uid'])
+                    ->where('status', 'unread')->count();
                 break;
         }
 
-        if($request->has('status')){
-            $table = $table->whereIn('status',explode(',',$request->get('status')) );
+        if ($request->has('status')) {
+            $table = $table->whereIn('status', explode(',', $request->get('status')));
         }
         $count = $table->count();
 
-        $table = $table->orderBy($request->get('order','created_at'),$request->get('dir','desc'));
+        $table = $table->orderBy($request->get('order', 'created_at'), $request->get('dir', 'desc'));
 
-        $table = $table->skip($request->get("offset",0))
-                    ->take($request->get('limit',10));
+        $table = $table->skip($request->get("offset", 0))
+            ->take($request->get('limit', 10));
 
         $result = $table->get();
 
         return $this->ok(
             [
-            "rows"=>NotificationResource::collection($result),
-            "count"=>$count,
-            'unread'=>$unread,
-            ]);
+                "rows" => NotificationResource::collection($result),
+                "count" => $count,
+                'unread' => $unread,
+            ]
+        );
     }
 
     /**
@@ -61,9 +63,9 @@ class NotificationController extends Controller
     {
         //
         $user = AuthApi::current($request);
-        if(!$user){
-            Log::error('notification auth failed {request}',['request'=>$request]);
-            return $this->error(__('auth.failed'),401,401);
+        if (!$user) {
+            Log::error('notification auth failed {request}', ['request' => $request]);
+            return $this->error(__('auth.failed'), 401, 401);
         }
         $new = new Notification;
         $new->id = Str::uuid();
@@ -79,7 +81,8 @@ class NotificationController extends Controller
         return $this->ok(new NotificationResource($new));
     }
 
-    public static function insert($from,$to,$res_type,$res_id,$channel){
+    public static function insert($from, $to, $res_type, $res_id, $channel)
+    {
         foreach ($to as $key => $one) {
             $new = new Notification;
             $new->id = Str::uuid();
@@ -118,18 +121,18 @@ class NotificationController extends Controller
     {
         //
         $user = AuthApi::current($request);
-        if(!$user){
-            return $this->error(__('auth.failed'),401,401);
+        if (!$user) {
+            return $this->error(__('auth.failed'), 401, 401);
         }
-        if($notification->to===$user['user_uid']){
-            $notification->status = $request->get('status','read');
+        if ($notification->to === $user['user_uid']) {
+            $notification->status = $request->get('status', 'read');
             $notification->save();
-            $unread = Notification::where('to',$notification->to)
-                                ->where('status','unread')
-                                ->count();
-            return $this->ok(['unread'=>$unread]);
-        }else{
-            return $this->error(__('auth.failed'),403,403);
+            $unread = Notification::where('to', $notification->to)
+                ->where('status', 'unread')
+                ->count();
+            return $this->ok(['unread' => $unread]);
+        } else {
+            return $this->error(__('auth.failed'), 403, 403);
         }
     }
 
@@ -143,10 +146,10 @@ class NotificationController extends Controller
     {
         //
         $notification->delete();
-        if($notification->trashed()){
+        if ($notification->trashed()) {
             return $this->ok('ok');
-        }else{
-            return $this->error('fail',500,500);
+        } else {
+            return $this->error('fail', 500, 500);
         }
     }
 }