-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
110 lines (92 loc) · 2.69 KB
/
Copy pathapp.js
File metadata and controls
110 lines (92 loc) · 2.69 KB
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
const express=require("express");
const bodyParser=require("body-parser");
const app=express();
const date=require(__dirname + "/day.js")
const items=[];
const workItems=[];
const mongoose=require("mongoose")
const host='0.0.0.0'
const port=process.env.PORT||3000
main().catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://0.0.0.0:27017/todolistDB');
//await mongoose.connect('mongodb+srv://muthu98:muthu98@mutz.lcgql2l.mongodb.net/todolistDB');
}
const itemsSchema= new mongoose.Schema({
name:String,
})
const Item = mongoose.model("Item",itemsSchema);
const listSchema={
name:String,
items:[itemsSchema]
}
const List = mongoose.model("List",listSchema)
const item1 = new Item({
name:"Hi!Welcome to the TodoList",
})
const item2 = new Item({
name:"Add the items that you want to save",
})
const defaultItems=[item1,item2]
Item.insertMany(defaultItems)
app.set("view engine","ejs")
app.use(bodyParser.urlencoded({extended:true}))
app.use(express.static("public"));
//Get route that sends the word hello when user acceses it
app.get("/",function(req,res){
const day = date.getDate();
Item.find({}).then(foundItems=>{
if (foundItems.length===0){
console.log(foundItems);
res.redirect("/")
}else{
res.render("list",{listTitle:day ,newListItems:foundItems});
}
})
})
//Redirect to home route when a new item is added since it is only possible to render once
app.post("/",function(req,res){
console.log(req.body)
const currentItem = req.body.newItem;
const tempItem = new Item({
name:currentItem
});
tempItem.save();
res.redirect("/")
})
app.get("/work",function(req,res){
res.render("list",{listTitle:"Work",newListItems:workItems})
})
app.get("/:customListName",function(req,res){
const customListName=req.params.customListName
List.findOne({name:customListName}).then(function(err,foundList){
if(!err){
if(!foundList){
console.log("Does not exist")
}else{
console.log("Exists")
}
}
});
const list = new List({
name:customListName,
items:defaultItems
})
list.save();
res.render("list",{listTitle:customListName,newListItems:defaultItems})
})
app.post("/work",function(req,res){
let item = req.body.newItem;
workItems.push(item);
res.redirect("/work")
})
app.post("/delete",function(req,res){
const id=req.body.checkbox
Item.findByIdAndDelete(id).then(()=>{
res.redirect("/")
}).catch(err=>console.log(err))
})
app.get("/about",function(req,res){
res.render("about");
})
app.listen(port,host);