-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
60 lines (53 loc) · 1.67 KB
/
app.js
File metadata and controls
60 lines (53 loc) · 1.67 KB
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
import express from "express";
import path from "path";
import ejs from "ejs";
import TicketService from "./src/pairtest/TicketService.js";
import TicketTypeRequest from "./src/pairtest/lib/TicketTypeRequest.js";
import {
TICKET_TYPE_ADULT,
TICKET_TYPE_INFANT,
TICKET_TYPE_CHILD,
} from "./config/constants.js";
const app = express();
global.__dirname = path.resolve();
app.set("views", __dirname + "/views");
app.set("view engine", "html");
app.engine("html", ejs.renderFile);
/** Serve static files from the public dir */
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.get("/", (req, res) => {
if (req.query.message) {
const message = req.query.message;
res.render("index", { message: message });
return;
}
if (req.query.error) {
const error = req.query.error;
res.render("index", { error: error });
return;
}
res.render("index");
});
app.post("/submit", (req, res) => {
const accountId = +req.body.accountId;
const adultTickets = +req.body.adultTickets;
const childTickets = +req.body.childTickets;
const infantTickets = +req.body.infantTickets;
const ticketService = new TicketService();
try {
const message = ticketService.purchaseTickets(
accountId,
new TicketTypeRequest(TICKET_TYPE_ADULT, adultTickets),
new TicketTypeRequest(TICKET_TYPE_CHILD, childTickets),
new TicketTypeRequest(TICKET_TYPE_INFANT, infantTickets)
);
res.redirect("/?message=" + encodeURIComponent(message));
} catch (err) {
const error = err.message;
res.redirect("/?error=" + encodeURIComponent(error));
}
});
app.listen(3000, () => {
console.log("Server listening on port 3000");
});