-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
186 lines (165 loc) · 4.56 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
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
175
176
177
178
179
180
181
182
183
184
185
186
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var http = require('http');
var methodOverride = require('method-override');
var fs = require('fs');
var EJS = require('ejs');
var express = require('express');
var session = require('express-session');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('makersBnB', ['adverts']);
var app = express();
var sess;
app.set('views', __dirname + '/views/');
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use(express.static(path.join(__dirname + '/public')));
app.use(express.static(path.join(__dirname + '/lib')));
app.use(session({secret: 'newsession'}));
app.use(methodOverride('_method'));
app.set('port', process.env.PORT || 3000);
//
app.listen(3000, function() {
console.log("Server started on Port 3000...");
});
app.get('/', function(req, res) {
sess=req.session;
if(sess.email) {
console.log("You are logged in");
}
var message = ((sess.email ? ("Welcome, " +sess.username) : "Please log in or sign up"));
db.adverts.find(function (err, docs) {
if(err) {
console.log(err);
}
res.render('index', {
adverts: docs,
welcomeMessage: message
});
});
});
app.get('/users/new', function(req, res) {
sess=req.session;
var message = ((sess.email ? ("Welcome, " +sess.username) : "Please log in or sign up"));
db.adverts.find(function (err, docs) {
if(err) {
console.log(err);
}
res.render('users/new', {
adverts: docs,
welcomeMessage: message
});
});
});
app.post('/users/new', function(req, res) {
sess=req.session;
console.log("Signed up with email: " + sess.email);
if(req.body.password == req.body.passwordComfirmation) {
var newUser = {
name: req.body.name,
username: req.body.username,
email: req.body.email,
password: req.body.password,
};
db.users.insert(newUser, function(err, result){
if(err){
console.log(err);
}
sess.email = req.body.email;
sess.username = req.body.username;
res.redirect('/');
});
}
});
app.get('/sessions/new', function(req, res) {
sess=req.session;
var message = ((sess.email ? ("Welcome, " +sess.username) : "Please log in or sign up"));
db.adverts.find(function (err, docs) {
if(err) {
console.log(err);
}
res.render('sessions/new', {
adverts: docs,
welcomeMessage: message
});
});
});
app.post('/sessions/new', function(req, res){
console.log("Logged in with email:" + req.body.email);
sess=req.session;
db.users.findOne({email: req.body.email}, function(err, foundUser){
if(foundUser == null){
res.redirect('/users/new');
}
else if (err){
console.log(err);
res.redirect('/users/new');
}
else if(req.body.password === foundUser.password){
sess.username = foundUser.username;
sess.email=req.body.email;
sess.password=req.body.password;
res.redirect('/');
}
else {
res.redirect('/sessions/new');
}
});
});
app.delete('/sessions', function(req, res) {
sess=req.session;
sess.destroy(function(err) {
if(err) {
console.log(err);
} else {
console.log("You have logged out");
res.redirect('/');
}
});
});
app.post('/book', function (req, res) {
db.adverts.update({_id:mongojs.ObjectId(req.body.bookBtn)}, {$set: {booked:true}});
res.redirect('/');
});
app.get('/new-advert', function(req, res) {
res.render('advert/new');
});
app.post('/new-advert', function(req, res) {
sess=req.session;
var user;
console.log("Adding advert for" + sess.email);
db.users.findOne({email: sess.email}, function(err, foundUser) {
if(err){
console.log(err);
}
user = foundUser;
console.log("User ID hexstring" + user._id.toHexString());
var newAd = {
userId : user._id.toHexString(),
ownerUsername: user.username,
name: req.body.advertName,
description: req.body.advertDescription,
price: req.body.advertPrice,
startDate: req.body.advertStartDate,
endDate: req.body.advertEndDate,
booked: false
};
db.adverts.insert(newAd, function(err, result){
if(err){
console.log(err);
}
console.log("Added advert to user"+ user.email + result);
res.redirect('/');
});
});
});
//
// module.exports = app;
// if (!module.parent) {
// http.createServer(app).listen(process.env.PORT, function(){
// console.log("Server listening on port " + app.get('port'));
// });
// }