-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.js
95 lines (80 loc) · 2.24 KB
/
models.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var User
,Plan
,ShoppingList
,Recipe
,Ingredient;
function defineModels(mongoose, cb){
var Schema = mongoose.Schema
,ObjectId = Schema.ObjectId
,crypto = require('crypto');
/**
* Schemas
*/
User = new Schema({
username : {type : String, unique : true}
,password : {type : String, set : function(password){
this.salt = this.makeSalt();
this.hashed_password = this.encryptPassword(password);
}
}
,email : {type : String, unique : true}
,firstName: String
,lastName: String
});
Plan = new Schema({
date : Number
,recipeName : String
,recipe : {type : ObjectId, ref: 'Recipe'}
,userID : { type: ObjectId, ref: "User"}
});
ShoppingList = new Schema({
name : {type : String, set : capitalize}
,userID : { type: ObjectId, ref: "User"}
,items : [{
ingredient : Ingredient
,count : Number
}]
});
Recipe = new Schema({
name : { type : String , unique : true, set : capitalize }
,userID : { type: ObjectId, ref: "User"}
,dateCreated : { type : Date, default : Date.now }
,dateAltered : { type : Date, default : Date.now }
,procedure : String
,ingredients : [{
amount : Number
,amountUnit : String
,ingredient : Ingredient
}]
});
Ingredient = new Schema({
name : { type : String, unique : true, set : capitalize}
,prodType : String
});
/**
* Middleware
**/
Recipe.pre('save', function(next){
this.dateAltered = Date.now();
next();
});
/**
* Schema methods
**/
User.methods.makeSalt = function(){
return Math.round((new Date().valueOf() * Math.random())) + 'very secret string';
}
User.methods.encryptPassword = function(password){
return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
}
User.methods.verifyPassword = function(password){
return this.encryptPassword(password) === this.hashed_password;
}
mongoose.model('User', User);
mongoose.model('Plan', Plan);
mongoose.model('ShoppingList', ShoppingList);
mongoose.model('Recipe', Recipe);
mongoose.model('Ingredient', Ingredient);
cb();
}
exports.defineModels = defineModels;