Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = Book= mongoose.model('Book', new Schema({
- name : String,
- checked_in : Boolean
- },{ collection : 'Book' }));
- var action = new Schema({
- checked_in: Boolean,
- });
- module.exports = Activity = mongoose.model('Activity', new Schema({
- book_id: String,
- actions: [action]
- },{ collection : 'Activity' }));
- exports.update = function(req, res){
- return Book.findById(req.params.id, function(err, book) {
- var activity = new Activity({book_id: book.id});
- activity.actions.push({
- checked_in: req.body.checked_in,
- });
- Activity.update({ book_id: book.id}, activity.toObject(), { upsert: true }));
- book.checked_in = req.body.checked_in;
- return device.save(function(err) {
- return res.send(book);
- });
- });
- };
- module.exports = Activity = mongoose.model('Activity', new Schema({
- book_id: Schema.ObjectId,
- actions: [new Schema({
- checked_in: Boolean,
- last_user: String
- })]
- },{ collection : 'Activity' }));
- exports.update = function(req, res){
- // TODO: Check for undefined.
- return book.findById(req.params.id, function(err, book) {
- if(!err) {
- // Update the book.
- book.checked_in = req.body.checked_in;
- book.last_user = req.body.last_user;
- book.save();
- // If there's no associated activity for the book, create one.
- // Otherwise update and push new activity to the actions array.
- Activity.findById(book._id, function (err, activity) {
- activity.actions.push({
- checked_in: req.body.checked_in,
- last_user: req.body.last_user
- })
- activity.save();
- });
- }
- });
- };
- {
- book_id: "5058c5ddeeb0a3aa253cf9d4",
- actions: [
- { checked_in: true, last_user: 'ralph' },
- { checked_in: true, last_user: 'gonzo' },
- { checked_in: true, last_user: 'animal' }
- ]
- }
- Activity.findOne({book_id: book._id}, function (err, activity) {
- if (!activity) {
- // No Activity doc for the book yet, create one.
- activity = new Activity({book_id: book._id});
- }
- activity.actions.push({
- checked_in: req.body.checked_in,
- last_user: req.body.last_user
- });
- activity.save();
- });
Add Comment
Please, Sign In to add comment