-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (72 loc) · 2.53 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
const express = require("express");
const bodyParser = require("body-parser");
const _ = require("lodash");
const ejs = require("ejs");
const mongoose = require("mongoose");
//App Preparation________________________________________________________
const app = express();
//Use static files
app.use(express.static("public"));
//Use EJS
app.set("view engine","ejs");
//Get content from forms
app.use(bodyParser.urlencoded({extended:true}));
//Database Preparation________________________________________________________
mongoose.connect("mongodb://localhost:27017/todolistDB");
const itemSchema = new mongoose.Schema({
name: String
});
const listSchema = new mongoose.Schema({
name: String,
items: [itemSchema]
});
const Item = mongoose.model("Item", itemSchema);
const List = mongoose.model("List", listSchema);
app.get("/", function(req,res){
res.redirect("/My ToDos");
});
//_______________________________________________________________________
//Creating lists through the sent name
app.get("/:listName", function(req,res){
const listName= _.capitalize(req.params.listName);
List.findOne({name: listName},function(err,foundList){
if(!err){
//If the list doesn't exist, then add it
if(!foundList){
const newList = new List({name:listName, items:[]});
newList.save();
res.redirect('/'+listName);
//Uses the view engine to render the page (from the views folder)
}else{
//Uses the view engine to render the page (from the views folder)
res.render('list',{listTitle:foundList.name, toDos: foundList.items});
}
}
})
});
app.post("/", function(req,res){
const listName= _.capitalize(req.body.listName);
const newTodo = new Item({name: req.body.newToDo});
List.findOne({name:listName},function(err,foundList){
if(!err){
foundList.items.push(newTodo);
foundList.save();
res.redirect("/"+listName);
}
})
});
app.post("/delete", function(req,res){
const itemID = mongoose.Types.ObjectId(req.body.checkbox);
const listName= _.capitalize(req.body.listName);
List.findOneAndUpdate({name:listName},
//Pull/Delete from items array the item with the correct id
{$pull:{ "items": {_id: itemID}}},
function(err,foundList){
if(!err){
res.redirect("/"+listName);
}
})
});
app.listen(process.env.PORT||3000, function(){
console.log("Server started");
});