Skip to content

Commit 4d66239

Browse files
author
Ian Redpath
committed
just getting comment functionality finalized
1 parent 09cc0bf commit 4d66239

File tree

10 files changed

+86
-8
lines changed

10 files changed

+86
-8
lines changed

public/project/components/libraries/library.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,8 @@ export class Library {
205205
return _.range(this.library.movies.length / 4)
206206
}
207207

208+
getCommentDate(datenum: number) {
209+
return new Date(datenum)
210+
}
211+
208212
}

public/project/components/libraries/library.view.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ <h3 class="h4-margins">Comments</h3>
7070
{{comment.comment}}
7171
</div>
7272
</td>
73+
<td>
74+
{{ getCommentDate(comment.date) }}
75+
</td>
7376
</tr>
7477
</table>
7578
</div>

public/project/components/movies/movie.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export class Movie {
219219
username: user.username,
220220
target: this.movie._id
221221
}
222-
this.libraryService.addCommentToLibrary(this.movie._id, comment)
222+
this.movieService.addCommentToMovie(this.movie._id, comment)
223223
.subscribe(resp => {
224224
if (resp.json().comment) {
225225
this.movie.comments.push(resp.json().comment)
@@ -241,7 +241,7 @@ export class Movie {
241241
}
242242

243243
saveEditComment() {
244-
this.libraryService.editCommentForLibrary(this.movie._id, this.editCommentId, this.editCommentText)
244+
this.movieService.editCommentForMovie(this.movie._id, this.editCommentId, this.editCommentText)
245245
.subscribe(resp => {
246246
if (resp.json().comment) {
247247
const comm: any = _.find(this.movie.comments, c => { return (<any>c)._id === this.editCommentId })
@@ -255,4 +255,8 @@ export class Movie {
255255
this.editCommentId = null
256256
this.editCommentText = ""
257257
}
258+
259+
getCommentDate(datenum: number) {
260+
return new Date(datenum)
261+
}
258262
}

public/project/components/movies/movie.view.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ <h3 class="h4-margins">Comments</h3>
8484
{{comment.comment}}
8585
</div>
8686
</td>
87+
<td>
88+
{{ getCommentDate(comment.date) }}
89+
</td>
8790
</tr>
8891
</table>
8992
</div>

public/project/services/movieService.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ export class MovieService {
3737
getMovieByImdbId(imdbId: string) {
3838
return this.http.get(`/api/project/movie?imdbId=${imdbId}`, { headers: this.headers })
3939
}
40-
/*
41-
getOrCreate(imdbid: string, title: string) {
42-
return this.http.post(`/api/project/movie?id=${imdbid}&title=${title}&`, { headers: this.headers })
40+
41+
addCommentToMovie(movieId: string, comment: any) {
42+
return this.http.post(`/api/project/movie/${movieId}/comment`, JSON.stringify({ comment }),
43+
{ headers: this.headers })
44+
}
45+
46+
editCommentForMovie(movieId: string, commentId: string, commentText: string) {
47+
return this.http.put(`/api/project/movie/${movieId}/comment/${commentId}`, JSON.stringify({ commentText }),
48+
{ headers: this.headers })
4349
}
44-
*/
4550
}

server/project/endpoints/movieEndpoints.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,19 @@ export default function(app, db) {
4646
.then(movie => { res.status(200).send({ movie }) },
4747
error => { res.status(400).send(error) })
4848
})
49+
50+
app.post('/api/project/movie/:movieId/comment', (req, res) => {
51+
const { comment } = req.body
52+
const movieId = req.params.movieId
53+
db.addCommentToMovie(movieId, comment)
54+
.then(comment => { res.status(200).send({ comment }) },
55+
error => { res.status(400).send(error) })
56+
})
57+
app.put('/api/project/movie/:movieId/comment/:commentId', (req, res) => {
58+
const { commentText } = req.body
59+
const { movieId, commentId } = req.params
60+
db.editCommentForMovie(movieId, commentId, commentText)
61+
.then(comment => { res.status(200).send({ comment }) },
62+
error => { res.status(400).send(error) })
63+
})
4964
}

server/project/models/comment.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default class CommentModel {
2121

2222
createComment(newComment) {
2323
let deferred = Q.defer()
24+
newComment.date = new Date().getTime()
2425
this.commentModel.create(newComment, (err, resp) => {
2526
if (err) {
2627
deferred.reject(err)

server/project/models/comment.schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export default function() {
66
userId: String,
77
username: String,
88
comment: String,
9-
target: String // movie _id or library _id
9+
target: String, // movie _id or library _id
10+
date: Number // numerical date object
1011
}, { collection: 'project.comment' })
1112
return CommentSchema
1213
}

server/project/models/library.model.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export default class LibraryModel {
162162

163163
addCommentToLibrary(libId: string, comment: any) {
164164
let deferred = Q.defer()
165+
comment.date = new Date().getTime()
165166
this.commentModel.create(comment, (err, resp) => {
166167
if (err) {
167168
deferred.reject(err)
@@ -181,7 +182,8 @@ export default class LibraryModel {
181182

182183
editCommentForLibrary(libId: string, commentId: string, commentText: string) {
183184
let deferred = Q.defer()
184-
this.commentModel.findByIdAndUpdate(commentId, { comment: commentText }, { new: true }, (err, resp) => {
185+
const date = new Date().getTime()
186+
this.commentModel.findByIdAndUpdate(commentId, { comment: commentText, date }, { new: true }, (err, resp) => {
185187
if (err) {
186188
deferred.reject(err)
187189
} else {

server/project/models/movie.model.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,44 @@ export default class LibraryModel {
196196
return deferred.promise
197197
}
198198

199+
addCommentToMovie(movieId: string, comment: any) {
200+
let deferred = Q.defer()
201+
comment.date = new Date().getTime()
202+
this.commentModel.create(comment, (err, resp) => {
203+
if (err) {
204+
deferred.reject(err)
205+
} else {
206+
// add comment to relevant user and library
207+
const update = { $push: { comments: resp._id } }
208+
Q.all([
209+
this.userModel.findByIdAndUpdate(comment.userId, update),
210+
this.libraryModel.findByIdAndUpdate(comment.target, update)
211+
]).then(success => {
212+
deferred.resolve(resp)
213+
}, error => { deferred.reject(error) })
214+
}
215+
})
216+
return deferred.promise
217+
}
218+
219+
editCommentForLibrary(movieId: string, commentId: string, commentText: string) {
220+
let deferred = Q.defer()
221+
const date = new Date().getTime()
222+
this.commentModel.findByIdAndUpdate(commentId, { comment: commentText, date }, { new: true }, (err, resp) => {
223+
if (err) {
224+
deferred.reject(err)
225+
} else {
226+
// add comment to relevant user and library
227+
const update = { $push: { comments: resp._id } }
228+
Q.all([
229+
this.userModel.findByIdAndUpdate(resp.userId, update),
230+
this.libraryModel.findByIdAndUpdate(resp.target, update)
231+
]).then(success => {
232+
deferred.resolve(resp)
233+
}, error => { deferred.reject(error) })
234+
}
235+
})
236+
return deferred.promise
237+
}
238+
199239
}

0 commit comments

Comments
 (0)