diff --git a/README.md b/README.md index adaa015..b506b12 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,12 @@ 1. python3 server.py #Start The Server ``` --- +### Recommendation system +1. Install the dependencies +```Bash + pip install mysql.connector +``` + ### Database 1. MySql Version 9.2.0 diff --git a/backend/controllers/product.js b/backend/controllers/product.js index d594f89..569e4c6 100644 --- a/backend/controllers/product.js +++ b/backend/controllers/product.js @@ -1,5 +1,37 @@ const db = require("../utils/database"); +exports.addProduct = async (req, res) => { + const { userID, name, price, qty, description, category, images } = req.body; + + try { + const [result] = await db.execute( + `INSERT INTO Product (Name, Price, StockQuantity, UserID, Description, CategoryID) VALUES (?, ?, ?, ?, ?, ?)`, + [name, price, qty, userID, description, category], + ); + + const productID = result.insertId; + if (images && images.length > 0) { + const imageInsertPromises = images.map((imagePath) => + db.execute(`INSERT INTO Image_URL (URL, ProductID) VALUES (?, ?)`, [ + imagePath, + productID, + ]), + ); + + await Promise.all(imageInsertPromises); //perallel + } + + res.json({ + success: true, + message: "Product and images added successfully", + }); + } catch (error) { + console.error("Error adding product or images:", error); + console.log(error); + return res.json({ error: "Could not add product or images" }); + } +}; + exports.addFavorite = async (req, res) => { const { userID, productID } = req.body; console.log(userID); diff --git a/backend/controllers/user.js b/backend/controllers/user.js index 27a0dc3..32aef17 100644 --- a/backend/controllers/user.js +++ b/backend/controllers/user.js @@ -134,6 +134,62 @@ exports.completeSignUp = async (req, res) => { } }; +exports.doLogin = async (req, res) => { + const { email, password } = req.body; + + // Input validation + if (!email || !password) { + return res.status(400).json({ + found: false, + error: "Email and password are required", + }); + } + + try { + // Query to find user with matching email + const query = "SELECT * FROM User WHERE email = ?"; + const [data, fields] = await db.execute(query, [email]); + + // Check if user was found + if (data && data.length > 0) { + const user = data[0]; + + // Verify password match + if (user.Password === password) { + // Consider using bcrypt for secure password comparison + // Return user data without password + return res.json({ + found: true, + userID: user.UserID, + name: user.Name, + email: user.Email, + UCID: user.UCID, + phone: user.Phone, + address: user.Address, + }); + } else { + // Password doesn't match + return res.json({ + found: false, + error: "Invalid email or password", + }); + } + } else { + // User not found + return res.json({ + found: false, + error: "Invalid email or password", + }); + } + } catch (error) { + console.error("Error logging in:", error); + return res.status(500).json({ + found: false, + error: "Database error occurred", + }); + } +}; + exports.getAllUser = async (req, res) => { try { const [users, fields] = await db.execute("SELECT * FROM User;"); @@ -174,6 +230,7 @@ exports.findUserByEmail = async (req, res) => { UCID: user.UCID, phone: user.Phone, address: user.Address, + password: user.Password, // Include any other fields your user might have // Make sure the field names match exactly with your database column names }); @@ -201,7 +258,7 @@ exports.updateUser = async (req, res) => { const phone = req.body?.phone; const UCID = req.body?.UCID; const address = req.body?.address; - + const password = req.body?.password; if (!userId) { return res.status(400).json({ error: "User ID is required" }); } @@ -213,7 +270,7 @@ exports.updateUser = async (req, res) => { if (phone) updateData.phone = phone; if (UCID) updateData.UCID = UCID; if (address) updateData.address = address; - + if (password) updateData.password = password; if (Object.keys(updateData).length === 0) { return res.status(400).json({ error: "No valid fields to update" }); } diff --git a/backend/routes/product.js b/backend/routes/product.js index 24c5705..944e63b 100644 --- a/backend/routes/product.js +++ b/backend/routes/product.js @@ -6,6 +6,7 @@ const { removeFavorite, getAllProducts, getProductById, + addProduct, } = require("../controllers/product"); const router = express.Router(); @@ -19,6 +20,7 @@ router.post("/addFavorite", addFavorite); router.post("/getFavorites", getFavorites); router.post("/delFavorite", removeFavorite); +router.post("/addProduct", addProduct); router.get("/getProduct", getAllProducts); router.get("/:id", getProductById); // Simplified route diff --git a/backend/routes/review.js b/backend/routes/review.js index 5b26a87..b39c8f6 100644 --- a/backend/routes/review.js +++ b/backend/routes/review.js @@ -4,6 +4,6 @@ const { getReviews, submitReview } = require("../controllers/review"); const router = express.Router(); router.get("/:id", getReviews); -router.post("/add", submitReview); +router.post("/addReview", submitReview); module.exports = router; diff --git a/backend/routes/user.js b/backend/routes/user.js index 3d11102..1ccbc88 100644 --- a/backend/routes/user.js +++ b/backend/routes/user.js @@ -7,6 +7,7 @@ const { findUserByEmail, updateUser, deleteUser, + doLogin, } = require("../controllers/user"); const router = express.Router(); @@ -26,6 +27,9 @@ router.get("/fetch_all_users", getAllUser); //Fetch One user Data with all fields: router.post("/find_user", findUserByEmail); +//Fetch One user Data with all fields: +router.post("/do_login", doLogin); + //Update A uses Data: router.post("/update", updateUser); diff --git a/backend/utils/database.js b/backend/utils/database.js index 689785e..6e75c3a 100644 --- a/backend/utils/database.js +++ b/backend/utils/database.js @@ -1,11 +1,9 @@ const mysql = require("mysql2"); -//Create a pool of connections to allow multiple query happen at the same time const pool = mysql.createPool({ host: "localhost", user: "root", database: "Marketplace", }); -//Export a promise for promise-based query module.exports = pool.promise(); diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index a3ec64f..4b88b70 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -52,6 +52,10 @@ function App() { return () => window.removeEventListener("resize", handleResize); }, []); + useEffect(() => { + sendSessionDataToServer(); + }, []); + // Send verification code const sendVerificationCode = async (userData) => { try { @@ -245,7 +249,7 @@ function App() { UCID: formValues.ucid, phone: formValues.phone, password: formValues.password, // This will be needed for the final signup - address: "NOT_GIVEN", + address: formValues.address, // Add this line client: 1, admin: 0, }; @@ -261,7 +265,7 @@ function App() { // Make API call to localhost:3030/find_user const response = await fetch( - "http://localhost:3030/api/user/find_user", + "http://localhost:3030/api/user/do_login", { method: "POST", headers: { @@ -296,7 +300,7 @@ function App() { // Save to localStorage to persist across refreshes sessionStorage.setItem("isAuthenticated", "true"); sessionStorage.setItem("user", JSON.stringify(userObj)); - sendSessionDataToServer(); // Call it after signup + sessionStorage.getItem("user"); console.log("Login successful for:", userData.email); @@ -365,8 +369,8 @@ function App() { try { // Retrieve data from sessionStorage const user = JSON.parse(sessionStorage.getItem("user")); - const isAuthenticated = - sessionStorage.getItem("isAuthenticated") === "true"; + // const isAuthenticated = + // sessionStorage.getItem("isAuthenticated") === "true"; if (!user || !isAuthenticated) { console.log("User is not authenticated"); @@ -527,6 +531,25 @@ function App() { )} + {isSignUp && ( +
+ + +
+ )} +