-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (140 loc) · 5.13 KB
/
index.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
149
150
151
152
153
154
155
156
157
158
159
160
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import axios from 'axios';
const app = express();
const worldTimeAPIURL = process.env.API_URL;
const dbURL = process.env.DB_URL;
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended : true}));
mongoose.set('strictQuery',true);
mongoose.connect(dbURL,{ useNewUrlParser: true, useUnifiedTopology: true}).then(db => {console.log("Database connected");}).catch(error => console.log("Could not connect to mongo db " + error));
const todoSchema = { name : String };
const TodoList = mongoose.model('todoList',todoSchema);
const task1 = new TodoList( {name : 'Welcome to tacklify todo app.'} );
const task2 = new TodoList( {name : 'Click + button to add a new task.'} );
const task3 = new TodoList( {name : 'Click - button to delete a completed task.'} );
const defaultTasks = [task1,task2,task3];
const customTodoSchema = {
name : String,
xitems : [todoSchema]
}
const CustomTodoList = mongoose.model('customList',customTodoSchema);
app.get('/home',async (req,res)=>{
try{
const localTime = await axios.get(worldTimeAPIURL);
const currentTime = localTime.data.datetime.slice(11,19) + `${localTime.data.datetime.slice(11,13) < 12 ? ' AM' : ' PM'}`;
const tasks = await TodoList.find({});
if(tasks.length === 0){
TodoList.insertMany(defaultTasks);
res.redirect('/');
}
else{
res.render('index.ejs',{
title : 'Welcome',
time: {
dd: localTime.data.datetime.slice(8,10),
mm: months[parseInt(localTime.data.datetime.slice(5,7)) - 1],
yyyy: localTime.data.datetime.slice(0,4),
time: currentTime
},
items : tasks,
});
}
} catch(err){
console.log(err);
}
});
app.get('/',async (req,res)=>{
try{
const localTime = await axios.get(worldTimeAPIURL);
const currentTime = localTime.data.datetime.slice(11,19) + `${localTime.data.datetime.slice(11,13) < 12 ? ' AM' : ' PM'}`;
const tasks = await TodoList.find({});
if(tasks.length === 0){
TodoList.insertMany(defaultTasks);
res.redirect('/');
}
else{
res.render('signup.ejs',{
title : 'SignUp',
time: {
dd: localTime.data.datetime.slice(8,10),
mm: months[parseInt(localTime.data.datetime.slice(5,7)) - 1],
yyyy: localTime.data.datetime.slice(0,4),
time: currentTime
},
items : tasks,
});
}
} catch(err){
console.log(err);
}
});
app.get('/:customListName',async (req,res)=>{
try{
const localTime = await axios.get(worldTimeAPIURL);
const currentTime = localTime.data.datetime.slice(11,19) + `${localTime.data.datetime.slice(11,13) < 12 ? ' AM' : ' PM'}`;
const customListName = req.params.customListName.charAt(0).toUpperCase() + req.params.customListName.toLowerCase().slice(1);
const cList = await CustomTodoList.findOne({name : customListName});
if(!cList){
const list = new CustomTodoList({
name : customListName,
xitems : defaultTasks
});
list.save();
res.redirect('/' + customListName);
}
else{
res.render('index.ejs',{
title : cList.name,
time: {
dd: localTime.data.datetime.slice(8,10),
mm: months[parseInt(localTime.data.datetime.slice(5,7)) - 1],
yyyy: localTime.data.datetime.slice(0,4),
time: currentTime
},
items: cList.xitems,
});
}
}catch(err){
console.log(err);
}
});
app.post('/', async (req,res)=>{
const taskName = req.body.task;
const listName = req.body.list;
const newTask = new TodoList({ name: taskName });
if(listName === 'Welcome'){
newTask.save();
res.redirect('/');
} else{
const newItem = await CustomTodoList.findOne({name : listName});
newItem.xitems.push(newTask);
newItem.save();
res.redirect('/'+ listName);
}
});
app.post('/remove',async (req,res)=>{
try{
const checkedTaskId = req.body.checkbox;
const listname = req.body.listName;
if(listname !== 'Welcome'){
await CustomTodoList.findOneAndUpdate({name : listname}, {$pull : { xitems : {_id : checkedTaskId}}});
res.redirect('/' + listname);
} else{
await TodoList.findByIdAndRemove({_id : checkedTaskId});
res.redirect('/');
}
}
catch(err){
console.log(err);
}
});
let port = process.env.PORT;
if(port == null || port == "") { port = 3000 };
app.listen(port,()=>{
console.log(`Todo App Rendering Successful!`);
})
const months = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug","Sept", "Oct", "Nov", "Dec"
];