-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
194 lines (131 loc) · 4.04 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
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
const express = require('express')
const bodyParser = require('body-parser')
const mondoDBconnection = require('./database/db')
const { modelNote } = require('./model/Notes')
const date = require('./date')
const list = require('./model/list')
const _ = require('lodash')
const app = express()
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static('public'))
app.set('view engine', 'ejs')
// const items = ["Buy Food", "Cook Food", "Eat Food"]
// const workItems = []
app.get("/:customListName", async (req, res) => {
const customListName = _.capitalize(req.params.customListName)
try {
const lista = await list.findOne({ name: customListName })
// console.log(lista)
res.render('list', {
listTitle: lista.name, newListItems: lista.items
})
} catch (error) {
// console.log('error')
res.render('list', {
listTitle: customListName, newListItems: null
})
}
})
app.post('/:customListName', async (req, res) => {
const customListName = _.capitalize(req.params.customListName)
const item = req.body.newItem
const lista = await list.findOne({ name: customListName })
// console.log(lista)
if (!lista) {
try {
const note = await list.create({ name: customListName, items: [{ name: item }] })
res.redirect('/' + customListName)
} catch (error) {
console.log(error)
}
} else {
try {
const note = await list.findOneAndUpdate(
{ name: customListName },
{ $push: { items: { name: item } } })
// console.log(note)
// res.status(200).json({
// note
// })
res.redirect('/' + customListName)
} catch (error) {
res.status(404).json({ error })
}
}
})
app.get("/", async (req, res) => {
try {
const note = await modelNote.find()
// console.log(note)
res.render('list', {
listTitle: date.getDate(), newListItems: note
})
} catch (error) {
// res.status(500).json({ "error msg": error })
// console.log('error')
res.render('list', {
listTitle: date.getDate(), newListItems: null
})
}
})
app.post('/', async (req, res) => {
// if (req.body.botton === "WorkList"){
// var item = req.body.newItem
// console.log(item)
// workItems.push(item)
// // res.render('list', { newListItem: req.body.newItem})
// res.redirect('/work')
// }else{
const name = req.body.newItem
console.log(name)
try {
const note = await modelNote.create({ name })
// res.status(200).json({
// note
// })
} catch (error) {
res.status(404).json({ error })
}
// res.render('list', { newListItem: req.body.newItem})
res.redirect('/')
// }
})
app.get('/delete/:id', async (req, res) => {
console.log(req.params.id)
console.log(req.query.list)
const customListName = req.query.list
const _id = req.params.id
if (req.query.list == date.getDate()) {
modelNote.findOneAndDelete(req.params.id, (err) => {
if (!err) {
console.log("succesfully deleted checked item")
}
})
res.redirect('/')
} else {
try {
await list.findOneAndUpdate(
{ name: customListName },
{ $pull: { items: { _id } } })
res.redirect('/' + customListName)
} catch (error) {
res.json(error)
throw error
}
}
})
app.get("/work", (req, res) => {
res.render("list", { listTitle: "WorkList", newListItems: workItems })
})
app.post("/work", (req, res) => {
const item = req.body.newItem
workItems.push(item)
res.redirect('/work')
})
app.get("/about", (req, res) => {
res.render("about")
})
app.listen(3000, () => {
console.log("server started on port 3000")
mondoDBconnection()
})