44
55use Illuminate \Http \Request ;
66use Session , Auth ;
7- use App \Problem , App \User , App \Comment ;
7+ use App \Problem , App \User , App \Comment , App \ Like ;
88
99
1010class ProblemController extends Controller
@@ -56,6 +56,7 @@ private static function loadProblemData($category, $sortCat="id",$order="asc"){
5656 Session::put ($ category , $ data );
5757 }
5858
59+
5960 /*
6061 * View a problem from a given $category with id $pid
6162 * if $feedback is true, then showing problem after an answer submit.
@@ -72,7 +73,7 @@ public function show($category, $pid, $feedback=false, $correct=false, $userResp
7273 $ pData = Session::get ($ category );
7374 for ($ i =0 ; $ i <$ pData ->count (); $ i ++) {
7475 $ p =$ pData [$ i ];
75- //linear search for id (ids not necessarily ordered)
76+ //search for problem by id
7677 if ($ p ->id ==$ pid ){
7778 $ prev = 'None ' ;
7879 $ next = 'None ' ;
@@ -143,7 +144,32 @@ public function postNewComment(Request $request,$category,$pid)
143144 $ newComment ->problem_id = $ pid ;
144145 $ newComment ->user_id = Auth::user ()->id ;
145146 $ newComment ->save ();
146- return $ this ->show ($ category , $ pid );
147+ return redirect ("/problems/ " .$ category ."/problem/ " .$ pid );
148+ }
149+
150+ public function likeComment ($ category ,$ pid ,$ cid ,$ dir ){
151+ /*Check if user has already liked/disliked comment, return if yes*/
152+ $ user =Auth::user ();
153+ $ prevLikes = $ user ->likes ;
154+ for ($ i =0 ; $ i <$ prevLikes ->count (); $ i ++){
155+ if ($ prevLikes [$ i ]->comment_id ==$ cid ){
156+ return redirect ("/problems/ " .$ category ."/problem/ " .$ pid );
157+ }
158+ }
159+
160+ $ c =Comment::find ($ cid );
161+ if ($ dir =='up ' ){
162+ $ c ->likes +=1 ;
163+ }
164+ else if ($ dir =='down ' ) {
165+ $ c ->dislikes +=1 ;
166+ }
167+ $ c ->save ();
168+ $ newLike = new Like ();
169+ $ newLike ->user_id = $ user ->id ;
170+ $ newLike ->comment_id = $ cid ;
171+ $ newLike ->save ();
172+ return redirect ("/problems/ " .$ category ."/problem/ " .$ pid );
147173 }
148174
149175 /*
@@ -153,6 +179,7 @@ private function getCommentsByProblem($pid) {
153179 return Problem::find ($ pid )->comments ;
154180 }
155181
182+
156183 /*
157184 * Update user data after problem is solved.
158185 * a) Updates the solved session var to include new pid
0 commit comments