-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
174 lines (134 loc) · 3.78 KB
/
app.js
File metadata and controls
174 lines (134 loc) · 3.78 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
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
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const date = require(__dirname + "/date.js");
const mongoose = require('mongoose');
const _ = require("lodash");
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
mongoose.connect('mongodb+srv://thomasjrhode:Z3ldaSpycr4b@todocluster.jgwykli.mongodb.net/?retryWrites=true&w=majority&appName=TodoCluster/todoDB');
const itemSchema = new mongoose.Schema ({
name: {
type: String,
required: [true, 'NAME REQUIRED']
}
});
const listSchema = new mongoose.Schema ({
name: {
type: String,
required: [true, 'NAME REQUIRED']
},
items: [itemSchema]
});
const Item = mongoose.model("item", itemSchema);
const List = mongoose.model("List", listSchema);
const step1 = new Item ({
name: "Step1",
});
const step2 = new Item ({
name: "Step2",
});
const step3 = new Item ({
name: "Step3",
});
const startingTasks = [step1, step2, step3];
//insert into collection
async function prePopulateTodo(){
const result = await Item.insertMany(startingTasks);
}
app.get("/", function(req, res) {
//list db collection
async function myTodo() {
const items = await Item.find({});
if (items.length === 0) {
console.log("repopulating defaults...")
prePopulateTodo();
//res.redirect("/")
}
const day = date.getDate();
res.render("list", {listTitle: "Today", newListItems: items});
}
myTodo();
});
app.get("/:customListName", function(req, res) {
const customListName = _.capitalize(req.params.customListName);
console.log("URL:" + customListName);
async function checkDupes() {
const existingList = await List.findOne({name: customListName});
if (existingList) {
console.log("list: " + customListName + " was found!");
res.render("list", {listTitle: existingList.name, newListItems: existingList.items});
}
else {
console.log("list: " + customListName + " was not found!");
const list = new List({
name: customListName,
items: startingTasks
});
list.save();
res.redirect('/' + customListName);
}
}
checkDupes();
});
//New item added
app.post("/", async(req, res) =>{
try {
const itemName = req.body.newItem;
const listName = req.body.list;
console.log("New item being added: " + req.body.newItem)
const newTask = new Item ({
name: itemName
});
if (listName === "Today"){
await newTask.save();
console.log('item saved')
res.redirect('/');
}
else {
const existingList = await List.findOne({name: listName});
if (existingList)
{
existingList.items.push(newTask);
existingList.save();
res.redirect("/" + listName);
}
}
}
catch(err) {
console.error("error saving item", err);
res.status(500).send("internal server error");
}
});
//check box ticked
app.post("/delete", async(req, res) =>{
try {
const checkedItemId = req.body.checkbox;
const listName = req.body.listName;
console.log("Deleting Item: " + checkedItemId + " from " + listName);
if (listName === "Today"){
await Item.deleteOne({ _id: checkedItemId});
console.log("Item deleted!");
res.redirect("/");
}
else {
const existingList = await List.findOneAndUpdate({name: listName}, {$pull: {items: {_id:checkedItemId}}});
if (existingList){
res.redirect("/" + listName);
}
}
}
catch(err) {
console.error("error deleting item", err);
res.status(500).send("internal server error");
}
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function() {
console.log("Server started on port 3000");
});