-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
160 lines (142 loc) · 3.74 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
/*
* Title : TodoList MEN
* Author : Kean Duque
* Description : Todo App MEN
*/
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const _ = require("lodash");
const date = require(__dirname + "/public/js/date.js");
const { connectToDB } = require(__dirname + "/public/db.js");
const { Todo, todoSchema, Page, pageSchema } = require("./models/todo");
// init app & middleware
const app = express();
const PORT = process.env.PORT || 3000;
mongoose.set("strictQuery", false);
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
// connect db
connectToDB()
.then(() => {
app.listen(PORT, () => {
console.log(`Listening on PORT ${PORT}`);
});
})
.catch((err) => console.log(err));
const defaultTodos = [
{ name: "Welcome to my TodoList!" },
{ name: "Press the (+) button to add new Todo" },
{ name: "Press Checkbox to Delete Todo" },
];
app.get("/", async (req, res) => {
const day = date.getDate();
await Todo.find({}).then((items) => {
if (items.length === 0) {
InsertTodoMany(defaultTodos);
res.redirect("/");
} else {
Page.find({}).then((pages) => {
res.render("list", {
listTitle: day,
newListItem: items,
navigation: pages,
});
});
}
});
});
app.get("/:TodoPage", async (req, res) => {
const todoTitle = _.capitalize(req.params.TodoPage);
await Page.findOne({ name: todoTitle })
.then((page) => {
if (!page) {
console.log("Page Not Exist, preparing to add..");
const page = new Page({
name: todoTitle,
todos: defaultTodos,
})
.save()
.then(() => console.log("New Page Succesfully Added!"))
.catch((err) => console.log(err));
res.redirect(`/${todoTitle}`);
} else {
Page.find({}).then((pages) => {
console.log(`${page.name} Page Exist`);
res.render("page", {
pageTitle: page.name,
pageContentTodo: page.todos,
navigation: pages,
});
});
}
})
.catch((err) => console.log(err));
});
app.get("/about", (req, res) => {
res.render("about");
});
app.post("/", async (req, res) => {
const itemName = req.body.todoItem;
const day = date.getDate().split(" ")[0];
const listTitle = req.body.list.split(" ")[0];
const item = new Todo({
name: itemName,
});
if (listTitle === day) {
await item.save();
console.log(item.name + " Successfully Added...");
res.redirect("/");
} else {
await Page.findOne({ name: listTitle })
.then((pageFound) => {
pageFound.todos.push(item);
pageFound.save();
console.log(item.name + " Successfully Added...");
res.redirect(`/${listTitle}`);
})
.catch((err) => console.log(err));
}
});
app.post("/delete", async (req, res) => {
const itemId = req.body.checkbox;
const day = date.getDate().split(" ")[0];
const pageName = req.body.pageTitle.split(" ")[0];
if (pageName === day) {
await DeleteTodo({ _id: itemId });
res.redirect("/");
} else {
await Page.findOneAndUpdate(
{ name: pageName },
{ $pull: { todos: { _id: itemId } } }
)
.then((result) => {
console.log("Successfully Deleted...");
res.redirect(`/${pageName}`);
})
.catch((err) => console.log(err));
}
});
async function InsertTodoMany(list) {
await Todo.insertMany(list)
.then(() => {
console.log("datas successfully inserted...");
mongoose.disconnect();
})
.catch((err) => console.log(err));
}
async function InsertTodo(list) {
await Todo(list)
.save()
.then(() => console.log("New Todo Succesfully Added!"))
.catch((err) => console.log(err));
}
async function DeleteTodo(list) {
await Todo.findByIdAndRemove(list)
.then(() => {
console.log("Successfully Deleted");
})
.catch((err) => console.log(err));
}