Skip to content

Commit 053a479

Browse files
committed
et tu angular
1 parent 2c4aeb4 commit 053a479

File tree

4 files changed

+81
-109
lines changed

4 files changed

+81
-109
lines changed

npm-debug.log

-40
This file was deleted.

public/javascripts/angularApp.js

+56-52
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,30 @@ function ($stateProvider, $urlRouterProvider) {
1010
//also set a controller to control this state
1111

1212
//home page with all the posts
13-
$stateProvider.state('home', {
14-
url: '/home',
15-
templateUrl: '/home.html', //inserted into ui-view when this state is active
16-
controller: 'mainCtrl',
17-
resolve: { // custom state data
18-
//query backend for all songs everytime we enter home state
19-
postPromise: ['songs', function (songs) {
20-
return songs.getAll();
21-
}]
22-
}
23-
})
13+
$stateProvider
14+
.state('home', {
15+
url: '/home',
16+
templateUrl: '/home.html', //inserted into ui-view when this state is active
17+
controller: 'mainCtrl',
18+
resolve: { // custom state data
19+
//query backend for all songs everytime we enter home state
20+
postPromise: ['songs', function (songs) {
21+
return songs.getAll();
22+
}]
23+
}
24+
})
2425

2526
//for each post
2627
.state('songs', {
2728
url: '/songs/{id}', //songs + url parameter (id) for each id
2829
templateUrl: '/songs.html', //inserted into ui-view when this state is active
29-
controller: 'songsCtrl'
30+
controller: 'songsCtrl',
31+
resolve: {
32+
//query for song post with the requested id everytime we enter the /songs/id state
33+
post: ['$stateParams', 'songs', function ($stateParams, songs) {
34+
return songs.get($stateParams.id);
35+
}]
36+
}
3037
});
3138

3239
//got to home template if you receive undefined url
@@ -43,29 +50,8 @@ function ($stateProvider, $urlRouterProvider) {
4350
app.factory('songs', ['$http', function ($http) {
4451
//create object which has song posts array. Object(o) is returned and now exposed to other Angular modules
4552
var o = {
46-
songs: [
47-
{
48-
title: 'Tame Impala - LoveParanoia',
49-
upbeats: 3
50-
},
51-
{
52-
title: 'Nirvana - Smells Like Teenage Spirit',
53-
upbeats: 0
54-
},
55-
{
56-
title: 'Drake - Model Views Controlla',
57-
upbeats: 6
58-
},
59-
{
60-
title: 'The Weeknd - The Hills',
61-
upbeats: 5
62-
},
63-
{
64-
title: 'Vitas - Opera',
65-
upbeats: 17
66-
}
67-
]
68-
}
53+
songs: []
54+
};
6955

7056
o.getAll = function () {
7157
//GET request to /songs and then run function after request is successfully returned
@@ -75,20 +61,38 @@ app.factory('songs', ['$http', function ($http) {
7561
});
7662
};
7763

78-
//when creating new song post, send a put request and after succest add to our songs array
64+
//when creating new song post, send a put request and after succes add to our songs array
7965
o.create = function (song) {
80-
return $http.post('/songs', post).success(function (data) {
66+
return $http.post('/songs', song).success(function (data) {
8167
o.songs.push(data);
8268
});
8369
};
8470

85-
//request for upbeating a song post
71+
//request for upbeating a song post and after success increment song upbeats
8672
o.upbeat = function (song) {
8773
return $http.put('/songs/' + song._id + '/upbeat').success(function (data) {
8874
song.upbeats += 1;
8975
});
9076
};
9177

78+
o.get = function (id) {
79+
//get single song post from server (async, the data returned once request is complete)
80+
return $http.get('/songs/' + id).then(function (res) {
81+
return res.data;
82+
});
83+
};
84+
85+
//add new comment to song post
86+
o.addComment = function (id, comment) {
87+
return $http.post('/songs/' + id + '/comments', comment);
88+
};
89+
90+
o.upbeatComment = function (song, comment) {
91+
return $http.put('/songs/' + song._id + '/comments/' + comment._id + '/upbeat').success(function (data) {
92+
comment.upbeats += 1;
93+
});
94+
};
95+
9296
return o;
9397
}]);
9498

@@ -103,12 +107,10 @@ app.controller('mainCtrl', [
103107
function ($scope, songs) {
104108
//any changes to $scope.songs now stored in service & available to other modules that inject 'songs' service
105109
//injecting: adding name of service to controller where we want to access it. ex. [$scope,songs <---INJECTION]
106-
107110
$scope.songs = songs.songs;
108111

109112

110113
$scope.addSong = function () {
111-
112114
//Check if user didn't submit title or submitted empty string, then alert them to enter title
113115
if (!$scope.title || $scope.title === '') {
114116
alert("Unable to submit tune. Please enter a song title!");
@@ -117,45 +119,47 @@ app.controller('mainCtrl', [
117119

118120
songs.create({
119121
title: $scope.title,
120-
link: $scope.link,
122+
link: $scope.link, //TODO: check for valid link submissions
121123
});
122124

123125
//clear name and link after
124126
$scope.title = "";
125127
$scope.link = "";
126128
};
127129

128-
$scope.addUpbeat = function (song) {
130+
$scope.upbeat = function (song) {
129131
songs.upbeat(song);
130132
};
131133
}]);
132134

133135
app.controller('songsCtrl', [
134136
'$scope', //the app object ('this')
135-
'$stateParams', //object that stores info about URL
136137
'songs',
137-
function ($scope, $stateParams, songs) {
138-
//index is post id FOR NOW
139-
// we use the stateparam from the url (/:id) to get the id
140-
$scope.songs = songs.songs[$stateParams.id];
138+
'song',
139+
function ($scope, songs, song) {
140+
$scope.song = song;
141141

142142
$scope.addComment = function () {
143+
143144
if (!$scope.body || $scope.body === '') {
144145
alert("Don't talk much do ya? No comment was posted, please try again.");
145146
return;
146147
}
147148

148-
$scope.song.comments.push({
149+
songs.addComment(song._id, {
149150
body: $scope.body,
150-
author: 'user', //anon user for now, should later be logged in user from db
151-
upbeats: 0
151+
author: 'user', //anon user for now, add auth l8r
152+
}).success(function (comment) {
153+
$scope.song.comments.push(comment);
152154
});
153155

154156
$scope.body = ''; //clear the body after
155157
};
156158

157-
$scope.addUpbeat = function (comment) {
158-
comment.upbeats++;
159+
//upbeat given comment
160+
//todo: add downbeat
161+
$scope.upbeat = function (comment) {
162+
songs.upbeatComment(song, comment);
159163
};
160164

161165
}]);

routes/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ var Song = mongoose.model('Song');
55
var Comment = mongoose.model('Comment');
66

77

8+
// home page
9+
router.get('/', function (req, res, next) {
10+
res.render('index');
11+
});
12+
813
//get all of the song posts (the front page essentially)
914
router.get('/songs', function (req, res, next) {
1015

views/index.ejs

+20-17
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
<meta charset="UTF-8">
66
<title>Tunez</title>
77

8+
89
<!--loading AngularJS-->
9-
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
10+
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.js"></script>
11+
1012
<!-- ui-router lib-->
1113
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
12-
<script src="../public/javascripts/angularApp.js"></script>
14+
<script src="/javascripts/angularApp.js"></script>
15+
1316

1417
<!--Style -->
1518
<script src="https://use.fontawesome.com/d4af7f078c.js"></script>
19+
1620
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet">
17-
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
21+
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css">
1822
<style>
1923
body {
2024
font-family: 'Montserrat', sans-serif;
@@ -81,12 +85,8 @@
8185
<!-- Link the view and controller -->
8286

8387
<body ng-app="tunez" class="container">
84-
<div class="row">
85-
<div class="col-md-12">
86-
<!-- templates will load here-->
87-
<ui-view></ui-view>
88-
</div>
89-
</div>
88+
<!-- templates will load here-->
89+
<ui-view></ui-view>
9090

9191
<script type="text/ng-template" id="/home.html">
9292
<div class="page-header">
@@ -96,22 +96,22 @@
9696
<!--List of new tunes-->
9797
<!--The ng-repeat directive iterates through song in our app (like python 'for in') -->
9898
<!-- orderBy is a built in filter, the '|' symbol seperates commands -->
99-
<div class="list-group song-posts" ng-repeat="tune in songs | orderBy: '-upbeats'">
99+
<div class="list-group song-posts" ng-repeat="song in songs | orderBy: '-upbeats'">
100100
<div class="list-group-item song-post">
101101
<!-- upbeat button -->
102102
<!--pass (reference) tune post to update upbeats when music icon is clicked -->
103103
<span class="fa-stack">
104104
<span class="fa fa-square-o fa-stack-2x"></span>
105-
<span class="fa fa-heart fa-stack-1x" ng-click="addUpbeat(tune)"></span>
105+
<span class="fa fa-heart fa-stack-1x" ng-click="upbeat(song)"></span>
106106
</span>
107107

108-
<span class="badge">UpBeats: {{tune.upbeats}}</span>
108+
<span class="badge">UpBeats: {{song.upbeats}}</span>
109109
<!-- show url if posted else hide-->
110-
<a target="_blank" ng-show="tune.link" href="{{tune.link}}">
111-
{{tune.title}}
110+
<a target="_blank" ng-show="song.link" href="{{song.link}}">
111+
{{song.title}}
112112
</a>
113-
<span ng-hide="tune.link">
114-
{{tune.title}}
113+
<span ng-hide="song.link">
114+
{{song.title}}
115115
</span>
116116
<br>
117117
<span>
@@ -143,14 +143,17 @@
143143
<span ng-hide="tune.link">
144144
{{tune.title}}
145145
</span>
146+
<br>
147+
<br>
148+
<a href="#/home">Home Page</a>
146149
</h3>
147150
</div>
148151

149152
<div ng-repeat="comment in songs.comments | orderBy:'-upbeats'">
150153
<h4>{{comment.author}}</h4>
151154
<span class="fa-stack">
152155
<span class="fa fa-square-o fa-stack-2x"></span>
153-
<span class="fa fa-heart fa-stack-1x" ng-click="addUpbeat(comment)"></span>
156+
<span class="fa fa-heart fa-stack-1x" ng-click="upbeat(comment)"></span>
154157
</span>
155158
<span class="badge"> UpBeats: {{comment.upbeats}}</span>
156159
<br>

0 commit comments

Comments
 (0)