From db49f5e0529dc760b7fa00841765a84ebb9a2f36 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 7 Jul 2019 22:27:55 -0600 Subject: [PATCH] Update HashtagFollowController, add store method --- .../Controllers/HashtagFollowController.php | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/HashtagFollowController.php b/app/Http/Controllers/HashtagFollowController.php index d597937b5..be47a9289 100644 --- a/app/Http/Controllers/HashtagFollowController.php +++ b/app/Http/Controllers/HashtagFollowController.php @@ -3,8 +3,48 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use Auth; +use App\{ + Hashtag, + HashtagFollow, + Status +}; class HashtagFollowController extends Controller { - // + public function __construct() + { + $this->middleware('auth'); + } + + public function store(Request $request) + { + $this->validate($request, [ + 'name' => 'required|alpha_num|min:1|max:124|exists:hashtags,name' + ]); + + $user = Auth::user(); + $profile = $user->profile; + $tag = $request->input('name'); + + $hashtag = Hashtag::whereName($tag)->firstOrFail(); + + $hashtagFollow = HashtagFollow::firstOrCreate([ + 'user_id' => $user->id, + 'profile_id' => $user->profile_id ?? $user->profile->id, + 'hashtag_id' => $hashtag->id + ]); + + if($hashtagFollow->wasRecentlyCreated) { + $state = 'created'; + // todo: send to HashtagFollowService + } else { + $state = 'deleted'; + $hashtagFollow->delete(); + } + + return [ + 'state' => $state + ]; + } }