-
Notifications
You must be signed in to change notification settings - Fork 27
/
accessDB.js
162 lines (139 loc) · 3.83 KB
/
accessDB.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Module dependencies
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// dependencies for authentication
var passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;
var Event = require('./models/event');
var User = require('./models/user');
var Note = require('./models/note');
// Define local strategy for Passport
passport.use(new LocalStrategy({
usernameField: 'email'
},
function(email, password, done) {
User.authenticate(email, password, function(err, user) {
return done(err, user);
});
}
));
// serialize user on login
passport.serializeUser(function(user, done) {
done(null, user.id);
});
// deserialize user on logout
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
// connect to database
module.exports = {
// Define class variable
myEventID: null,
// initialize DB
startup: function(dbToUse) {
mongoose.connect(dbToUse);
// Check connection to mongoDB
mongoose.connection.on('open', function() {
console.log('We have connected to mongodb');
});
},
// get user's selected event
getMyEvent: function(callback) {
Event.findOne({'_id': this.myEventID}, function(err, myEvent) {
callback(null, myEvent);
});
},
// clear user's selected event
clearMyEvent: function(callback) {
this.myEventID = null;
callback(null);
},
// save a user
saveUser: function(userInfo, callback) {
//console.log(userInfo['fname']);
var newUser = new User ({
name : { first: userInfo.fname, last: userInfo.lname }
, email: userInfo.email
//, password: userInfo.password
}).setPassword(userInfo.password, function(newUser) {
newUser.save(function(err) {
if (err) {throw err;}
//console.log('Name: ' + newUser.name + '\nEmail: ' + newUser.email);
callback(null, userInfo);
});
});
},
// save an event
saveEvent: function(eventInfo, callback) {
var newEvent = new Event ({
name : eventInfo.name
, date : eventInfo.date
, description : eventInfo.desc
});
newEvent.save(function(err) {
if (err) {throw err;}
//console.log('Name: ' + newEvent.name + '\nDate: ' + newEvent.date + '\nDesc: ' + newEvent.description);
callback(null, eventInfo);
});
},
// save a note
saveNote: function(noteInfo, callback) {
var newNote = new Note ({
_user : noteInfo.userid
, body : noteInfo.note
//, date : Date.now
, _event : noteInfo.eventid
});
newNote.save(function (err) {
if (err) {throw err;}
//console.log('Name: ' + newNote._user + '\nNote: ' + newNote.body);
callback(null, newNote);
});
},
// disconnect from database
closeDB: function() {
mongoose.disconnect();
},
// get all the events
getEvents: function(sortby, callback) {
var query = Event.find({},'name date _id');
query.sort(sortby);
query.exec(function(err, events) {
callback(null, events);
});
},
// get all the users
getUsers: function(callback) {
User.find({}, 'name _id', function(err, users) {
callback(null, users);
});
},
// get all the notes from a specific event
getNotesFromEvent: function(eventid, callback) {
Note
.find({'_event':eventid})
.populate('_user')
.populate('_event')
.exec(function(err, notes) {
callback(null, notes);
})
},
// set the event
setEvent: function(eventid, callback) {
this.myEventID = eventid;
callback(null);
},
// get the notes from a specific user
getNotesFromUser: function(userid, callback) {
console.log('userid: ' + userid);
Note
.find({'_user':userid})
.populate('_user')
.populate('_event')
.exec(function(err, notes) {
callback(null, notes);
})
}
}