-
Notifications
You must be signed in to change notification settings - Fork 0
/
now_module.js
142 lines (130 loc) · 3.41 KB
/
now_module.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
var mongoose = require('mongoose');
var classes = mongoose.model('Class');
var study = mongoose.model('StudyTime');
var users = mongoose.model('User');
var seshcomment = mongoose.model('SessionComment');
exports.searchCourse = function (department, text, callback) {
console.log(department);
var regex = new RegExp('\^' + text + '\.*', 'gi');
console.log(regex);
classes.find({dept: department, num: regex}, callback);
}
exports.createSession = function (session, callback) {
console.log('adding session');
var sesh = new study();
var time = session.time;
var title = session.title;
var course = session.course;
console.log(course);
var description = session.description;
var location = session.location;
//sesh.time = time;
sesh.title = title;
sesh.description = description;
var classes = mongoose.model('Class');
var userId = this.user.session.userId;
sesh.course = course;
sesh.users.push(userId);
sesh.save(function (err) {
if (err) console.log(err);
else {
callback(sesh);
everyone.now.distributeSession(sesh);
}
});
var course = classes.findById(course, function (err,doc) {
sesh.course = doc;
sesh.users.push(userId);
//console.log("pushed userId")
// user.findById(userId,function(err,usr) {
// usr.studytimes.push(sesh);
// usr.save(function (err) {
// if (err) {console.log(err);}
// else {
// // console.log("courses " + sesh)
// // console.log(usr)
//
// }
// });
// });
});
}
exports.addSession = function (sessionId, callback) {
var userId = this.user.session.userId;
study.findById(sessionId, function (err, sesh) {
sesh.users.push(userId);
sesh.save(function (err) {
callback(sesh);
//gonna need to update the users section for the session if at all.
})
});
}
exports.removeSession = function (sessionid) {
console.log(sessionid)
var userID = this.user.session.userId;
users.findById(userID, function(err, usr) {
if (err) { console.log(err); }
else {
usr.studytimes.id(sessionid).remove()
usr.save(function(err) {
if (err) {
console.log(err);
}
});
}
});
}
exports.searchDept = function (text, callback) {
var regex = new RegExp('\^' + text + '\.*', 'gi');
console.log(regex);
classes.distinct('dept' , {dept: regex} , callback);
}
exports.submitClass = function (department, classNum, callback) {
var userID = this.user.session.userId;
console.log(userID);
classes.findOne({dept: department, num: classNum}, function (err,course) {
users.findById(userID, function(err, usr) {
if (err) { console.log(err); }
else {
/* if (usr.classes.indexOf(course)) {
console.log('The user is already enrolled in this class');
}
else { */
console.log(usr);
usr.classes.push(course);
usr.save(function(err) {
if (err) { console.log(err); }
else {
callback(course);
}
});
// }
}
});
});
}
exports.addSessionComment = function (text, author, sessionid) {
console.log('Adding comment');
var comment = new seshcomment();
comment.text = text;
comment.author = author;
comment.save(function(err) {
if (err) {
console.log(err);
}
});
study.findById(sessionid, function(err, sesh) {
console.log(sesh);
// console.log(comment);
sesh.comments.push(comment);
sesh.save(function(err) {
if (err) {
console.log(err)
}
else {
console.log('Comment added');
everyone.now.distributeSessionComment(comment, author);
}
});
});
}