From 76f2fd7b7e94ea60ae08afcf71a53177f992f5aa Mon Sep 17 00:00:00 2001 From: Mann Patel <130435633+MannPatel0@users.noreply.github.com> Date: Tue, 11 Mar 2025 16:44:25 -0600 Subject: [PATCH] Backend user query made express func to create user, read user table with url, --- backend/index.js | 56 ++++++++++++++++++++++++++++++++++----- backend/recommondation.go | 2 -- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/backend/index.js b/backend/index.js index d70dfc0..e3c0bca 100644 --- a/backend/index.js +++ b/backend/index.js @@ -1,16 +1,58 @@ import express, { json } from "express"; import cors from "cors"; -const app = express(); +import mysql from "mysql2"; +const app = express(); app.use(cors()); app.use(json()); -// sample route -app.get("/api/test", (req, res) => { - res.json({ message: "Hello from server!" }); +//TODO: Connect with the database: +const db_con = mysql.createConnection({ + host: "localhost", + user: "root", + password: "12345678", + database: "marketplace", + enableKeepAlive: true, // false by default. }); -app.listen(3030, () => { - console.log("\tRunning Backend:"); - console.log("\x1b[36m \tLocal:\thttp://localhost:3030/ \x1b[0m"); +db_con.connect((err) => { + if (err) { + console.error("Database connection failed: " + err.stack); + return; + } + console.log("Connected to MySQL database."); +}); + +//TODO: Create a users: +app.post("/create_users", (req, res) => { + const data = req.body; + db_con.query( + `INSERT INTO Users (Name, Email, UCID, Password, Phone, Address) + VALUES ('${data.name}', '${data.email}', '${data.UCID}', '${data.password}', '${data.phone}', '${data.address}');`, + ); + db_con.query( + `INSERT INTO UserRole (Role) + VALUES ('${data.role}');`, + ); + console.log(data); + res.send(); +}); + +//TODO: Fetch all users data: +app.get("/fetch_all_users", (req, res) => { + db_con.query("SELECT * FROM Users;", (err, data) => { + if (err) { + console.error("Errors: ", err); + return res.status(500).json({ error: "\nCould not fetch users!" }); + } + res.json({ Users: data }); + }); +}); + +//TODO: Fetch One user Data: +//TODO: Update A uses Data: +//TODO: Delete A uses Data: + +app.listen(3030, () => { + console.log("Running Backend on http://localhost:3030/"); }); diff --git a/backend/recommondation.go b/backend/recommondation.go index f87cb26..3a82edb 100644 --- a/backend/recommondation.go +++ b/backend/recommondation.go @@ -41,5 +41,3 @@ func main() { fmt.Printf("Similarity with product %d: %f\n", i, sim) } } - -