Skip to content

Commit 8c93ae1

Browse files
author
Jon Janelle
committed
added likes/dislikes
1 parent f39e82f commit 8c93ae1

11 files changed

Lines changed: 218 additions & 138 deletions

File tree

app/Comment.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66

77
class Comment extends Model
88
{
9+
/*
10+
Comment belongs to User
11+
Define an inverse one-to-many relationship.
12+
*/
913
public function user() {
10-
# Comment belongs to User
11-
# Define an inverse one-to-many relationship.
1214
return $this->belongsTo('App\User');
1315
}
1416

17+
/*
18+
Comment may have many likes (and dislikes)
19+
*/
20+
public function likes() {
21+
return $this->hasMany('App\Like')->orderBy('created_at', 'DESC');
22+
}
23+
/*
24+
Comment also belongs to Problem
25+
Define an inverse one-to-many relationship.
26+
*/
1527
public function problem() {
16-
# Comment also belongs to Problem
17-
# Define an inverse one-to-many relationship.
1828
return $this->belongsTo('App\Problem');
1929
}
2030
}

app/Http/Controllers/ProblemController.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Http\Request;
66
use Session, Auth;
7-
use App\Problem, App\User, App\Comment;
7+
use App\Problem, App\User, App\Comment, App\Like;
88

99

1010
class 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

app/Like.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@
66

77
class Like extends Model
88
{
9-
//
9+
public function user() {
10+
return $this->belongsTo('App\User');
11+
}
12+
13+
public function problem() {
14+
return $this->belongsTo('App\Problem');
15+
}
1016
}

app/User.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Notifications\Notifiable;
66
use Illuminate\Foundation\Auth\User as Authenticatable;
7-
use App\Problem;
7+
use App\Problem, App\Like;
88

99
class User extends Authenticatable
1010
{
@@ -35,6 +35,10 @@ public function comments() {
3535
return $this->hasMany('App\Comment')->orderBy('created_at', 'DESC');
3636
}
3737

38+
public function likes() {
39+
return $this->hasMany('App\Like')->orderBy('created_at', 'DESC');
40+
}
41+
3842
public function problems() {
3943
return $this->belongsToMany('App\Problem')->withTimestamps();
4044
}

database/migrations/2017_04_22_182756_connect_comments_and_likes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ConnectCommentsAndLikes extends Migration
1313
*/
1414
public function up()
1515
{
16+
//likes belong to a single comment
1617
Schema::table('likes', function (Blueprint $table) {
1718
$table->integer('comment_id')->unsigned();
1819
$table->foreign('comment_id')->references('id')->on('comments');

database/migrations/2017_04_22_182804_connect_users_and_likes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ConnectUsersAndLikes extends Migration
1313
*/
1414
public function up()
1515
{
16+
//likes belong to a single user
1617
Schema::table('likes', function (Blueprint $table) {
1718
$table->integer('user_id')->unsigned();
1819
$table->foreign('user_id')->references('id')->on('users');

database/seeds/ProblemsTableSeeder.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public function ratioSeed() {
248248
Problem::insert([
249249
'created_at' => Carbon\Carbon::now()->toDateTimeString(),
250250
'updated_at' => Carbon\Carbon::now()->toDateTimeString(),
251-
'category'=>'Ratios',
251+
'category'=>'Ratio',
252252
'name'=> 'Problem 1',
253253
'difficulty'=>1,
254254
'image'=>'/images/ratio/ratioP1.PNG',
@@ -259,7 +259,7 @@ public function ratioSeed() {
259259
Problem::insert([
260260
'created_at' => Carbon\Carbon::now()->toDateTimeString(),
261261
'updated_at' => Carbon\Carbon::now()->toDateTimeString(),
262-
'category'=>'Ratios',
262+
'category'=>'Ratio',
263263
'name'=> 'Problem 2',
264264
'difficulty'=>2,
265265
'image'=>'/images/ratio/ratioP2.PNG',
@@ -270,7 +270,7 @@ public function ratioSeed() {
270270
Problem::insert([
271271
'created_at' => Carbon\Carbon::now()->toDateTimeString(),
272272
'updated_at' => Carbon\Carbon::now()->toDateTimeString(),
273-
'category'=>'Ratios',
273+
'category'=>'Ratio',
274274
'name'=> 'Problem 3',
275275
'difficulty'=>3,
276276
'image'=>'/images/ratio/ratioP3.PNG',
@@ -281,7 +281,7 @@ public function ratioSeed() {
281281
Problem::insert([
282282
'created_at' => Carbon\Carbon::now()->toDateTimeString(),
283283
'updated_at' => Carbon\Carbon::now()->toDateTimeString(),
284-
'category'=>'Ratios',
284+
'category'=>'Ratio',
285285
'name'=> 'Problem 4',
286286
'difficulty'=>3,
287287
'image'=>'/images/ratio/ratioP4.PNG',
@@ -292,7 +292,7 @@ public function ratioSeed() {
292292
Problem::insert([
293293
'created_at' => Carbon\Carbon::now()->toDateTimeString(),
294294
'updated_at' => Carbon\Carbon::now()->toDateTimeString(),
295-
'category'=>'Ratios',
295+
'category'=>'Ratio',
296296
'name'=> 'Problem 5',
297297
'difficulty'=>8,
298298
'image'=>'/images/ratio/ratioP5.PNG',
@@ -303,7 +303,7 @@ public function ratioSeed() {
303303
Problem::insert([
304304
'created_at' => Carbon\Carbon::now()->toDateTimeString(),
305305
'updated_at' => Carbon\Carbon::now()->toDateTimeString(),
306-
'category'=>'Ratios',
306+
'category'=>'Ratio',
307307
'name'=> 'Problem 6',
308308
'difficulty'=>4,
309309
'image'=>'/images/ratio/ratioP6.PNG',
@@ -313,7 +313,11 @@ public function ratioSeed() {
313313

314314
}
315315

316-
public function seqenceSeed() {
316+
public function sequenceSeed() {
317+
318+
}
319+
320+
public function trigSeed() {
317321

318322
}
319323

@@ -328,5 +332,6 @@ public function run()
328332
$this->geomSeed();
329333
$this->ratioSeed();
330334
$this->sequenceSeed();
335+
$this->trigSeed();
331336
}
332337
}

public/css/master.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,7 @@ th, tr, td{
113113
border: 1px solid #bbc;
114114
box-shadow: 3px 5px 20px 3px rgba(50,50,50,.7);
115115
}
116+
117+
.panel-heading {
118+
font-weight: bold;
119+
}

public/css/problems.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@
9797
font-size: 1.5em;
9898
padding: 20px 5px 5px 10px;
9999
}
100+
.comment-vote {
101+
display:inline-block;
102+
margin-left: 5px;
103+
}
100104
.comment-author {
101105
float: left;
102106
padding-right: 10px;
@@ -105,6 +109,7 @@
105109
.comment-date {
106110
float: right;
107111
}
112+
108113
.comment-box {
109114
max-width: 95%;
110115
height: auto;
@@ -124,7 +129,7 @@
124129
font-family: Raleway,sans-serif;
125130
font-size: 14px;
126131
line-height: 1.5;
127-
color: #797979;
132+
color: #555;
128133
}
129134

130135
#new-comment-toggle {

0 commit comments

Comments
 (0)