Last active
May 26, 2016 09:13
-
-
Save notrab/a46477daef0dac1d6e93 to your computer and use it in GitHub Desktop.
Walter models
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
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var validator = require('validator'); | |
var EntrySchema = new Schema({ | |
email: { | |
type: String, | |
required: true, | |
validate: [validator.isEmail, 'invalid email'] | |
}, | |
data: { | |
type: Schema.Types.Mixed, | |
validate: [validator.isJSON, 'invalid form data'] | |
}, | |
form: { | |
type: Schema.Types.ObjectId, | |
required: true, | |
ref: 'Form' | |
}, | |
created_at: { | |
type: Date, | |
default: Date.now | |
} | |
}); | |
module.exports = mongoose.model('Entry', EntrySchema); |
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
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var randToken = require('rand-token'); | |
var validator = require('validator'); | |
var FormSchema = new Schema({ | |
token: { | |
type: String, | |
required: true, | |
lowercase: true, | |
index: { | |
unique: true | |
} | |
lowercase: true | |
}, | |
name: { | |
type: String, | |
required: true | |
}, | |
after_success_url: { | |
type: String, | |
trim: true, | |
validate: [validator.isURL, 'invalid thankyou url'] | |
}, | |
after_failure_url: { | |
type: String, | |
trim: true, | |
validate: [validator.isURL, 'invalid thankyou url'] | |
}, | |
forward_query_string: { | |
type: Boolean, | |
default: false | |
}, | |
recipients: [String], | |
entries: [{ | |
type: Schema.Types.ObjectId, | |
ref: 'Entry' | |
}], | |
user: { | |
type: Schema.Types.ObjectId, | |
required: true, | |
ref: 'User' | |
}, | |
number_entries: { | |
type: Number, | |
default: 0 | |
}, | |
updated_at: { | |
type: Date, | |
required: true, | |
default: Date.now | |
} | |
}); | |
FormSchema.pre('save', function(next) { | |
this.token = randToken.generate(24); | |
next(); | |
}); | |
module.exports = mongoose.model('Form', FormSchema); |
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
var express = require('express'), | |
router = express.Router(), | |
Form = require('../models/form'); | |
router | |
.get('/', function(req, res) { | |
var params = {}; | |
Form.find(params) | |
.sort({ | |
created_at: 'desc' | |
}) | |
.exec(function(err, forms) { | |
if (err) return res.json(err); | |
return res.json(forms); | |
}) | |
}) | |
.get('/:id', function(req, res) { | |
if (req.params.id) { | |
} | |
Form.get(req.params.id, function(err, form) { | |
if (err) return res.json(err); | |
return res.json(form); | |
}) | |
}) | |
; | |
module.exports = router; |
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
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var validator = require('validator'); | |
var passportLocalMongooseEmail = require('passport-local-mongoose-email'); | |
var crypto = require('crypto'); | |
var UserSchema = new Schema({ | |
email: { | |
type: String, | |
required: true, | |
lowercase: true, | |
index: { | |
unique: true | |
}, | |
validate: [validator.isEmail, 'invalid email'] | |
}, | |
name: { | |
first: String, | |
last: String | |
}, | |
forms: [{ | |
type: Schema.Types.ObjectId, | |
ref: 'Form' | |
}], | |
created_at: { | |
type: Date, | |
required: true, | |
default: Date.now | |
} | |
}); | |
UserSchema.virtual('name.full').get(function() { | |
return this.name.first + ' ' + this.name.last; | |
}); | |
UserSchema.virtual('avatar').get(function(size) { | |
size = size || 60; | |
var hash = crypto.createHash('md5').update(this.email).digest('hex'); | |
return "https://secure.gravatar.com/avatar/" + hash + '?s=' + size | |
}); | |
UserSchema.plugin(passportLocalMongooseEmail, { | |
usernameField: 'email' | |
}); | |
module.exports = mongoose.model('User', UserSchema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment