-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistance.js
114 lines (101 loc) · 2.24 KB
/
persistance.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
const models = require('./models')
const summarization = require('./summarization.js')
const Course = models.Course
const Student = models.Student
const Txn = models.Txn
const path = require('path')
const url = require('url')
const init = function(uri, cb){
models.init(uri, function(err){
if(err){
console.log("Models initialization failed")
cb(err)
}else{
console.log("Models initialized..")
cb()
}
})
}
const saveCourse = function(course, cb){
let c = Course.create(course)
c.save().then(function(doc){
cb(null, doc._id)
}, function(err){
cb(err)
})
}
const getCourse = function(course, cb){
Course.findOne(course).then(function(doc){
cb(null, doc)
}, function(err){
cb(err)
})
}
const getCourses = function(course, cb){
Course.find(course, {sort: '-startDate'}).then(function(docs){
cb(null, docs)
}, function(err){
cb(err)
})
}
const getStudents = function(student, cb){
Student.find(student, {sort: 'name'}).then(function(docs){
cb(null, docs)
}, function(err){
cb(err)
})
}
const saveStudent = function(student, cb){
let s = Student.create(student)
s.save().then(function(doc){
cb(null, doc._id)
}, function(err){
cb(err)
})
}
const addTxn = function(student, txn, cb){
student.txns.push(txn);
student.save().then(function(doc){
cb(null,student._id)
}, function(err){
cb(err)
})
}
const saveTxn = function(txn, cb){
let t = Txn.create(txn)
Student.findOne({_id: txn.studentID}).then(function(doc){
addTxn(doc, t, cb)
}, function(err){
cb(err)
})
}
const getTxns = function(student, cb){
Student.findOne(student).then(function(doc){
cb(null, doc.txns)
}, function(err){
cb(err)
})
}
// TODO: we should have a test covering this whole flow
const getCourseSummary = function(course, cb){
Student.find({courseID: course._id}, {sort: 'name'}).then(function(students){
let chits = summarization.summarize(students);
cb(null, chits)
}, function(err){
cb(err)
})
// Student.find({courseID: course._id}, {sort: 'name'}).then(summarization.summarize, function(err){
// cb(err)
// })
}
module.exports = {
init,
saveCourse,
getCourse,
getCourses,
getStudents,
saveStudent,
saveTxn,
getTxns,
getCourseSummary
}