-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
185 lines (155 loc) · 5.72 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const express = require("express");
const mysql = require("mysql");
const cors = require("cors");
const bodyParser = require("body-parser");
const app = express();
app.use(cors());
app.use(express.json());
app.use(bodyParser.json());
const pool = mysql.createPool({
connectionLimit: 10,
host: "localhost",
user: "root",
password: "1234",
database: "alumniportal",
});
function queryDatabase(query, values = []) {
return new Promise((resolve, reject) => {
pool.getConnection((err, connection) => {
if (err) {
console.error("Error getting connection from pool:", err);
return reject(err);
}
connection.query(query, values, (error, results) => {
connection.release();
if (error) return reject(error);
resolve(results);
});
});
});
}
// Routes
app.post("/submit-form", async (req, res) => {
const { userName, jobStatus, mobile, social, batch, email, password } = req.body;
const query = "INSERT INTO user (userName, jobStatus, mobile, social, batch, email, password) VALUES (?, ?, ?, ?, ?, ?, ?)";
try {
await queryDatabase(query, [userName, jobStatus, mobile, social, batch, email, password]);
res.json({ success: true });
} catch (error) {
console.error("Error saving data:", error);
res.status(500).json({ success: false, message: "Error saving data" });
}
});
app.post("/add-event", async (req, res) => {
const { location, organiser, description, date } = req.body;
const query = "INSERT INTO events (location, organiser, description, date) VALUES (?, ?, ?, ?)";
try {
await queryDatabase(query, [location, organiser, description, date]);
res.json({ success: true, message: "Event data saved successfully" });
} catch (error) {
console.error("Error saving event data:", error);
res.status(500).json({ success: false, message: "Error saving event data", error: error.message });
}
});
app.post("/login", async (req, res) => {
const { batch, email, password } = req.body;
const query = "SELECT * FROM user WHERE batch = ? AND email = ? AND password = ?";
try {
const results = await queryDatabase(query, [batch, email, password]);
if (results.length > 0) {
res.json({ success: true, user: results[0] });
} else {
res.json({ success: false, message: "Invalid batch, email, or password" });
}
} catch (error) {
console.error("Error fetching user data:", error);
res.status(500).json({ success: false, message: "Error fetching user data" });
}
});
app.post("/message", async (req, res) => {
const { user, message } = req.body;
const query = "INSERT INTO messages (user, message) VALUES (?, ?)";
try {
await queryDatabase(query, [user, message]);
res.json({ success: true });
} catch (error) {
console.error("Error saving data:", error);
res.status(500).json({ success: false, message: "Error saving data" });
}
});
app.post("/meetup", async (req, res) => {
const { name, city, s_date, e_date } = req.body;
const query = "INSERT INTO meetup (name, city, s_date, e_date) VALUES (?, ?, ?, ?)";
try {
await queryDatabase(query, [name, city, s_date, e_date]);
res.json({ success: true });
} catch (error) {
console.error("Error saving data:", error);
res.status(500).json({ success: false, meetup: "Error saving data" });
}
});
app.post("/events", async (req, res) => {
const {counter, location}= req.body
const query = "UPDATE events SET counter = ? where location = ?";
// const query = "UPDATE events SET counter = 5";
try {
await queryDatabase(query,[counter,location]);
res.json({ success: true });
} catch (error) {
console.error("Error saving data:", error);
res.status(500).json({ success: false, message: "Error saving data" });
}
});
app.get("/events", async (req, res) => {
const query = "SELECT * FROM events";
try {
const results = await queryDatabase(query);
res.json({ success: true, events: results });
} catch (error) {
console.error("Error fetching events:", error);
res.status(500).json({ success: false, message: "Error fetching events" });
}
});
app.get("/network", async (req, res) => {
const query = "SELECT * FROM user";
try {
const results = await queryDatabase(query);
res.json({ success: true, events: results });
} catch (error) {
console.error("Error fetching users:", error);
res.status(500).json({ success: false, message: "Error fetching users" });
}
});
app.get("/messages", async (req, res) => {
const query = "SELECT * FROM messages";
try {
const results = await queryDatabase(query);
res.json({ success: true, messages: results });
} catch (error) {
console.error("Error fetching messages:", error);
res.status(500).json({ success: false, message: "Error fetching messages" });
}
});
app.get("/meetup", async (req, res) => {
const query = "SELECT * FROM meetup";
try {
const results = await queryDatabase(query);
res.json({ success: true, meetups: results });
} catch (error) {
console.error("Error fetching events:", error);
res.status(500).json({ success: false, message: "Error fetching events" });
}
});
app.get("/mapdemo", async (req, res) => {
const query = "SELECT city FROM meetup";
try {
const results = await queryDatabase(query);
res.json({ success: true, meetups: results });
} catch (error) {
console.error("Error fetching events:", error);
res.status(500).json({ success: false, message: "Error fetching events" });
}
});
app.listen(3000, () => {
console.log("Server running on http://localhost:3000 Db");
});