-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
151 lines (134 loc) · 3.87 KB
/
Copy pathapp.js
File metadata and controls
151 lines (134 loc) · 3.87 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
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const date = require(__dirname + "/date.js");
const mongoose = require("mongoose");
const lod=require("lodash"); //lodash is used to format route strings
const app = express();
app.set('view engine', 'ejs'); //ejs is the templating used
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));
mongoose.connect("mongodb://localhost:27017/listItemDB");
const itemSchema = { //each todo item will just have a string
name: String
}
const Item = mongoose.model("Item", itemSchema);
const newListScheme = { //has a name and an array of listItems which are basically modelled on the itemSchema
name: String,
listItems: [itemSchema]
}
const List = mongoose.model("list", newListScheme);
//the three objects below are added to the app by default
const defaultOne = {
name: "Buy Groceries"
}
const defaultTwo = {
name: "Make Food"
}
const defaultThree = {
name: "Eat Food"
}
const defaultList = [defaultOne, defaultTwo, defaultThree];
app.get("/", function(req, res) { //the home route
const day = date.getDate();
Item.find({}, function(err, itemList) {
if (itemList.length === 0) {
Item.insertMany(defaultList, function(err) { //the default items are inserted
if (err) {
console.log(err);
} else {
console.log("success");
}
});
res.redirect("/");
} else {
res.render("list", { //list.ejs is displayed with listTitle as the date today and listItems displayed below
listTitle: day,
newListItems: itemList
});
}
});
});
app.post("/", function(req, res) { //whenever a new list item is added
const day = date.getDate();
const itemdata = req.body.newItem;
const pageTitle = req.body.list;
console.log(pageTitle);
const item = new Item({
name: itemdata
});
if (pageTitle === day) {
item.save();
res.redirect("/");
} else {
List.findOne({
name: pageTitle
}, function(err, found) {
if (!err) {
console.log(item);
found.listItems.push(item);
found.save();
res.redirect("/" + pageTitle);
}
})
}
});
app.post("/delete", function(req, res) { //this is called whenever the checkbox is ticked
var ci = req.body.checkbox;
const day = date.getDate();
var pageTitle = req.body.listName;
if (pageTitle === day) {
Item.deleteOne({
_id: ci
}, function(err) {
if (err) {
console.log("error");
} else {
console.log("success");
res.redirect("/");
}
});
}else{
List.findOneAndUpdate({name:pageTitle},{$pull:{listItems:{_id:ci}}},function(err){ //this empties the item
if(!err){
res.redirect("/"+pageTitle);
}
})
}
});
app.get("/:newList", function(req, res) { //this a custom url list
const listName = lod.capitalize(req.params.newList); //captalizes the first letter of each new word
List.findOne({
name: listName
}, function(err, listFound) {
if (!err) {
if (listFound) {
res.render("list", {
listTitle: listName,
newListItems: listFound.listItems
});
} else {
const newList = new List({
name: listName,
listItems: defaultList
});
newList.save();
res.redirect("/" + listName);
}
}
});
});
app.get("/work", function(req, res) { //this is the worklist
res.render("list", {
listTitle: "Work List",
newListItems: workItems
});
});
app.get("/about", function(req, res) { //about list
res.render("about");
});
app.listen(process.env.PORT || 3000, function() { //this runs on heroku and local port
console.log("Server started on port 3000");
});