First commit
[qomet.git] / entities / course.js
1 const db = require("../utils/database");
2
3 const CourseEntity =
4 {
5 /*
6 * Structure:
7 * _id: BSON id
8 * uid: prof ID
9 * code: varchar
10 * description: varchar
11 * password: monitoring password hash
12 * students: array of
13 * number: student number
14 * forename: varchar
15 * name: varchar
16 * group: integer
17 */
18
19 getByUser: function(uid, callback)
20 {
21 db.courses.find(
22 { uid: uid },
23 callback
24 );
25 },
26
27 getById: function(cid, callback)
28 {
29 db.courses.findOne(
30 { _id: cid },
31 callback
32 );
33 },
34
35 getByPath: function(uid, code, callback)
36 {
37 db.courses.findOne(
38 {
39 $and: [
40 { uid: uid },
41 { code: code },
42 ]
43 },
44 callback
45 );
46 },
47
48 insert: function(uid, code, description, cb)
49 {
50 db.courses.insert(
51 {
52 uid: uid,
53 code: code,
54 description: description,
55 students: [ ],
56 },
57 cb);
58 },
59
60 setStudents: function(cid, students, cb)
61 {
62 db.courses.update(
63 { _id: cid },
64 { $set: { students: students } },
65 cb
66 );
67 },
68
69 // Note: return { students: { ... } }, pointing on the requested row
70 getStudent: function(cid, number, cb)
71 {
72 db.courses.findOne(
73 { _id: cid },
74 {
75 _id: 0,
76 students: { $elemMatch: {number: number} }
77 },
78 cb
79 );
80 },
81
82 setPassword: function(cid, pwd, cb)
83 {
84 db.courses.update(
85 { _id: cid },
86 { $set: { password: pwd } },
87 cb
88 );
89 },
90
91 remove: function(cid, cb)
92 {
93 db.courses.remove(
94 { _id: cid },
95 cb
96 );
97 },
98 }
99
100 module.exports = CourseEntity;