-
Notifications
You must be signed in to change notification settings - Fork 2
/
dal.coffee
208 lines (179 loc) · 5.18 KB
/
dal.coffee
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Copyright (C) 2013 Avi Romanoff <aviromanoff at gmail.com>
# Provides a data access layer for common
# database operations and queries.
async = require "async"
colors = require "colors"
moment = require "moment"
config = require "./config"
models = require "./models"
logging = require "./logging"
Account = models.Account
Course = models.Course
Assignment = models.Assignment
logger = new logging.Logger "API"
L = (prefix, message, urgency="debug") ->
logger[urgency] "#{prefix.underline} :: #{message}"
module.exports =
# Used for debugging; normal account creation happens in jbha module
_create_account: (username, cb) ->
account = new Account()
account._id = username
account.nickname = "TestAccount"
account.save (err, doc) =>
if err
return cb err
token =
cookie: "1235TESTCOOKIE54321"
username: doc._id
cb null, doc, token
read_settings: (username, cb) ->
Account
.findOne()
.where('_id', username)
.select('nickname is_new firstrun updated migrate')
.exec cb
update_settings: (username, settings, cb) ->
Account.update _id: username,
nickname: settings.nickname
firstrun: settings.firstrun
migrate: settings.migrate,
cb
# Used for debugging; currently no public delete function.
_delete_account: (username, account, cb) ->
async.parallel [
(callback) ->
Account
.where('_id', account)
.remove callback
(callback) ->
Course
.where('owner', account)
.remove callback
(callback) ->
Assignment
.where('owner', account)
.remove callback
], cb
migrate: (username, nuke, cb) ->
finish = ->
Account.update _id: username,
migrate: false,
cb
if nuke
async.parallel [
(callback) ->
Course
.where('owner', username)
.remove callback
(callback) ->
Assignment
.where('owner', username)
.remove callback
], finish
else
finish()
# JSON-ready dump of an account's courses and assignments
by_course: (username, cb) ->
Course
.where('owner', username)
.populate('assignments', 'title archived details date done jbha_id')
.select('-owner -jbha_id')
.exec (err, courses) =>
if err
return cb err
cb err, courses
create_assignment: (username, data, cb) ->
async.waterfall [
(wf_callback) ->
Course
.findById(data.course)
.exec wf_callback
(course, wf_callback) ->
assignment = new Assignment()
assignment.owner = username
assignment.title = data.title
assignment.date = data.date
assignment.details = data.details
assignment.save (err) ->
wf_callback err, course, assignment
(course, assignment, wf_callback) ->
course.assignments.addToSet assignment
course.save (err) ->
wf_callback err, course, assignment
], (err, course, assignment) ->
cb err, course, assignment
update_assignment: (username, assignment, cb) ->
# Pull the assignment from the current course,
# push it onto the new one, save it,
# and finally update the assignment fields.
async.waterfall [
(wf_callback) ->
Course.update {
owner: username
assignments: assignment._id
},
{
$pull: {assignments: assignment._id}
},
{},
(err, numAffected, raw) ->
# We don't want any of the other args
# passed through the waterfall
wf_callback err
(wf_callback) ->
Course
.findOne()
.where('owner', username)
.where('_id', assignment.course)
.exec wf_callback
(course, wf_callback) ->
course.assignments.addToSet assignment._id
course.save wf_callback
], (err) ->
if err
return cb err
Assignment.update {
owner: username
_id: assignment._id
},
{
title: assignment.title
date: assignment.date
details: assignment.details
done: assignment.done
archived: assignment.archived
},
{},
cb
delete_assignment: (username, assignment, cb) ->
Assignment
.where('owner', username)
.where('_id', assignment._id)
.remove cb
create_course: (username, data, cb) ->
course = new Course()
course.owner = username
course.title = data.title
course.teacher = data.teacher
course.save cb
update_course: (username, course, cb) ->
Course.update {
owner: username
_id: course._id
},
{
title: course.title
teacher: course.teacher
},
# Don't pass along numAffected and raw to
# the callback -- just return the err argument.
(err, numAffected, raw) ->
cb err
delete_course: (username, course, cb) ->
Course
.where('owner', username)
.where('_id', course._id)
.remove cb
# Used in testing to suppress log output.
suppress_logging: ->
L = -> # pass