X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=models%2Fcourse.js;h=748ab6c33f7c32ea62728b824a33012dc2fb79d3;hb=HEAD;hp=53419c1f29e2c1b869de4fe6af059554e060ed3b;hpb=e99c53fb3be56eb4c685dd061eef0e5b5bf22b73;p=qomet.git diff --git a/models/course.js b/models/course.js index 53419c1..748ab6c 100644 --- a/models/course.js +++ b/models/course.js @@ -1,17 +1,115 @@ -const CourseEntity = require("../entities/course"); -const UserEntity = require("../entities/user"); -const AssessmentEntity = require("../entities/assessment"); +const UserModel = require("../models/user"); +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 +118,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,38 +133,41 @@ 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) => { - if (!!err) - return cb(err,{}); - // 3) remove course (with its students) - CourseEntity.remove(cid, cb); - }); + // 2) remove all associated evaluations + db.evaluations.remove( + { cid: cid }, + (err2,ret) => { + if (!!err) + return cb(err,{}); + // 3) remove course (with its students) + CourseModel.remove(cid, cb); + } + ); }); }, }