-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservor.js
More file actions
85 lines (77 loc) · 2.75 KB
/
Copy pathservor.js
File metadata and controls
85 lines (77 loc) · 2.75 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
const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// const cors = require('cors');
// app.use(cors());
// app.use(express.json());
// Connect to MySQL
const mysql = require('mysql');
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'Chuchu123',
database: 'courierservicedb'
});
db.connect((err) => {
if (err) throw err;
console.log('Connected to the database.');
});
app.get('/submitBooking',(req,res)=>{
res.send("Connected to Booking");
})
function getRandomStatus() {
const statuses = ['processing', 'shipped', 'out-of-delivered'];
return statuses[Math.floor(Math.random() * statuses.length)];
}
app.post('/submitBooking',(req,res)=>{
console.log(req.body);
console.log(req.body.email)
console.log("__________________")
console.log("all data")
console.log(typeof(req.body.orderId))
res.send("yayayay")
let a=getRandomStatus();
console.log(typeof(expectedDeliveryDate))
console.log("asdasdasd")
console.log(typeof(a));
console.log(a);
console.log(typeof(req.body.expectedDeliveryDateFinal))
const sqlInsert = "INSERT INTO bookings (orderId, fullName, email, phoneNumber, parcelItem, departureDate, deliveryAddress, pickupAddress, trackingId, expectedDeliveryDate,statusDelivery) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
db.query(sqlInsert, [
req.body.orderId,
req.body.fullName,
req.body.email,
req.body.phoneNumber,
req.body.parcelItem,
req.body.departureDate,
req.body.deliveryAddress,
req.body.pickupAddress,
req.body.trackingId,
req.body.expectedDeliveryDate,
req.body.statusDelivery,
]);
})
app.get('/getOrderDetails', (req, res) => {
const { orderId, trackingId } = req.query;
let sqlQuery = '';
if (orderId &&!trackingId) {
sqlQuery = "SELECT * FROM bookings WHERE orderId =?";
} else if (!orderId && trackingId) {
sqlQuery = "SELECT * FROM bookings WHERE trackingId =?";
} else {
res.status(400).send('Please provide either an order ID or a tracking ID.');
return;
}
db.query(sqlQuery, [orderId || trackingId], (error, results) => {
if (error) throw error;
if (results.length > 0) {
res.json(results[0]);
} else {
res.status(404).send('No order found with the given ID.');
}
});
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));