generated from lighthouse-labs/node-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
180 lines (148 loc) · 5.46 KB
/
server.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
// load .env data into process.env
require('dotenv').config();
// Imported Modules
const restaurants = require('./lib/database/restaurants');
const orders = require('./lib/database/orders');
const users = require('./lib/database/users');
const menu_items = require('./lib/database/menu_items');
// Web server config
const PORT = process.env.PORT || 8080;
const ENV = process.env.ENV || "development";
const express = require("express");
const bodyParser = require("body-parser");
const sass = require("node-sass-middleware");
const app = express();
const morgan = require('morgan');
const cookieSession = require('cookie-session');
const methodOverride = require('method-override');
// PG database client/connection setup
const { Pool } = require('pg');
const dbParams = require('./lib/database/db.js');
const db = new Pool(dbParams);
db.connect();
// Load the logger first so all (static) HTTP requests are logged to STDOUT
// 'dev' = Concise output colored by response status for development use.
// The :status token will be colored red for server error codes, yellow for client error codes, cyan for redirection codes, and uncolored for all other codes.
app.use(morgan('dev'));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/styles", sass({
src: __dirname + "/styles",
dest: __dirname + "/public/styles",
debug: true,
outputStyle: 'expanded'
}));
app.use(express.static("public"));
app.use(methodOverride('_method'));
/* Session Manager */
app.use(cookieSession({
httpOnly: false,
name: 'session',
keys: ['user_id', 'order_id', 'msg'],
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}));
// Separated Routes for each Resource
// Note: Feel free to replace the example routes below with your own
const usersRoutes = require("./routes/users");
const apiRoutes = require('./routes/apiRoutes');
const orderRoutes = require('./routes/orderRoutes');
const restaurant_owner_routes = require('./routes/restaurant_owner_routes');
// Mount all resource routes
// Note: Feel free to replace the example routes below with your own
app.use("/api/users", usersRoutes(db));
// Note: mount other resources here, using the same pattern above
/* /api/endpoints/ */
//Passes restaurants database as well as orders database.
app.use('/restaurant/owner', (req, res, next) => {
let user_id = req.session.user_id;
restaurants.findRestaurantOwnerId(user_id).then(restaurant => {
if (restaurant) {
next();
} else {
res.redirect('/');
}
})
}, restaurant_owner_routes(restaurants, orders));
app.post("/user/login", (req, res) => {
if (!req.body.email) {
res.status(400).json({ error: 'invalid request: no data in POST body' });
return;
}
if (!req.session.user_id) {
users.findUserId(req.body.email).then(user => {
if (!user.id) {
res.json(null);
} else {
req.session.user_id = user.id;
restaurants.findRestaurantOwnerId(user.id).then(restaurant => {
if (restaurant) {
res.json(restaurant.id);
} else {
res.json(0);
}
});
}
});
} else {
restaurants.findRestaurantOwnerId(req.session.user_id).then(restaurant => {
if (restaurant) {
res.json(restaurant.id);
} else {
res.json(0);
}
});
}
});
app.post('/user/new', (req, res) => {
//Insert users.
});
app.post('/user/logout', (req, res) => {
req.session.user_id = null;
res.redirect('/');
});
app.get('/', (req, res) => {
res.status(200);
restaurants.findAllRestaurants().then(all_restaurants => {
let allRestaurants = all_restaurants;
restaurants.findRestaurantIdByOwnerId(req.session.user_id).then(exist => {res.render('index', { title: 'Ritual', restaurants: allRestaurants, msg: req.session.msg || '' , user_id: req.session.user_id || 0, is_owner: exist.id});
}).catch(doesnt_exist => {
res.render('index', { title: 'Ritual', restaurants: allRestaurants, msg: req.session.msg || '' , user_id: req.session.user_id || 0, is_owner: 0});
});
});
});
app.get("/restaurants/:id", (req, res) => {
res.status(200);
// TODO: feed user_id - hardcoded 1 for now
orders.findActiveOrderId(req.session.user_id, req.params.id).then(order_id => {
if (order_id) {
req.session.order_id = order_id;
restaurants.findAllMenuItemsForRestaurant(req.params.id).then(menu_items => {
restaurants.findRestaurantById(req.params.id).then(restaurant => {
let allItems = menu_items;
res.render('restaurant', { restaurant: restaurant, menu_items: allItems, order_id: req.session.order_id });
});
});
}
}).catch(no_order_id => {
//If no order_id exist create one
// HARD CODED USER ID
orders.createOrder(req.session.user_id, req.params.id).then(order => {
// Sets session on GET request before page loads
req.session.order_id = order[0].id;
restaurants.findAllMenuItemsForRestaurant(req.params.id).then(menu_items => {
restaurants.findRestaurantById(req.params.id).then(restaurant => {
let allItems = menu_items;
res.render('restaurant', { restaurant: restaurant, menu_items: allItems, order_id: req.session.order_id });
});
});
});
});
});
app.use('/order', orderRoutes(db));
app.use('/api', apiRoutes(db));
// Home page
// Warning: avoid creating more routes in this file!
// Separate them into separate routes files (see above).
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`);
});