Skip to content

Commit d7917b7

Browse files
committed
downbeats
1 parent 685946b commit d7917b7

File tree

6 files changed

+91
-16
lines changed

6 files changed

+91
-16
lines changed

app.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
/*
2+
app.js
3+
import server files
4+
import modules
5+
open connection to our db (tunez)
6+
*/
7+
//middleware/routing [server side MVC framework for node]
18
var express = require('express');
9+
//for path manipulation
210
var path = require('path');
311
var favicon = require('serve-favicon');
12+
//http request logging middle
413
var logger = require('morgan');
514
var cookieParser = require('cookie-parser');
15+
//express middleware, parses text makes it easier to work with
616
var bodyParser = require('body-parser');
717
var mongoose = require('mongoose');
18+
//the mongoose Models
819
require('./models/Songs'); //Our song posts model
920
require('./models/Comments'); //Our song posts model
1021

models/Comments.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ var CommentSchema = new mongoose.Schema({
1313
} //in reference to which song post this comment under
1414
});
1515

16-
//update comment upbeats and save to db
16+
//increment comment upbeats and save to db
1717
//cb is the callback function
1818
CommentSchema.methods.upbeat = function (cb) {
1919
this.upbeats += 1;
2020
this.save(cb);
2121
};
2222

23+
//decrement comment upbeats and save to db
24+
//cb is the callback function
25+
CommentSchema.methods.downbeat = function (cb) {
26+
this.upbeats -= 1;
27+
this.save(cb);
28+
}
2329

2430
mongoose.model('Comment', CommentSchema);

models/Songs.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ var SongSchema = new mongoose.Schema({
1414
}] //uses Mongoose's built in populate method to retreive array of Comment references
1515
});
1616

17-
//update and save the number of upbeats
17+
//increment song post upbeats and save to db
1818
//cb is the callback function
1919
SongSchema.methods.upbeat = function (cb) {
2020
this.upbeats += 1;
2121
this.save(cb);
2222
};
2323

24+
//decrement song post upbeats and save to db
25+
//cb is the callback function
26+
SongSchema.methods.downbeat = function (cb) {
27+
this.upbeats -= 1;
28+
this.save(cb);
29+
};
30+
2431

2532
mongoose.model('Song', SongSchema);

public/javascripts/angularApp.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//main app
22
//external modules (like ui-router) are added as a dependancy here
3-
var app = angular.module('tunez', ['ui.router'])
3+
var app = angular.module('tunez', ['ui.router']);
44

55
app.config([
66
'$stateProvider', //refers to place in app (in terms of UI/navigation)
@@ -68,13 +68,20 @@ app.factory('songs', ['$http', function ($http) {
6868
});
6969
};
7070

71-
//request for upbeating a song post and after success increment song upbeats
71+
//request for upbeating a song post and after success increment song upbeats in view
7272
o.upbeat = function (song) {
7373
return $http.put('/songs/' + song._id + '/upbeat').success(function (data) {
7474
song.upbeats += 1;
7575
});
7676
};
7777

78+
//request for downbeating a song post and after success decrement song upbeats in view
79+
o.downbeat = function (song) {
80+
return $http.put('/songs/' + song._id + '/downbeat').success(function (data) {
81+
song.upbeats -= 1;
82+
});
83+
};
84+
7885
o.get = function (id) {
7986
//get single song post from server (async, the data returned once request is complete)
8087
return $http.get('/songs/' + id).then(function (res) {
@@ -93,6 +100,12 @@ app.factory('songs', ['$http', function ($http) {
93100
});
94101
};
95102

103+
o.downbeatComment = function (song, comment) {
104+
return $http.put('/songs/' + song._id + '/comments/' + comment._id + '/downbeat').success(function (data) {
105+
comment.upbeats -= 1;
106+
});
107+
};
108+
96109
return o;
97110
}]);
98111

@@ -117,19 +130,27 @@ app.controller('mainCtrl', [
117130
return;
118131
}
119132

133+
//add new song post
120134
songs.create({
121135
title: $scope.title,
122136
link: $scope.link, //TODO: check for valid link submissions
137+
upbeats: 0
123138
});
124139

125140
//clear name and link after
126141
$scope.title = "";
127142
$scope.link = "";
128143
};
129144

145+
//upbeat song post
130146
$scope.upbeat = function (song) {
131147
songs.upbeat(song);
132148
};
149+
150+
//downbeat song post
151+
$scope.downbeat = function (song) {
152+
songs.downbeat(song);
153+
};
133154
}]);
134155

135156
app.controller('songsCtrl', [
@@ -158,9 +179,13 @@ app.controller('songsCtrl', [
158179
};
159180

160181
//upbeat given comment
161-
//todo: add downbeat
162182
$scope.upbeat = function (comment) {
163183
songs.upbeatComment(song, comment);
164184
};
165185

186+
//downbeat given comment
187+
$scope.downbeat = function (comment) {
188+
songs.downbeatComment(song, comment);
189+
};
190+
166191
}]);

routes/index.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ router.get('/', function (req, res, next) {
1010
res.render('index');
1111
});
1212

13+
1314
//get all of the song posts (the front page essentially)
1415
router.get('/songs', function (req, res, next) {
1516

@@ -20,7 +21,7 @@ router.get('/songs', function (req, res, next) {
2021
return next(err);
2122
}
2223
//json response of song posts
23-
res.json(posts)
24+
res.json(posts);
2425
});
2526
});
2627

@@ -92,6 +93,16 @@ router.put('/songs/:song/upbeat', function (req, res, next) {
9293
});
9394
});
9495

96+
//downbeat song post
97+
router.put('/songs/:song/downbeat', function (req, res, next) {
98+
req.song.downbeat(function (err, song) {
99+
if (err) {
100+
return next(err);
101+
}
102+
res.json(song);
103+
});
104+
});
105+
95106
//add new comment to a certain song post
96107
router.post('/songs/:song/comments', function (req, res, next) {
97108

@@ -127,4 +138,14 @@ router.put('/songs/:song/comments/:comment/upbeat', function (req, res, next) {
127138
});
128139
});
129140

141+
//downbeat a specific comment on a specific song post
142+
router.put('/songs/:song/comments/:comment/downbeat', function (req, res, next) {
143+
req.comment.downbeat(function (err, comment) {
144+
if (err) {
145+
return next(err);
146+
}
147+
res.json(comment);
148+
});
149+
});
150+
130151
module.exports = router;

views/index.ejs

+15-10
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@
99
<!--loading AngularJS-->
1010
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.js"></script>
1111
<!-- ui-router lib-->
12-
<script src="//unpkg.com/angular-ui-router/release/angular-ui-router.min.js"></script>
13-
<!--
1412
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
15-
-->
1613
<script src="/javascripts/angularApp.js"></script>
1714

1815

1916
<!--Style -->
2017
<script src="https://use.fontawesome.com/d4af7f078c.js"></script>
21-
2218
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet">
2319
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
20+
<!--
21+
<link rel="stylesheet" type="text/css" href="../public/stylesheets/appStyle.css">
22+
-->
2423
<style>
2524
body {
2625
font-family: 'Montserrat', sans-serif;
@@ -82,7 +81,6 @@
8281
border-bottom-color: black;
8382
}
8483
</style>
85-
8684
</head>
8785
<!-- Link the view and controller -->
8886

@@ -103,10 +101,13 @@
103101
<!-- upbeat button -->
104102
<!--pass (reference) tune post to update upbeats when music icon is clicked -->
105103
<span class="fa-stack">
106-
<span class="fa fa-square-o fa-stack-2x"></span>
107-
<span class="fa fa-heart fa-stack-1x" ng-click="upbeat(song)"></span>
104+
<span class="fa fa-square-o fa-stack-2x"></span>
105+
<span class="fa fa-volume-up fa-stack-1x" ng-click="upbeat(song)"></span>
106+
</span>
107+
<span class="fa-stack">
108+
<span class="fa fa-square-o fa-stack-2x"></span>
109+
<span class="fa fa-volume-down fa-stack-1x" ng-click="downbeat(song)"></span>
108110
</span>
109-
110111
<span class="badge">UpBeats: {{song.upbeats}}</span>
111112
<!-- show url if posted else hide-->
112113
<a target="_blank" ng-show="song.link" href="{{song.link}}">
@@ -139,7 +140,7 @@
139140
<script type="text/ng-template" id="/songs.html">
140141
<div class="page-header">
141142
<h3>
142-
<a ng-show="song.link" href="{{song.link}}">
143+
<a target="_blank" ng-show="song.link" href="{{song.link}}">
143144
{{song.title}}
144145
</a>
145146
<span ng-hide="song.link">
@@ -155,7 +156,11 @@
155156
<h4>{{comment.author}}</h4>
156157
<span class="fa-stack">
157158
<span class="fa fa-square-o fa-stack-2x"></span>
158-
<span class="fa fa-heart fa-stack-1x" ng-click="upbeat(comment)"></span>
159+
<span class="fa fa-volume-up fa-stack-1x" ng-click="upbeat(comment)"></span>
160+
</span>
161+
<span class="fa-stack">
162+
<span class="fa fa-square-o fa-stack-2x"></span>
163+
<span class="fa fa-volume-down fa-stack-1x" ng-click="downbeat(comment)"></span>
159164
</span>
160165
<span class="badge"> UpBeats: {{comment.upbeats}}</span>
161166
<br>

0 commit comments

Comments
 (0)