Browse Source

Update AccountController

Daniel Supernault 6 years ago
parent
commit
b5a4e45ccb
1 changed files with 42 additions and 0 deletions
  1. 42 0
      app/Http/Controllers/AccountController.php

+ 42 - 0
app/Http/Controllers/AccountController.php

@@ -3,6 +3,9 @@
 namespace App\Http\Controllers;
 
 use App\EmailVerification;
+use App\Follower;
+use App\FollowRequest;
+use App\Jobs\FollowPipeline\FollowPipeline;
 use App\Mail\ConfirmEmail;
 use App\Notification;
 use App\Profile;
@@ -236,4 +239,43 @@ class AccountController extends Controller
 
         return redirect()->back();
     }
+
+    public function followRequests(Request $request)
+    {
+        $pid = Auth::user()->profile->id;
+        $followers = FollowRequest::whereFollowingId($pid)->orderBy('id','desc')->whereIsRejected(0)->simplePaginate(10);
+        return view('account.follow-requests', compact('followers'));
+    }
+
+    public function followRequestHandle(Request $request)
+    {
+        $this->validate($request, [
+            'action' => 'required|string|max:10',
+            'id' => 'required|integer|min:1'
+        ]);
+
+        $pid = Auth::user()->profile->id;
+        $action = $request->input('action') === 'accept' ? 'accept' : 'reject';
+        $id = $request->input('id');
+        $followRequest = FollowRequest::whereFollowingId($pid)->findOrFail($id);
+        $follower = $followRequest->follower;
+
+        switch ($action) {
+            case 'accept':
+                $follow = new Follower();
+                $follow->profile_id = $follower->id;
+                $follow->following_id = $pid;
+                $follow->save();
+                FollowPipeline::dispatch($follow);
+                $followRequest->delete();
+                break;
+
+            case 'reject':
+                $followRequest->is_rejected = true;
+                $followRequest->save();
+                break;
+        }
+
+        return response()->json(['msg' => 'success'], 200);
+    }
 }