-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
199 lines (174 loc) · 4.91 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"use strict";
require("dotenv").config();
const PORT = process.env.PORT || 8080;
const ENV = process.env.ENV || "development";
const express = require("express");
const bodyParser = require("body-parser");
const cookieSession = require("cookie-session");
const sass = require("node-sass-middleware");
const app = express();
const knexConfig = require("./knexfile");
const knex = require("knex")(knexConfig[ENV]);
const morgan = require("morgan");
const knexLogger = require("knex-logger");
// Setup for Twilio.
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const twilio = require("twilio")(accountSid, authToken);
// Seperated Routes for each Resource
const usersRoutes = require("./routes/users");
// 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"));
// Log knex SQL queries to STDOUT as well
app.use(knexLogger(knex));
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(
cookieSession({
name: "session",
keys: ["key1", "key2"]
})
);
app.use(express.static("public"));
// Mount all resource routes
app.use("/api/users", usersRoutes(knex));
// Function used to
// Home page:
app.get("/", (req, res) => {
const userId = req.session.user_id;
res.render("index");
});
// Menu Page:
app.get("/dishes", (req, res) => {
const userId = req.session.user_id;
knex("dishes").asCallback((err, rows) => {
if (err) console.error(err);
res.render("menu", { dishes: rows });
});
});
// Orders Page:
app.get("/orders", (req, res) => {
const userId = req.session.user_id;
knex("orders").asCallback((err, rows) => {
if (err) console.error(err);
res.render("menu", { orders: rows });
});
});
// Handle request to add dish to the user's cart.
app.post("/cart", (req, res) => {
const userId = req.session.user_id;
const { dishId, qty } = req.body;
knex("cart")
.insert({
user_id: userId,
dish_id: dishId,
quantity: qty
})
.then(() => {
// Prevents hanging refresh.
res.sendStatus(200);
});
});
// knex("cart")
// .select("*")
// .where("dish_id", dishId)
// .andWhere('user_id', userId)
// .then(rows => {
// // To check if dish is already in a user's cart.
// if (rows.length < 1) {
// console.log(rows);
// knex("cart").insert({
// user_id: userId,
// dish_id: dishId,
// quantity: qty
// })
// } else {
// let newQty = rows[0].quantity + parseInt(qty);
// console.log(newQty);
// knex("cart")
// .where('dish_id', dishId)
// .andWhere('user_id', userId)
// .update({quantity: newQty})
// }
// Login route.
app.get("/login/:id", (req, res) => {
req.session.user_id = req.params.id;
res.redirect("/");
});
// Logout route.
app.get("/logout/:id", (req, res) => {
req.session = null;
res.redirect("/");
});
// View contents of user's cart.
app.get("/cart", (req, res) => {
const userId = req.session.user_id;
knex("dishes")
.join("cart", "dishes.id", "=", "cart.dish_id")
.select("*")
.where("user_id", userId)
.asCallback((err, result) => {
let templateVars = { cart: result };
res.render("orders", templateVars);
});
});
// Handle request to submit order and send SMS confirmation.
app.post("/orders", (req, res) => {
const userId = req.session.user_id;
const {
firstName,
lastName,
streetNumber,
streetName,
city,
province,
postalCode,
phoneNumber
} = req.body;
return Promise.all([
// Send an SMS to the client about their order.
twilio.messages.create({
body: `Thank you, ${firstName}! Your order is being processed and your delivery will be completed shortly.`,
from: "+14388060140",
to: "+15149289639"
}),
// Empty the user's cart.
knex("cart")
.where("user_id", userId)
.del()
]).then(() => {
res.send(firstName);
});
});
// Delete an item from a user's cart.
app.post("/cart/:id/delete", (req, res) => {
const userId = req.session.user_id;
const cartId = parseInt(req.body.cartId);
knex("cart")
.where("id", cartId)
.del()
.then(result => {
knex("dishes")
.join("cart", "dishes.id", "=", "cart.dish_id")
.select("*")
.where("user_id", userId)
.asCallback((err, result) => {
let templateVars = { cart: result };
res.render("orders", templateVars);
});
});
});
app.listen(PORT, () => {
console.log("Example app listening on port " + PORT);
});