-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
121 lines (93 loc) · 2.69 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
//Setting up the app
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var methodOverride = require("method-override");
var port = process.env.PORT;
app.set ("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({extended: true}));
mongoose.connect("mongodb://Spajk:[email protected]:63164/ralawproject");
app.use(methodOverride("_method"));
mongoose.set('useFindAndModify', false);
//Setting up the database data schema
var contactSchema = new mongoose.Schema
({ name: String,
email: String,
number: Number
})
var Contact = mongoose.model("Contact", contactSchema);
//Contact routes
app.get("/", function (req, res){
res.redirect("/kontakty");
});
app.get("/kontakty", function (req, res){
Contact.find({}, function (err,allContacts){
if(err){console.log(err);}
else{res.render("new", {contacts:allContacts});
}
});
});
//New contact
app.post ("/kontakty" , function (req, res){
var name = req.body.name;
var email = req.body.email;
var number = req.body.number;
var newContact ={name: name, email: email, number: number};
Contact.create(newContact, function(err,newlyCreated){
if(err){
console.log(err);
}
else {
res.redirect("/kontakty");
}
});
});
// Update contact
app.get("/kontakty/:id/edit", function(req, res){
Contact.findById(req.params.id, function(err, foundContact){
if(err){
res.redirect("/kontakty");
} else {
res.render("update", {contact: foundContact});
}
});
});
app.put("/kontakty/:id", function(req, res){
// find and update the correct campground
Contact.findByIdAndUpdate(req.params.id, req.body.contact, function(err, updatedContact){
if(err){
res.redirect("/kontakty");
} else {
//redirect somewhere(show page)
res.redirect("/kontakty");
}
});
});
//Destroy contact
app.get("/kontakty/:id/delete", function(req, res){
Contact.findById(req.params.id, function(err, foundContact){
if(err){
res.redirect("/kontakty");
} else {
res.render("delete", {contact: foundContact});
}
});
});
app.delete("/kontakty/:id", function(req, res){
Contact.findByIdAndRemove(req.params.id, function(err){
if(err){
res.redirect("/kontakty");
} else {
res.redirect("/kontakty");
}
});
});
//Quiz routes
app.get("/kviz", function(req, res){
res.render("kviz");
});
app.listen(port, function (){
console.log("server started");
});