import mysql from "mysql2"; import pool from "../db/db.js"; const db = mysql.createConnection({ host: "db-mysql-nyc1-99306-do-user-12431193-0.b.db.ondigitalocean.com", user: "doadmin", password: "AVNS_EPHqPilzmVjZfm8GH4G", database: "defaultdb", port: "25060", connectTimeout: 20000, // 20 seconds }); // const db = mysql.createConnection({ // host: "localhost", // user: "root", // password: "root", // database: "estates", // port: "3306", // connectTimeout: 20000, // 20 seconds // }); // Connect to MySQL database db.connect((err) => { if (err) { console.error("Database connection failed: " + err.stack); return; } console.log("Connected to the MYSQL database."); }); // Controller function to get the data from the table from MYSQL with no pagination and huge data cant be getting // export const searchMySQL = (req, res) => { // const q = "SELECT * FROM client_information"; // // const q = "SELECT * FROM home_information"; // db.query(q, (err, data) => { // if (err) { // console.log(err); // return res.status(500).json({ error: "Database query failed" }); // } // return res.json(data); // }); // }; // Controller function to get all data from MYSQL table with pagination // export const searchMySQL = (req, res) => { // const limit = parseInt(req.query.limit) || 10; // Default to 10 items per page // const offset = parseInt(req.query.offset) || 0; // Default to the first page // const q = `SELECT * FROM home_information LIMIT ${limit} OFFSET ${offset}`; // db.query(q, (err, data) => { // if (err) { // console.log(err); // return res.status(500).json({ error: "Database query failed" }); // } // // Set cache-control headers to prevent caching // res.set('Cache-Control', 'no-store'); // return res.json(data); // }); // }; // Controller function to get all data from MYSQL table with pagination and total records export const searchMySQL = (req, res) => { const limit = parseInt(req.query.limit) || 10; // Default to 10 items per page const offset = parseInt(req.query.offset) || 0; // Default to the first page const searchQuery = req.query.search || ''; // Get search query if provided // Query to get total count of records const countQuery = `SELECT COUNT(*) as total FROM home_information WHERE address LIKE ?`; // Query to fetch paginated data const dataQuery = `SELECT * FROM home_information WHERE address LIKE ? LIMIT ? OFFSET ?`; // Perform count query db.query(countQuery, [`%${searchQuery}%`], (err, countResult) => { if (err) { console.log(err); return res.status(500).json({ error: "Database query failed" }); } // Get the total count from the query result const totalRecords = countResult[0].total; // Perform data query db.query(dataQuery, [`%${searchQuery}%`, limit, offset], (err, data) => { if (err) { console.log(err); return res.status(500).json({ error: "Database query failed" }); } // Set cache-control headers to prevent caching res.set('Cache-Control', 'no-store'); // Return both count and paginated data in the response return res.json({ data, totalRecords }); }); }); }; // Controller function to search for MYSQL properties by house_id export const PropertiesMysqlView = async (req, res) => { const { house_id } = req.params; console.log("hu", house_id); try { const query = ` SELECT address, city, county, state, total_living_sqft, year_built, cost_per_sqft FROM home_information WHERE house_id = ? `; const [rows] = await pool.query(query, [house_id]); if (rows.length > 0) { res.status(200).json(rows[0]); // Send property details if found } else { res.status(404).json({ message: "Property not found" }); } } catch (error) { console.error("Error fetching property details:", error); res.status(500).json({ error: "Internal Server Error" }); } };