-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
134 lines (121 loc) · 3.16 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
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const date = require(__dirname + "/date.js");
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/toDoListDB",{useNewUrlParser : true});
const _ = require("lodash");
app.use(bodyParser.urlencoded({extended : true}));
app.set("view engine","ejs");
app.use(express.static("public"));
const itemSchema = new mongoose.Schema({
name : {
type : String,
required : [true,"Please! provide a name !"]
}
});
const Item = mongoose.model("item",itemSchema);
const listSchema = new mongoose.Schema({
name : String,
listItems : [itemSchema]
});
const List = mongoose.model("list",listSchema);
const item1 = new Item({
name : "Welcome !! To your TODOList"
});
const item2 = new Item({
name : "->-> click on + to add items"
});
const item3 = new Item({
name : "<-<- check that box to delete an item"
});
const defaultItems = [item1,item2,item3];
app.get("/",function(req,res){
// let day = date.getDate();
// console.log(date.getDay());
Item.find({},function(err,foundItems){
if(err)
console.log(err);
else if(foundItems.length === 0){
Item.insertMany(defaultItems , function(err){
if(err)
console.log(err);
else
console.log("Added default items successfully");
});
res.redirect("/");
}
else {
res.render("list" ,{
listTitle : "Today",
listItem : foundItems
});
}
});
});
app.post("/",function(req,res){
const nit = req.body.newTodo;
const whchList = req.body.button;
const it = new Item({
name : nit
});
if(whchList === "Today"){
it.save();
res.redirect("/");
}
else{
List.findOne({name : whchList} , function(err,foundRes){
foundRes.listItems.push(it);
foundRes.save();
res.redirect("/"+whchList);
})
}
});
app.post("/delete",function(req,res){
const delId = req.body.chkb;
const listName = req.body.listName;
if(listName === "Today"){
Item.findByIdAndRemove(delId,function(err){
if(! err)
console.log("Succsesfully deleted");
});
res.redirect("/");
}
else{
List.findOneAndUpdate({name : listName} , {$pull : { listItems : { _id : delId}}}, function(err,resFound){
if(! err)
res.redirect("/"+listName);
});
}
});
app.get("/:place",function(req,res){
var pl = _.capitalize(req.params.place);
List.findOne({name : pl},function(err,resFound){
if(err)
console.log(err);
else{
if(resFound){
console.log("Result exists already");
res.render("list",{
listTitle : resFound.name,
listItem : resFound.listItems
});
}
else{
const list = new List({
name : pl,
listItems : defaultItems
});
list.save();
console.log("New List saved");
res.redirect("/"+pl);
}
}
});
});
app.get("/about",function(req,res){
res.render("about");
});
app.listen("3000",function(){
console.log("Port 3000 has started");
});