/
users.js
78 lines (64 loc) · 2.12 KB
/
users.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
// Dependencies
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// -----------------
module.exports = function(connection) {
// --- EDIT HERE
var modelName = "Users";
// Create Schema
var theSchema = new Schema({
email: {type : String, required : true, index: {unique: true, dropDups: true}},
firstName: {type : String, required : true},
lastName: {type : String, required : true},
age: {type : Number, required : true},
password: {type : String, required : true, select: false},
skills: {type : Array, required : true}
});
// -----------------
// Model
var theModel = connection.model(modelName, theSchema);
var tagsToReplace = {
'&': '&',
'<': '<',
'>': '>'
};
// Escapes HTML so that evil people can't inject mean things into the page
function escapeHtml(str) {
return str.replace(/[&<>]/g, function (tag) {
return tagsToReplace[tag] || tag;
});
}
function Store() {
// Constructor
}
// Returns a Todo by it's id
Store.prototype.find = function (params, callback) {
theModel.find(params.query, function (err, data) {
callback(err, data);
})
}
Store.prototype.get = function (id, params, callback) {
theModel.find({ _id: id },function (err, data) {
callback(err, data);
});
}
Store.prototype.create = function (data, params, callback) {
// Create our actual Todo object so that we only get what we really want
var data = new theModel(data);
data.save(function (err, data) {
//console.log(err, data);
callback(err, data);
});
}
Store.prototype.update = function (id, data, params, callback) {
theModel.update({_id: id}, data, { upsert: true }, function(err, data) {
return callback(err, data);
});
}
Store.prototype.remove = function (id, params, callback) {
theModel.findByIdAndRemove(id, function(err, data) {
return callback(err, data);
});
}
return Store;
}