X-Git-Url: https://git.auder.net/?p=qomet.git;a=blobdiff_plain;f=models%2Fcourse.js;h=631bab9016d765b32de5f09d2883f6c4695bf899;hp=53419c1f29e2c1b869de4fe6af059554e060ed3b;hb=43828378be054cf3604b753e8d9ab24af911188f;hpb=7a7dc732599b358b25b770cfc27036f4b403d1b4 diff --git a/models/course.js b/models/course.js index 53419c1..631bab9 100644 --- a/models/course.js +++ b/models/course.js @@ -1,17 +1,116 @@ -const CourseEntity = require("../entities/course"); -const UserEntity = require("../entities/user"); -const AssessmentEntity = require("../entities/assessment"); +const UserModel = require("../models/user"); +const AssessmentModel = require("../models/assessment"); +const db = require("../utils/database"); const CourseModel = { + /* + * Structure: + * _id: BSON id + * uid: prof ID + * code: varchar + * description: varchar + * password: monitoring password hash + * students: array of + * number: student number + * name: varchar + * group: integer + */ + + ////////////////// + // BASIC FUNCTIONS + + getByUser: function(uid, callback) + { + db.courses.find( + { uid: uid }, + callback + ); + }, + + getById: function(cid, callback) + { + db.courses.findOne( + { _id: cid }, + callback + ); + }, + + getByPath: function(uid, code, callback) + { + db.courses.findOne( + { + $and: [ + { uid: uid }, + { code: code }, + ] + }, + callback + ); + }, + + insert: function(uid, code, description, cb) + { + db.courses.insert( + { + uid: uid, + code: code, + description: description, + students: [ ], + }, + cb); + }, + + setStudents: function(cid, students, cb) + { + db.courses.update( + { _id: cid }, + { $set: { students: students } }, + cb + ); + }, + + // Note: return { students: { ... } }, pointing on the requested row + getStudent: function(cid, number, cb) + { + db.courses.findOne( + { _id: cid }, + { + _id: 0, + students: { $elemMatch: {number: number} } + }, + cb + ); + }, + + setPassword: function(cid, pwd, cb) + { + db.courses.update( + { _id: cid }, + { $set: { password: pwd } }, + cb + ); + }, + + remove: function(cid, cb) + { + db.courses.remove( + { _id: cid }, + cb + ); + }, + + ///////////////////// + // ADVANCED FUNCTIONS + getByInitials: function(initials, callback) { - UserEntity.getByInitials(initials, (err,user) => { + UserModel.getByInitials(initials, (err,user) => { if (!!err || !user) callback(err, []); else { - CourseEntity.getByUser(user._id, (err2,courseArray) => { + CourseModel.getByUser(user._id, (err2,courseArray) => { callback(err2, courseArray); }); } @@ -20,12 +119,12 @@ const CourseModel = getByRefs: function(initials, code, callback) { - UserEntity.getByInitials(initials, (err,user) => { + UserModel.getByInitials(initials, (err,user) => { if (!!err || !user) callback(err, []); else { - CourseEntity.getByPath(user._id, code, (err2,course) => { + CourseModel.getByPath(user._id, code, (err2,course) => { callback(err2, course); }); } @@ -35,37 +134,37 @@ const CourseModel = importStudents: function(uid, cid, students, cb) { // 1) check if uid == course uid - CourseEntity.getById(cid, (err,course) => { + CourseModel.getById(cid, (err,course) => { if (!!err || !course || !course.uid.equals(uid)) return cb({errmsg:"Not your course"},{}); // 2) Set students - CourseEntity.setStudents(cid, students, cb); + CourseModel.setStudents(cid, students, cb); }); }, setPassword: function(uid, cid, pwd, cb) { // 1) check if uid == course uid - CourseEntity.getById(cid, (err,course) => { + CourseModel.getById(cid, (err,course) => { if (!!err || !course || !course.uid.equals(uid)) return cb({errmsg:"Not your course"},{}); // 2) Insert new student (overwrite if number already exists) - CourseEntity.setPassword(cid, pwd, cb); + CourseModel.setPassword(cid, pwd, cb); }); }, remove: function(uid, cid, cb) { // 1) check if uid == course uid - CourseEntity.getById(cid, (err,course) => { + CourseModel.getById(cid, (err,course) => { if (!!err || !course || !course.uid.equals(uid)) return cb({errmsg:"Not your course"},{}); // 2) remove all associated assessments - AssessmentEntity.removeGroup(cid, (err2,ret) => { + AssessmentModel.removeGroup(cid, (err2,ret) => { if (!!err) return cb(err,{}); // 3) remove course (with its students) - CourseEntity.remove(cid, cb); + CourseModel.remove(cid, cb); }); }); },