-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
157 lines (129 loc) · 3.7 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const express = require("express")
const bodyParser = require("body-parser")
const mongoose = require("mongoose")
const lodash = require("lodash")
const app = express()
app.use(bodyParser.urlencoded ({extended: true}))
// Connect CSS and Image
app.use(express.static("public"))
// Set Up for ejs file system ...
app.set("view engine", "ejs")
// Connect the Mongodb Local servier
mongoose.connect("mongodb+srv://Praveen:[email protected]/todolist")
// mongoose.connect("mongodb://localhost:27017/todolist")
// Schema
const todoSchema = {
name:{
type: String,
}
}
// Schema for list
const listSchema = {
name: String,
list_todo: [todoSchema]
}
// Create a model
const ToDo = new mongoose.model("ToDo", todoSchema)
// Create model for list
const List = new mongoose.model("List", listSchema)
// Document
const item1 = new ToDo({
name: "Welcome to ToDo-List"
})
const item2 = new ToDo({
name: "Type the input box and click the + button"
})
const item3 = new ToDo({
name: "<------ Hit this to delete an item"
})
// Array
const default_items = [item1, item2, item3]
// Main Root
app.get("/", function(req, res){
// Find
ToDo.find({}).then((foundItems) =>{
// Conditon for Insert default values
if (foundItems.length === 0){
ToDo.insertMany(default_items).then(() =>{
console.log("Insert Default Items!!!")
})
// Redirect the servier to rendering values
res.redirect("/")
}else{
res.render("list", {titles: "Today", todo: foundItems})
}
}).catch((Error) =>{
console.log(Error)
})
})
// Get the user input
app.post("/", function(req, res){
// Get the input value
let todo_item = req.body.todo_input
let list_name = req.body.list
// Create Model
const item = new ToDo({
name: todo_item
})
// Condition
if (list_name === "Today"){
item.save()
res.redirect("/")
}else{
List.findOne({name: list_name}).then((foundlist) =>{
foundlist.list_todo.push(item)
foundlist.save()
res.redirect("/" + list_name)
})
}
})
// Root for delete
app.post("/delete", function(req, res){
const checkIditem = req.body.checkbox
const button_route_value = req.body.hidden_btn
// Condition for Deleteing
if (button_route_value === "Today"){
ToDo.deleteOne({_id: checkIditem}).then(() =>{
console.log("Deleted Successful")
res.redirect("/")
}).catch((Error) =>{
console.log(Error)
})
}else{
List.findOneAndUpdate({name: button_route_value},{$pull: {list_todo: {_id: checkIditem}}}).then(() =>{
console.log("Updated")
res.redirect("/" + button_route_value)
}).catch((Error) =>{
console.log(Error)
})
}
})
// Finish Route
app.get("/:routeName", function(req, res){
// Get Route Name
const routename = lodash.capitalize(req.params.routeName)
// Condition
List.findOne({name :routename}).then((result) =>{
if (result.name === routename ){
res.render("list", {
titles: result.name,
todo: result.list_todo
})
}
}).catch(() =>{
// Creat a new document
const list = new List({
name: routename,
list_todo: default_items
})
list.save().then(() =>{
console.log("New route list has been saved")
res.redirect("/" + routename)
})
// Redirect
})
})
// Local Seriver
app.listen(3000, function(){
console.log("Server 3000 has been connecting....")
})