Guest User

Untitled

a guest
Jan 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. module.exports = Book= mongoose.model('Book', new Schema({
  2. name : String,
  3. checked_in : Boolean
  4. },{ collection : 'Book' }));
  5.  
  6. var action = new Schema({
  7. checked_in: Boolean,
  8. });
  9.  
  10. module.exports = Activity = mongoose.model('Activity', new Schema({
  11. book_id: String,
  12. actions: [action]
  13. },{ collection : 'Activity' }));
  14.  
  15. exports.update = function(req, res){
  16. return Book.findById(req.params.id, function(err, book) {
  17. var activity = new Activity({book_id: book.id});
  18. activity.actions.push({
  19. checked_in: req.body.checked_in,
  20. });
  21.  
  22. Activity.update({ book_id: book.id}, activity.toObject(), { upsert: true }));
  23.  
  24. book.checked_in = req.body.checked_in;
  25. return device.save(function(err) {
  26. return res.send(book);
  27. });
  28. });
  29. };
  30.  
  31. module.exports = Activity = mongoose.model('Activity', new Schema({
  32. book_id: Schema.ObjectId,
  33. actions: [new Schema({
  34. checked_in: Boolean,
  35. last_user: String
  36. })]
  37. },{ collection : 'Activity' }));
  38.  
  39. exports.update = function(req, res){
  40. // TODO: Check for undefined.
  41. return book.findById(req.params.id, function(err, book) {
  42. if(!err) {
  43. // Update the book.
  44. book.checked_in = req.body.checked_in;
  45. book.last_user = req.body.last_user;
  46. book.save();
  47.  
  48. // If there's no associated activity for the book, create one.
  49. // Otherwise update and push new activity to the actions array.
  50. Activity.findById(book._id, function (err, activity) {
  51. activity.actions.push({
  52. checked_in: req.body.checked_in,
  53. last_user: req.body.last_user
  54. })
  55.  
  56. activity.save();
  57. });
  58. }
  59. });
  60. };
  61.  
  62. {
  63. book_id: "5058c5ddeeb0a3aa253cf9d4",
  64. actions: [
  65. { checked_in: true, last_user: 'ralph' },
  66. { checked_in: true, last_user: 'gonzo' },
  67. { checked_in: true, last_user: 'animal' }
  68. ]
  69. }
  70.  
  71. Activity.findOne({book_id: book._id}, function (err, activity) {
  72. if (!activity) {
  73. // No Activity doc for the book yet, create one.
  74. activity = new Activity({book_id: book._id});
  75. }
  76. activity.actions.push({
  77. checked_in: req.body.checked_in,
  78. last_user: req.body.last_user
  79. });
  80.  
  81. activity.save();
  82. });
Add Comment
Please, Sign In to add comment