From 095b23ac7833c1f46122a4f5260eb747f5ce745f Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 26 May 2018 16:59:31 -0600 Subject: [PATCH] Update LikeController --- app/Http/Controllers/LikeController.php | 29 ++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/LikeController.php b/app/Http/Controllers/LikeController.php index 6847a9a4b..1691f32cb 100644 --- a/app/Http/Controllers/LikeController.php +++ b/app/Http/Controllers/LikeController.php @@ -3,8 +3,35 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use Auth, Hashids; +use App\{Like, Profile, Status, User}; class LikeController extends Controller { - // + public function store(Request $request) + { + if(Auth::check() === false) { abort(403); } + $this->validate($request, [ + 'item' => 'required|integer', + ]); + + $statusId = $request->item; + + $user = Auth::user(); + $profile = $user->profile; + $status = Status::findOrFail($statusId); + + if($status->likes()->whereProfileId($profile->id)->count() !== 0) { + $like = Like::whereProfileId($profile->id)->whereStatusId($status->id)->firstOrFail(); + $like->delete(); + return redirect()->back(); + } + + $like = new Like; + $like->profile_id = $profile->id; + $like->status_id = $status->id; + $like->save(); + + return redirect($status->url()); + } }