estatesfunding/ef-api/controllers/mysqlproperty.js

120 lines
3.9 KiB
JavaScript
Raw Permalink Normal View History

2024-09-17 14:36:35 +00:00
import mysql from "mysql2";
2024-09-22 15:25:05 +00:00
import pool from "../db/db.js";
2024-09-19 11:35:14 +00:00
2024-09-17 14:36:35 +00:00
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",
2024-09-17 15:14:56 +00:00
connectTimeout: 20000, // 20 seconds
2024-09-17 14:36:35 +00:00
});
2024-09-19 11:35:14 +00:00
// const db = mysql.createConnection({
// host: "localhost",
// user: "root",
// password: "root",
// database: "estates",
// port: "3306",
// connectTimeout: 20000, // 20 seconds
// });
2024-09-17 14:36:35 +00:00
// Connect to MySQL database
db.connect((err) => {
if (err) {
console.error("Database connection failed: " + err.stack);
return;
}
console.log("Connected to the MYSQL database.");
});
2024-09-19 15:47:01 +00:00
// Controller function to get the data from the table from MYSQL with no pagination and huge data cant be getting
2024-09-19 11:35:14 +00:00
// 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);
// });
// };
2024-09-19 15:47:01 +00:00
// 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
2024-09-17 14:36:35 +00:00
export const searchMySQL = (req, res) => {
2024-09-19 15:47:01 +00:00
const limit = parseInt(req.query.limit) || 10; // Default to 10 items per page
2024-09-19 11:35:14 +00:00
const offset = parseInt(req.query.offset) || 0; // Default to the first page
2024-09-19 15:47:01 +00:00
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 ?`;
2024-09-19 11:35:14 +00:00
2024-09-19 15:47:01 +00:00
// Perform count query
db.query(countQuery, [`%${searchQuery}%`], (err, countResult) => {
2024-09-17 14:36:35 +00:00
if (err) {
console.log(err);
return res.status(500).json({ error: "Database query failed" });
}
2024-09-19 11:35:14 +00:00
2024-09-19 15:47:01 +00:00
// Get the total count from the query result
const totalRecords = countResult[0].total;
2024-09-19 11:35:14 +00:00
2024-09-19 15:47:01 +00:00
// 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" });
}
2024-09-19 11:35:14 +00:00
2024-09-19 15:47:01 +00:00
// 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
});
});
});
};
2024-09-20 11:18:56 +00:00
// Controller function to search for MYSQL properties by house_id
export const PropertiesMysqlView = async (req, res) => {
const { house_id } = req.params;
2024-09-22 15:25:05 +00:00
// console.log("hu", house_id);
2024-09-20 11:18:56 +00:00
try {
2024-09-22 15:25:05 +00:00
const query = `SELECT * FROM home_information WHERE house_id = ?`;
2024-09-20 11:18:56 +00:00
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" });
}
};