From dff76e70b3378df1483bbbfefbdbdd8aa2f650b0 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 29 May 2018 20:59:10 -0600 Subject: [PATCH] Add FollowerController --- app/Http/Controllers/FollowerController.php | 34 ++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/FollowerController.php b/app/Http/Controllers/FollowerController.php index fa35ca2bb..42c776b23 100644 --- a/app/Http/Controllers/FollowerController.php +++ b/app/Http/Controllers/FollowerController.php @@ -2,9 +2,41 @@ namespace App\Http\Controllers; +use Auth; +use App\{Follower, Profile}; use Illuminate\Http\Request; +use App\Jobs\FollowPipeline\FollowPipeline; class FollowerController extends Controller { - // + public function __construct() + { + $this->middleware('auth'); + } + + public function store(Request $request) + { + $this->validate($request, [ + 'item' => 'required|integer', + ]); + + $user = Auth::user()->profile; + $target = Profile::where('id', '!=', $user->id)->findOrFail($request->input('item')); + + $isFollowing = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->count(); + + if($isFollowing == 0) { + $follower = new Follower; + $follower->profile_id = $user->id; + $follower->following_id = $target->id; + $follower->save(); + FollowPipeline::dispatch($follower); + } else { + $follower = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->firstOrFail(); + $follower->delete(); + } + + + return redirect()->back(); + } }