-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
175 lines (147 loc) · 6.02 KB
/
Copy pathindex.js
File metadata and controls
175 lines (147 loc) · 6.02 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
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql2');
const cors = require('cors');
const path = require('path');
require('dotenv').config();
const app = express();
app.use(cors());
app.use(express.static('public'));
app.use(express.json());
app.use(bodyParser.json());
const mysqlConnection = {
host: 'localhost', //'bestroutedatabse.cnw4kgmcmwfk.eu-north-1.rds.amazonaws.com',
user: 'root', //'admin',
password: '7460', //'9108107460',
database: 'BestRoute', //'BestRoute',
port: '3306',
};
app.get('/bus/', (req, res) => {
const connection = mysql.createConnection(mysqlConnection);
const query = 'SELECT * FROM Bus';
connection.connect((err) => {
if (err) {
res.status(500).json({ error: 'Database connection failed' });
} else {
connection.query(query, (err, results) => {
if (err) {
res.status(500).json({ error: 'Query execution failed' });
} else {
console.log(results);
res.status(200).json(results);
}
connection.end();
});
}
});
});
app.post('/bus/add', (req, res) => {
const { route_name, starting_point, destination, description } = req.body;
// Validate the input
if (!route_name || !starting_point || !destination || !description) {
return res.status(400).json({ error: 'All fields are required (route_name, starting_point, destination, description)' });
}
const connection = mysql.createConnection(mysqlConnection);
const query = 'INSERT INTO Bus (route_name, starting_point, destination, description) VALUES (?, ?, ?, ?)';
connection.connect((err) => {
if (err) {
return res.status(500).json({ error: 'Database connection failed' });
} else {
connection.query(query, [route_name, starting_point, destination, description], (err, results) => {
if (err) {
return res.status(500).json({ error: 'Query execution failed' });
} else {
return res.status(201).json({ message: 'Bus added successfully!', results });
}
connection.end();
});
}
});
});
app.get('/bus/search', (req, res) => {
const { starting_point, destination } = req.query;
if (!starting_point || !destination) {
return res.status(400).json({ error: 'Starting Point and Destination are required' });
}
const connection = mysql.createConnection(mysqlConnection);
const query = 'SELECT * FROM Bus WHERE starting_point = ? AND destination = ?';
connection.connect((err) => {
if (err) {
res.status(500).json({ error: 'Database connection failed' });
} else {
connection.query(query, [starting_point, destination], (err, results) => {
if (err) {
res.status(500).json({ error: 'Query execution failed' });
} else {
res.status(200).json(results);
}
connection.end();
});
}
});
});
app.put('/bus/update/:id', (req, res) => {
const { id } = req.params;
const { route_name, starting_point, destination, description } = req.body;
if (!route_name || !starting_point || !destination || !description) {
return res
.status(400)
.json({ error: 'All fields are required. Please fill in all details.' });
}
const connection = mysql.createConnection(mysqlConnection);
const query = `
UPDATE Bus
SET route_name = ?, starting_point = ?, destination = ?, description = ?
WHERE id = ?`;
connection.connect((err) => {
if (err) {
console.error('Database connection failed:', err);
return res.status(500).json({ error: 'Database connection failed' });
}
connection.query(
query,
[route_name, starting_point, destination, description, id],
(err, results) => {
connection.end();
if (err) {
console.error('Query execution failed:', err);
return res.status(500).json({ error: 'Query execution failed' });
}
if (results.affectedRows === 0) {
return res.status(404).json({ error: 'Route not found' });
}
return res.status(200).json({ message: 'Route updated successfully!' });
}
);
});
});
app.delete('/bus/delete/:id', (req, res) => {
const { id } = req.params;
console.log(id);
const connection = mysql.createConnection(mysqlConnection);
const query = 'DELETE FROM Bus WHERE id = ?';
connection.connect((err) => {
if (err) {
return res.status(500).json({ error: 'Database connection failed' });
} else {
connection.query(query, [id], (err, results) => {
if (err) {
return res.status(500).json({ error: 'Query execution failed' });
} else if (results.affectedRows === 0) {
return res.status(404).json({ error: 'Route not found' });
} else {
return res.status(200).json({ message: 'Route deleted successfully!' });
}
});
connection.end();
}
});
});
const PORT = process.env.PORT || 5500;
let server;
if (require.main === module) {
server = app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
}
module.exports = {app,server};