-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
148 lines (128 loc) · 4.86 KB
/
app.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
mongoose = require('mongoose');
Course = require('./models/courses.js')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(express.static(__dirname + '/public'));
app.set('views', './public');
app.set('view engine', 'ejs');
// mongoose.connect("mongodb://localhost/yotei-1",{useFindAndModify: false});
mongoose.connect("mongodb+srv://karan:karanaman123@yotei-1.twybd.azure.mongodb.net/yotei-1-1?retryWrites=true&w=majority",{dbName: 'yotei-1-1', useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false})
.then( () => {
console.log('Connection to the Atlas Cluster is successful!')
})
.catch( (err) => console.error(err));
// Homepage
app.get('/',async (req,res)=>{
Course.find({all_deadlines: {$not: {$size: 0}}},function(err,data){
if(err) {console.log(err);}
if(data==undefined || data==[]){res.render('index.ejs',{data: {}, tasks: {}});}
else {
all_tasks = []
for(var i=0;i<data.length;i++){
for(var j=0;j<data[i].all_deadlines.length;j++){
all_tasks.push(data[i].all_deadlines[j]);
}
}
all_tasks = all_tasks.sort(function(a,b){
return (new Date(a.deadline)-new Date(b.deadline));
});
recent_deadlines = []
for(var i=0;i<all_tasks.length;i++){
if(all_tasks[i].completed==false)
recent_deadlines.push(all_tasks[i])
if(recent_deadlines.length==3)
break
}
all_tasks = all_tasks.sort(function(a,b){
return (new Date(b.completedOn)-new Date(a.completedOn));
});
recent_completed = []
for(var i=0;i<all_tasks.length;i++){
if(all_tasks[i].completed==true)
recent_completed.push(all_tasks[i]);
if(recent_completed.length==3)
break;
}
// recent_completed = recent_completed.sort(function(a,b){
// return (new Date(b.completedOn)-new Date(a.completedOn));
// });
// console.log(recent_completed)
// all_tasks = all_tasks.sort(function(a,b){
// return (new Date(a.deadline)-new Date(b.deadline));
// });
res.render('index.ejs',{data: data, tasks: all_tasks/*, recent_deadlines: recent_deadlines*/, recent_completed: recent_completed});
}
});
});
// Add a task
app.post('/',(req,res)=>{
// console.log(req.body)
var date = new Date(req.body.date.split('-').reverse().join('-'));
date = date.toDateString();
var type = 'Homework';
if(req.body.options=='assn'){type='Assignment';}
else if (req.body.options=='project') {
type="Project";
}
else if(req.body.options=='tut'){
type="Tutorial";
}
Course.findOne({coursename: req.body.coursename},function(err,data){
// console.log(data);
if(data){
Course.updateOne({coursename: req.body.coursename},{ $push: { all_deadlines: {"createdOn": (new Date()).toDateString(), "deadline": date, "type": type, "description": req.body.descr, "coursename": req.body.coursename, "completed": false} } },function(err,data_1){
if(err)
console.log(err);
res.redirect('/');
});
}
else {
Course.create({coursename: req.body.coursename, all_deadlines:[{"createdOn": (new Date()).toDateString(), "deadline": date, "type": type, "description": req.body.descr, "coursename": req.body.coursename, "completed": false}]},(err,data_2)=>{
if(err){
console.log(err);
}
res.redirect('/');
});
}
});
});
// Mark the task complete
app.post('/task/completed/:courseid/:taskid',function(req,res){
Course.findOneAndUpdate({_id: req.params.courseid},{},(err,data)=>{
if(err)
console.log(err);
console.log(data)
for(var i=0;i<data.all_deadlines.length;i++){
if(data.all_deadlines[i]._id==req.params.taskid){
data.all_deadlines[i].completed = true;
console.log('Status changed');
data.all_deadlines[i].completedOn = (new Date()).toDateString();
break;
}
}
data.save();
res.redirect('/');
})
})
// Delete the task
app.post('/task/delete/:courseid/:taskid',function(req,res){
// console.log('courseid:'+req.params.courseid);
// console.log('taskid: '+req.params.taskid);
Course.findOneAndUpdate({_id: req.params.courseid},{/*$pullAll: {all_deadlines: [req.params.taskid]}*/},function(err,data){
if(err)
console.log(err);
for(var i=0;i<data.all_deadlines.length;i++){
if(data.all_deadlines[i]._id==req.params.taskid){
data.all_deadlines.splice(i,1);
break;
}
}
data.save();
res.redirect('/');
});
});
port = process.env.PORT || 3000
app.listen(port,()=>{console.log('Server started at '+port)});