Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
115 lines (103 loc) · 2.76 KB
/
Copy pathapp.js
File metadata and controls
115 lines (103 loc) · 2.76 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
111
112
113
114
const express = require("express");
const mongoose = require("mongoose");
const _ = require("lodash");
const app = express();
app.use(require("body-parser").urlencoded({extended:true}));
app.use(express.static("public"));
app.set("view engine", "ejs");
//database connection
mongoose.connect("mongodb://localhost:27017/todo");
const schema = {
thing : String,
};
const SSchema = {
title: String,
listt: [schema]
}
const Item = mongoose.model("item", schema);
const List = mongoose.model("list",SSchema);
const item1 = new Item({
thing : "Welcome to Todo list!!"
});
const item2 = new Item({
thing : "Fill the box amd hit + button to add a new item"
});
const item3 = new Item({
thing : "click the checkbox button to delete the item"
});
//Item.insertMany([item1,item2,item3]);
// main server
app.get("/",function(req,res){
Item.find(function(err,results){
res.render("list",{Title:"Today",new_item:results});
});
});
//another custom list
app.get("/:cont",function(req,res){
//console.log(Item);
var heading=_.capitalize(req.params.cont);
List.findOne({title: heading},function(err, option){
if(!err){
if(option){
//console.log()
res.render("list",{Title: option.title, new_item: option.listt});
}
else{
const list1 =new List({
title: heading,
listt: [item1,item2,item3]
});
list1.save();
res.redirect("/"+req.params.cont);
}
}
else{
console.log(err);
}
});
//res.redirect("/"+req.params.cont);
});
// Adding item
app.post("/",function(req,res){
var heading = req.body.list;
var text= req.body.Addwork;
const itemm = new Item({
thing : text
});
if(heading==="Today"){
itemm.save();
res.redirect("/");
}
else{
List.findOne({title:heading},function(err,_list){
_list.listt.push(itemm);
_list.save();
res.redirect("/"+heading);
});
}
});
//deleting an item
app.post("/delete",function(req,res){
var heading = req.body.ListName;
var ItemId = req.body.checkBox;
if(heading==="Today"){
Item.deleteOne({_id: ItemId},function(err){
if(!err){
console.log("successfully deleted!");
res.redirect("/");
}
});
}
else{
List.findOneAndUpdate({title: heading},{$pull: {listt: {_id: ItemId}}},
function(err,result){
if(!err){
res.redirect("/"+heading);
}
});
}
});
//To start the server
app.listen(3000,function(req,res){
console.log("servers is set to port 3000");
});