Last active
December 23, 2015 20:19
-
-
Save logicware/6688992 to your computer and use it in GitHub Desktop.
Mongoose Model and Route handler for the same (REST API).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created with JetBrains WebStorm. | |
* User: Matloob | |
* Date: 9/12/13 | |
* Time: 12:06 PM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var DATA_NOT_FOUND = "Group not found!"; | |
// group or class | |
GroupSchema = new Schema({ | |
schoolId: { type: Schema.ObjectId, ref: 'School' }, | |
teacherId: { type: Schema.ObjectId, ref: 'User' }, | |
name: String, | |
description: String, | |
grade: String, | |
imageSrc: { type: String, default: "" }, | |
createdBy: { type: Schema.ObjectId, ref: 'User' }, | |
createdOn: { type: Date, default: Date.now }, | |
isActive: { type: Boolean, default: true } | |
}); | |
GroupSchema.index({ name: 1 }, { unique: true }); | |
GroupSchema.index({ schoolId: 1, teacherId: 1 }); | |
GroupSchema.statics.findByQuery = function(query, done) { | |
this.find(query) | |
.populate('schoolId', 'name city state') | |
.populate('teacherId', 'firstName lastName') | |
.exec(function(err, data){ | |
if(err) return done(err); | |
if (data.length == 0) { | |
return done(new Error(DATA_NOT_FOUND)); | |
} | |
return done(null, data); | |
}); | |
}; | |
GroupSchema.statics.createGroup = function(newGroup, done){ | |
console.log("In create Group"); | |
this.create(newGroup, function(err, group){ | |
if (err) return done(err); | |
console.log("New Group created"); | |
console.log(group); | |
done(null, group); | |
}); | |
}; | |
GroupSchema.statics.updateGroup = function(groupId, updates, done) { | |
var query = { _id: groupId }; | |
var options = { new: true }; | |
delete updates._id; | |
this.findOneAndUpdate(query, updates, options, function(err, group) { | |
if(err) return done(err); | |
if (!group){ | |
return done(new Error( DATA_NOT_FOUND )); | |
} | |
console.log(group); | |
return done(null, group); | |
}); | |
}; | |
GroupSchema.statics.deleteGroup = function(groupId, done) { | |
var self = this; | |
var Student = require('./student'); | |
Student.findOne({ groupId: groupId }, function(err, data){ | |
if (data){ | |
if(err) return done(new Error("Cannot Delete: Group has student(s)!")); | |
} else { | |
if(err) return done(err); | |
var query = { _id: groupId }; | |
self.remove(query, function(err) { | |
if(err) return done(err); | |
return done(null, "Deleted!"); | |
}); | |
} | |
}); | |
}; | |
var Group = mongoose.model("Group", GroupSchema); | |
module.exports = Group; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created with JetBrains WebStorm. | |
* User: Matloob | |
* Date: 9/14/13 | |
* Time: 7:41 PM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
var Group = require('../../app/models/group'); | |
var ObjectId = require('mongoose').Types.ObjectId; | |
exports.createGroup = function(req, res) { | |
var newGroup = createNewGroup(req.body); | |
if (newGroup.errorMessage) { | |
return res.send(newGroup.errorMessage); | |
} | |
newGroup.createdBy = ObjectId( (req.user._id + '') ); | |
Group.createGroup(newGroup, function(err, group){ | |
if(err) return res.send(err.message, 400); | |
res.send(group); | |
}); | |
}; | |
exports.getGroups = function(req, res) { | |
var query = { createdBy: req.user._id, isActive: true }; | |
Group.findByQuery(query, function(err, data){ | |
if(err) return res.send(err.message, 400); | |
res.send(data); | |
}); | |
}; | |
exports.getGroup = function(req, res) { | |
if (!req.params.id){ | |
return res.send("Please provide an id", 400); | |
} | |
var query = { _id: req.params.id, isActive: true }; | |
Group.findByQuery(query, function(err, data){ | |
if(err) return res.send(err.message, 400); | |
res.send(data[0]); | |
}); | |
}; | |
exports.updateGroup = function(req, res) { | |
if (!req.params.id){ | |
return res.send("Please provide an id", 400); | |
} | |
console.log(req.body); | |
var groupId = req.params.id; | |
var updates = {}; | |
for (var field in req.body) { | |
updates[field] = req.body[field]; | |
} | |
if (Object.getOwnPropertyNames(updates).length > 0){ | |
Group.updateGroup(groupId, updates, function(err, data){ | |
if(err) return res.send(err.message, 400); | |
res.send(data); | |
}); | |
} else { | |
res.send("Nothing to Update!", 400); | |
} | |
}; | |
exports.deleteGroup = function(req, res) { | |
if (!req.params.id){ | |
return res.send("Please provide an id", 400); | |
} | |
var groupId = req.params.id; | |
Group.deleteGroup (groupId, function(err, data){ | |
if(err) return res.send(err.message, 400); | |
res.send(data); | |
}); | |
}; | |
function createNewGroup(body) { | |
var newGroup = {}; | |
if (body.name && body.schoolId && body.teacherId) { | |
for (var field in body) { | |
newGroup[field] = body[field]; | |
} | |
} else { | |
newGroup = { errorMessage: " Group's name, schoolId, and teacherId are required!" }; | |
} | |
return newGroup; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment