-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
253 lines (212 loc) · 6.35 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import express from "express";
import bodyParser from "body-parser";
import mongoose from "mongoose";
import _ from "lodash";
const app=express();
const port=process.env.PORT|3000;
app.set('view engine', 'ejs');
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended:true}));
//connect mongoDB with mongoose
import {} from 'dotenv/config';
import { connectDB } from './DB/mongo.js';
connectDB();
//create schema
const itemSchema= new mongoose.Schema({
name: String
});
//create model
const Item= mongoose.model("Item", itemSchema);
//creating items
const item1=new Item({
name: "Welcome to your TodoList!"
});
const item2=new Item({
name: "Hit + button to add new item."
});
const item3=new Item({
name: "<-- Hit this to delete an item."
});
const defaultItems=[item1, item2, item3];
const listSchema={
name: String,
desc: String,
items: [itemSchema]
};
const List=mongoose.model("List", listSchema);
app.get("/",(req,res)=>{
async function findAllLists(){
try{
const result=await List.find();
//console.log(result[0].name);
res.render("home.ejs",{AllLists: result});
}
catch(error){
console.log(error);
}
}
findAllLists();
});
app.get("/about",(req,res)=>{
res.render("about.ejs");
});
app.get("/today",(req,res)=>{
//find all items in Item collection
async function findAllItems(){
try{
const itemList= await Item.find({});
//console.log(itemList);
//inserting all items into items collection
async function insertItems(defaultItems){
try{
const result= await Item.insertMany(defaultItems);
console.log(`Succesfully inserted ${result.length} document(s).`);
}
catch(error){
console.log(error);
}
}
if(itemList.length===0){
insertItems(defaultItems);
res.redirect("/today");
}
else{
res.render("today",{listTitle: "Today",tasks: itemList});
}
}
catch(error){
console.log(error);
}
}
findAllItems();
});
app.get("/:customListName", (req,res) => {
const customListName= _.capitalize(req.params.customListName);
async function findList(){
try{
const foundList=await List.findOne({name: customListName});
//console.log(foundList);
if(!foundList){
//console.log("Doesn't Exists!");
//create a new list
const list=new List({
name: customListName,
items: defaultItems
});
list.save();
res.redirect("/"+ customListName);
}
else{
//console.log("Exists!");
//show existing list
res.render("today", {listTitle: foundList.name,tasks: foundList.items})
}
}
catch(error){
console.log(error);
}
}
findList();
});
app.post("/addTask",(req,res)=>{
const listName=req.body.list;
const taskName=req.body.task;
const newItem=new Item({
name: taskName
});
if(listName==="Today"){
newItem.save();
res.redirect("/today");
}
else{
async function findList(){
try{
const foundList=await List.findOne({name: listName});
foundList.items.push(newItem);
foundList.save();
res.redirect("/"+ listName);
}
catch(error){
console.log(error);
}
};
findList();
}
});
app.post("/delete", (req,res)=> {
const checkedItemId=req.body.checkbox;
const listName=req.body.listName;
//console.log(checkedItemId);
if(listName==="Today"){
async function deleteItem(){
try{
const result= await Item.deleteOne({_id: checkedItemId});
console.log(`Succefully deleted ${result.deletedCount} document(s).`);
res.redirect("/today");
}
catch(error){
console.log(error);
}
}
deleteItem();
}
else{
async function findAndUpdate(){
try{
const result=await List.findOneAndUpdate({name: listName}, {$pull: {items: {_id: checkedItemId}}});
console.log(`Succefully deleted 1 document(s) from ${listName}.`);
res.redirect("/"+ listName);
}
catch(error){
console.log(error);
}
}
findAndUpdate();
}
});
app.post("/addList", (req,res) => {
const listName=_.capitalize(req.body.list);
const listDesc=req.body.desc;
//console.log(listName);
async function checkIfListExists(){
try{
const result=await List.find({name: listName});
//console.log(result.length);
if(result.length===0 && listName!="Today"){
//console.log("Doesn't exists!");
const list=new List({
name: listName,
desc: listDesc,
// items: defaultItems
});
list.save();
res.redirect("/");
}
else{
res.redirect("/");
}
}
catch(error){
console.log(error);
}
}
checkIfListExists();
});
app.post("/deleteList", (req,res) => {
const listName=_.capitalize(req.body.list);
//console.log(listName);
async function deleteList(){
try{
const result=await List.deleteOne({name: listName});
console.log(`Succefully deleted ${listName} list.`);
res.redirect("/")
}
catch(error){
console.log(error);
}
}
deleteList();
});
app.listen(port,()=>{
console.log(`Server running on port ${port}.`);
});