-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (113 loc) · 2.72 KB
/
index.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
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
dotenv.config();
app.set("view engine", "ejs");
app.use("/static", express.static("public"));
app.use(express.urlencoded({ extended: true }));
// mongo db connection parameters
const connectionParams = {
useNewUrlParser: true,
};
// the function to add it in the sirted manner
function sorted(arr) {
for (var i = 0; i < arr.length; i++) {
for (var j = i + 1; j < arr.length; j++) {
if (arr[i].priority > arr[j].priority) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
// DATABSE CONNECTION STARTED
mongoose
.connect(process.env.DB_CONNECT, connectionParams)
.then(() => {
console.log("connected to the database");
app.listen(3000, () => {
console.log("server is up and running");
});
})
.catch((err) => {
console.error(`error in connecting to the database. n${err}`);
});
// DATABASE CONNECTION DONE
// importing models for our database
const TodoTask = require("./models/ToDoTask");
app.get("/", (req, res) => {
TodoTask.find({})
.then(function (tasks) {
console.log(tasks);
sorted(tasks);
res.render("todo.ejs", { todoTasks: tasks });
})
.catch((err) => {
console.log(err);
});
});
app.post("/", async (req, res) => {
const todoTask = new TodoTask({
content: req.body.content,
priority: req.body.priority,
});
try {
await todoTask.save();
res.redirect("/");
} catch (err) {
res.redirect("/");
}
});
//update the tasks
app
.route("/edit/:id")
.get((req, res) => {
const id = req.params.id;
TodoTask.find({}).then(function (tasks) {
res.render("edit.ejs", { todoTasks: tasks, idTask: id });
});
})
.post((req, res) => {
const id = req.params.id;
console.log(req.body.content);
console.log(req.body.priority);
TodoTask.findByIdAndUpdate(
id,
{ content: req.body.content[1], priority: req.body.priority[1] },
{ new: true }
)
.then((result) => {
console.log(result);
res.redirect("/");
})
.catch((err) => {
console.log(err);
});
});
// deleting
app
.route("/remove/:id")
.get((req, res) => {
const id = req.params.id;
TodoTask.findByIdAndRemove(id)
.then((result) => {
console.log(result);
res.redirect("/");
})
.catch((err) => {
console.log(err);
});
})
.post((req, res) => {
const id = req.body.checkbox;
TodoTask.findByIdAndRemove(id)
.then((result) => {
console.log(result);
res.redirect("/");
})
.catch((err) => {
console.log(err);
});
});