@@ -10,23 +10,30 @@ function ($stateProvider, $urlRouterProvider) {
10
10
//also set a controller to control this state
11
11
12
12
//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
+ } )
24
25
25
26
//for each post
26
27
. state ( 'songs' , {
27
28
url : '/songs/{id}' , //songs + url parameter (id) for each id
28
29
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
+ }
30
37
} ) ;
31
38
32
39
//got to home template if you receive undefined url
@@ -43,29 +50,8 @@ function ($stateProvider, $urlRouterProvider) {
43
50
app . factory ( 'songs' , [ '$http' , function ( $http ) {
44
51
//create object which has song posts array. Object(o) is returned and now exposed to other Angular modules
45
52
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
+ } ;
69
55
70
56
o . getAll = function ( ) {
71
57
//GET request to /songs and then run function after request is successfully returned
@@ -75,20 +61,38 @@ app.factory('songs', ['$http', function ($http) {
75
61
} ) ;
76
62
} ;
77
63
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
79
65
o . create = function ( song ) {
80
- return $http . post ( '/songs' , post ) . success ( function ( data ) {
66
+ return $http . post ( '/songs' , song ) . success ( function ( data ) {
81
67
o . songs . push ( data ) ;
82
68
} ) ;
83
69
} ;
84
70
85
- //request for upbeating a song post
71
+ //request for upbeating a song post and after success increment song upbeats
86
72
o . upbeat = function ( song ) {
87
73
return $http . put ( '/songs/' + song . _id + '/upbeat' ) . success ( function ( data ) {
88
74
song . upbeats += 1 ;
89
75
} ) ;
90
76
} ;
91
77
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
+
92
96
return o ;
93
97
} ] ) ;
94
98
@@ -103,12 +107,10 @@ app.controller('mainCtrl', [
103
107
function ( $scope , songs ) {
104
108
//any changes to $scope.songs now stored in service & available to other modules that inject 'songs' service
105
109
//injecting: adding name of service to controller where we want to access it. ex. [$scope,songs <---INJECTION]
106
-
107
110
$scope . songs = songs . songs ;
108
111
109
112
110
113
$scope . addSong = function ( ) {
111
-
112
114
//Check if user didn't submit title or submitted empty string, then alert them to enter title
113
115
if ( ! $scope . title || $scope . title === '' ) {
114
116
alert ( "Unable to submit tune. Please enter a song title!" ) ;
@@ -117,45 +119,47 @@ app.controller('mainCtrl', [
117
119
118
120
songs . create ( {
119
121
title : $scope . title ,
120
- link : $scope . link ,
122
+ link : $scope . link , //TODO: check for valid link submissions
121
123
} ) ;
122
124
123
125
//clear name and link after
124
126
$scope . title = "" ;
125
127
$scope . link = "" ;
126
128
} ;
127
129
128
- $scope . addUpbeat = function ( song ) {
130
+ $scope . upbeat = function ( song ) {
129
131
songs . upbeat ( song ) ;
130
132
} ;
131
133
} ] ) ;
132
134
133
135
app . controller ( 'songsCtrl' , [
134
136
'$scope' , //the app object ('this')
135
- '$stateParams' , //object that stores info about URL
136
137
'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 ;
141
141
142
142
$scope . addComment = function ( ) {
143
+
143
144
if ( ! $scope . body || $scope . body === '' ) {
144
145
alert ( "Don't talk much do ya? No comment was posted, please try again." ) ;
145
146
return ;
146
147
}
147
148
148
- $scope . song . comments . push ( {
149
+ songs . addComment ( song . _id , {
149
150
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 ) ;
152
154
} ) ;
153
155
154
156
$scope . body = '' ; //clear the body after
155
157
} ;
156
158
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 ) ;
159
163
} ;
160
164
161
165
} ] ) ;
0 commit comments