forked from innextinit/elearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteacher.js
75 lines (68 loc) · 2.21 KB
/
teacher.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
var mongoose = require("mongoose");
var fs = require("fs");
var multer = require("multer");
var bcryptjs = require('bcryptjs');
// Teacher Schema (this is the definition of the input the DB will take more like stating the head of table)
var TeacherSchema = new mongoose.Schema({
name: {
type: String,
index: true,
trim: true,
require: true,
},
address: [{
street_address: {type: String, trim: true, require: true},
city: {type: String, trim: true, require: true},
state: {type: String, trim: true, require: true},
zip: {type: Number, trim: true, require: true}
}],
username: {
type: String,
index: true,
trim: true,
require: true,
unique: true
},
email: {
type: String,
trim: true,
require: true,
unique: true
},
TeacherImg: {
data: Buffer, // using Buffer, which allows us to store our image as data in the form of arrays
contentType: String,
},
classes: [{
class_id:{type: [mongoose.Schema.Types.ObjectId]},
class_title:{type: String}
}],
admin: false,
});
// this is to convert this TeacherSchema in a usable model called Teacher
var Teacher = mongoose.model("Teacher", TeacherSchema);
module.exports = mongoose.model("Teacher", TeacherSchema); // then this export the model Teacher so it can be used outside this file.
module.exports.getTeacherByUsername = function(username, callback) {
var query = {username: username};
Teacher.findOne(query, callback).lean();
};
// newClass
module.exports.saveNewClass = function(newClass, callback){
teacher_username = newClass.teacher_username;
class_id = newClass.class_id;
class_title = newClass.class_title;
var query = {username: teacher_username}
Teacher.findOneAndUpdate(
query,
{$push: {"classes": {class_id: class_id, class_title: class_title}}},
{save: true, upsert: true},
callback
).lean();
};
// this is for when i am sending POST req to add new class to inculde this part for the classImg upload that is include.
// app.post('/api/photo', function(req, res){
// var newClass = new Class();
// newClass.classImg.data = fs.readFileSync(req.files.userPhoto.path);
// newClass.classImg.contentType = 'image/png';
// newClass.save();
// });