-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.js
More file actions
277 lines (263 loc) · 8.27 KB
/
Copy pathserver.js
File metadata and controls
277 lines (263 loc) · 8.27 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const app = require("./backend/app");
const debug = require("debug")("node-angular");
const http = require("http");
const pool = require('./backend/database');
const normalizePort = val => {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
};
const onError = error => {
if (error.syscall !== "listen") {
throw error;
}
const bind = typeof port === "string" ? "pipe " + port : "port " + port;
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
};
const onListening = () => {
const addr = server.address();
const bind = typeof port === "string" ? "pipe " + port : "port " + port;
debug("Listening on " + bind);
};
const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
const server = http.createServer(app);
const io = require('socket.io')(server);
io.on("connection", socket => {
// Log whenever a user connects
console.log("user connected");
// Log whenever a client disconnects from our websocket server
socket.on("disconnect", function() {
console.log("user disconnected");
});
// When we receive a 'message' event from our client, print out
// the contents of that message and then echo it back to our client
// using `io.emit()`
socket.on("message", message => {
console.log("Message Received: " + message);
io.emit("message", { type: "new-message", text: message });
});
socket.on("auction_create",auction=>
{
let Auction=JSON.parse(auction);
delete Auction.type;
console.log(JSON.stringify(Auction));
let sql="INSERT INTO Auction set ?";
pool.query(
sql,[Auction],(err,rows,fields)=>
{
if(err)
console.log(err);
else
{
io.emit({type:"auction_create","content":Auction});
}
}
)
})
socket.on("auction_by_userid",aucget=>
{
let sql="Select * from Auction where Created_by=?";
console.log(aucget);
pool.query(sql,[JSON.parse(aucget).content],(error,rows,fields)=>
{
if(error)
console.log(error);
else
{
console.log(rows);
socket.send({type:"auction_by_userid","content":rows});
}
})
})
socket.on("product_create",product=>
{
let Product=JSON.parse(product);
delete Product.type;
console.log(JSON.stringify(Product));
let sql="INSERT INTO Product SET ?";
pool.query(sql,[Product],(err,rows,fields)=>
{
if(err)
console.log(error);
else
{
Product["Product_Id"]=rows['insertId'];
console.log(Product);
socket.send({ type:"product_create",content:Product});
}
})
})
socket.on("user_products",req=>{
let sql="Select * from Product where User_Id=?";
console.log(req);
pool.query(sql,[JSON.parse(req).content],(error,rows,fields)=>{
if(error)
console.log(error);
else
{console.log(rows)
socket.send({type:"user_products","content":rows});}
})
})
socket.on("bid",bid => {
let Bid=JSON.parse(bid);
delete Bid.type;
console.log(JSON.stringify(Bid));
let sql = "Insert into Bid Set ? ; Select Username from Users Where User_Id="+Bid.User_Id;
pool.query(sql,[Bid],(err,rows,fields)=>{
if(err)
console.log(err);
else{
io.emit("message",{ type:"bid",content:{"Product_Id":Bid.Product_Id,"Username":rows[1][0].Username,"Base_price":Bid.Bid_amount}});
}
})
})
socket.on("auction_by_id",req=>{
let sql="Select Auction_Id,Title,Auction_description,Username,Auction.Created_at,Start_time,End_time,hasStarted,hasEnded from Auction,Users WHERE Auction_Id=? and Created_by=User_Id;";
pool.query(sql,[JSON.parse(req).content],(error,rows,fields)=>{
if(error)
console.log(error);
else
socket.send({type:"auction_by_id","content":rows});
})
})
socket.on("auction",req=>{
req=JSON.parse(req);
let sql="Select Auction_Id,Title,Auction_description,Username,Auction.Created_at,Start_time,End_time,hasStarted,hasEnded from Auction,Users WHERE Created_by=User_Id and Auction_Id NOT IN (SELECT Auction_Id FROM Auction_users where Auction_users.User_Id="+req["content"]+")";
pool.query(sql,(error,rows,fields)=>{
if(error)
console.log(error);
else
socket.send({type:"auction","content":rows});
})
})
socket.on("auction_user",req=>{
req=JSON.parse(req);
let sql="Select Auction_Id,Title,Auction_description,Username,Auction.Created_at,Start_time,End_time from Auction,Users WHERE Created_by=User_Id and Auction_Id IN (SELECT Auction_Id FROM Auction_users where Auction_users.User_Id="+req["content"]+")";
pool.query(sql,(error,rows,fields)=>{
if(error)
console.log(error);
else
socket.send({type:"auction_user","content":rows});
})
})
socket.on("auction_join",auction=>
{
let Auction=JSON.parse(auction);
delete Auction.type;
console.log(JSON.stringify(Auction));
let sql="INSERT INTO Auction_users Values("+Auction["User_Id"]+","+ Auction["Auction_Id"]+")";
console.log(sql);
pool.query(
sql,(err,rows,fields)=>
{
if(err){
console.log(err);
}
else{
socket.send({type:"auction_join","content":Auction});
}
}
)
});
socket.on("auction_leave",auction=>
{
let Auction=JSON.parse(auction);
delete Auction.type;
console.log(JSON.stringify(Auction));
let sql="DELETE FROM Auction_users Where User_Id="+Auction["User_Id"]+" and Auction_Id = "+ Auction["Auction_Id"];
console.log(sql);
pool.query(
sql,(err,rows,fields)=>
{
if(err){
console.log(err);
}
else{
socket.send({type:"auction_leave","content":Auction});
}
}
)
})
socket.on("auction_products",req=>{
let sql="Select * from Product where Product_Id IN (Select Product_Id from Auction_product Where Auction_Id =?)";
pool.query(sql,[JSON.parse(req).content],(error,rows,fields)=>{
if(error)
console.log(error);
else
socket.send({type:"auction_products","content":rows});
})
})
socket.on("delete_user_products",req=>{
let sql="delete from Product where Product_Id=?";
pool.query(sql,[JSON.parse(req).content],(error,rows,fields)=>{
if(error){
console.log(error);
}
else{
socket.send({type:"delete_user_products",content:JSON.parse(req)});
}
})
})
socket.on("start_auction",req=>{
let sql="Update Auction set hasStarted=1 where Auction_Id=?";
pool.query(sql,[JSON.parse(req).content],(error,rows,fields)=>{
if(error)
{
console.log(error);
}
else{
io.emit("message",JSON.parse(req));
}
})
})
socket.on("end_auction",req=>{
let sql="Update Auction set hasEnded=1 where Auction_Id=?;"
+"REPLACE INTO Product_sold SELECT DISTINCT `Product_Id`,`User_Id`, `Bid_amount` FROM `Bid` WHERE `Bid_amount` in ( SELECT MAX(`Bid_amount`) FROM `Bid` GROUP BY `Product_Id`);"
+"UPDATE `Product` INNER JOIN (SELECT DISTINCT `User_Id`, `Product_Id`, `Bid_amount` FROM `Bid` WHERE `Bid_amount` in ( SELECT MAX(`Bid_amount`) FROM `Bid` GROUP BY `Product_Id`)) AS `Bd` ON `Bd`.`Product_Id`=`Product`.`Product_Id` SET Product.User_Id=Bd.User_Id,Product.Base_price=Bd.Bid_amount";
pool.query(sql,[JSON.parse(req).content],(error,rows,fields)=>{
if(error)
{
console.log(error);
}
else{
io.emit("message",JSON.parse(req));
}
})
})
socket.on("selected_products",auction=>{
let Auction=JSON.parse(auction);
delete Auction.type;
console.log(JSON.stringify(Auction));
let sql="Replace into Auction_product (Auction_Id,Product_Id) values ?";
pool.query(sql,[Auction],(error,rows,fields)=>{
if(error)
console.log(error);
else{
console.log(rows);
socket.send({type:"selected_products","content":rows});
}
})
})
});
server.on("error", onError);
server.on("listening", onListening);
server.listen(port);