-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
162 lines (129 loc) · 3.69 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
// IMPORTS
const http = require("http");
const express = require("express");
const session = require("express-session");
const passport = require("passport");
const flash = require("connect-flash");
const path = require("path");
//
const { isAuthenticated } = require("./middlewares/authentication");
// DATABASE
let { bids, products, users } = require("./database");
// ROUTES
const indexRoute = require(path.join(__dirname, "routes", "index"));
// INIT PASSPORT
const InitPassport = require(path.join(__dirname, "config", "InitPassport"));
InitPassport(passport);
// INIT SERVER
const server = express();
const httpServer = http.Server(server);
const io = require("socket.io")(httpServer, { cors: { origin: "*" } });
// MIDDLEWARES & SETTINGS
server.set("view engine", "ejs");
server.use(flash());
server.use(express.static(path.join(__dirname, "static")));
server.use(express.urlencoded({ extended: false }));
server.use(
session({
secret: process.env.SESSION_SECRET_KEY || "$tracker_SECRET_KEY",
resave: false,
saveUninitialized: false,
}),
);
server.use(passport.initialize());
server.use(passport.session());
// ROUTES
server.get("/", (req, res) => {
res.redirect("/products");
});
server.get("/database", (req, res) => {
let database = { bids, products, users };
res.json(database);
});
server.get("/products/:id/bids", (req, res) => {
let { id } = req.params;
let product = products.find((product) => product.id === parseInt(id));
if (product == null) {
req.flash("errorMessage", "Product Not Found...");
return res.redirect("/products");
}
let productsBids = bids
.filter((bid) => bid.productId === product.id)
.map((bid) => {
let user = users.find((user) => user.username === bid.userId);
let object = {
user,
price: bid.price,
};
return object;
})
.reverse();
res.send(productsBids);
});
server.post("/products/:id/bids", isAuthenticated, (req, res) => {
const { id } = req.params;
const { raiseTo } = req.body;
let productIndex = products.findIndex(
(product) => product.id === parseInt(id),
);
if (productIndex == -1) {
req.flash("errorMessage", "Product Not Found...");
return res.redirect("/products");
}
let product = products[productIndex];
if (product.soldTo != null) {
req.flash("errorMessage", "Product already sold...");
return res.redirect("/products");
}
if (new Date(product.endDateTime).getTime() < Date.now()) {
req.flash("errorMessage", "Bidding over...");
return res.redirect("/products");
}
let productBids = bids.filter((bid) => bid.productId === parseInt(id));
bigger = true;
if (productBids.length !== 0) {
for (let bid of bids) {
if (bid.price >= raiseTo) {
bigger = false;
break;
}
}
} else if (product.startingPrice > parseFloat(raiseTo)) {
bigger = false;
}
if (bigger == false) {
req.flash("errorMessage", "You must offer more...");
return res.redirect(`/products/${id}`);
}
let bid = {
id: Date.now(),
productId: product.id,
userId: req.user.username,
datetime: Date.now(),
price: parseFloat(raiseTo),
};
bids.push(bid);
let productsBids = bids
.filter((bid) => bid.productId === product.id)
.map((bid) => {
let user = users.find((user) => user.username === bid.userId);
let object = {
user,
price: bid.price,
};
return object;
})
.reverse();
io.emit(`bid:${id}`, productsBids);
res.redirect(`/products/${id}`);
});
server.use("/", indexRoute);
server.use((req, res) => {
res.render("error", { errorMessage: "404 Not Found!" });
});
// LISTENING TO PORT
const PORT = process.env.PORT || 5000;
httpServer.listen(PORT, () => console.log(`Server started on port ${PORT}...`));
io.on("connection", (socket) => {
console.log(`user connected: ${socket.id}`);
});