Untitled
unknown
javascript
a year ago
6.3 kB
5
Indexable
const express = require('express');
const mysql = require('mysql2');
const path = require('path');
const app = express();
// Your MySQL connection configuration
const connectionConfig = {
host: 'localhost',
user: 'root',
password: 'Aeondev555666',
database: 'hl7_data'
};
// Create a MySQL connection pool
const pool = mysql.createPool(connectionConfig);
// Define the directory where your HTML and JS files are located
const templatePath = 'c:/Users/andre/Desktop/web/corona-free-dark-bootstrap-admin-template-1.0.0';
// Serve static files (CSS, JavaScript, images) from the 'assets' directory
app.use(express.static(path.join(templatePath, 'assets')));
// Serve the index.html file
app.get('/index.html', (req, res) => {
res.sendFile(path.join(templatePath, 'template', 'index.html'));
});
// Serve the style.css file
app.get('/assets/css/style.css', (req, res) => {
res.sendFile(path.join(templatePath, 'template', 'assets', 'css', 'style.css'));
});
app.use('/assets/fonts', express.static(path.join(templatePath, 'template', 'assets', 'fonts')));
app.use('/assets/images', express.static(path.join(templatePath, 'template', 'assets', 'images')));
app.use('/assets/js', express.static(path.join(templatePath, 'template', 'assets', 'js')));
app.use('/assets/scss', express.static(path.join(templatePath, 'template', 'assets', 'scss')));
app.use('/assets/vendors', express.static(path.join(templatePath, 'template', 'assets', 'vendors')));
// Route to handle data requests from frontend with pagination, item count, and search
app.get('/data', (req, res) => {
// Extract query parameters
const { search, NAZKL, patientID, timestamp, page, limit } = req.query;
// Pagination parameters
const pageNumber = parseInt(page) || 1; // Default to page 1 if not provided
const pageSize = parseInt(limit) || 10; // Default page size to 10 if not provided
const offset = (pageNumber - 1) * pageSize;
// Construct the main SQL query based on filters and pagination parameters
let query = `
SELECT
po.patientId,
po.operation_name AS operationName,
po.min_value AS minValue,
po.max_value AS \`maxValue\`,
po.patient_value AS patientValue,
po.compared_value AS comparedValue,
po.units,
po.timestampOperation AS timestamp,
p.Meno,
k.NAZKL,
MIN(po.operation_name) AS firstOperationName,
MIN(po.min_value) AS firstMinValue,
MIN(po.max_value) AS firstMaxValue,
MIN(po.patient_value) AS firstPatientValue,
MIN(po.compared_value) AS firstComparedValue,
MIN(po.units) AS firstUnits
FROM
patient_operations po
INNER JOIN
pacienti p ON po.patientId = p.patientID
INNER JOIN
klienti k ON p.id_kln = k.ID_ADR
`;
// Group by NAZKL and timestampOperation
query += ' GROUP BY k.NAZKL, po.timestampOperation';
let queryParams = [];
// Add search filter to the query if search parameter is provided
if (search) {
query += ' WHERE po.patientId LIKE ? OR po.timestampOperation LIKE ? OR p.Meno LIKE ? OR k.NAZKL LIKE ?';
queryParams.push(`%${search}%`, `%${search}%`, `%${search}%`, `%${search}%`); // Use wildcard to match any substring
}
// Add NAZKL filter to the query if NAZKL parameter is provided
if (NAZKL) {
query += (search ? ' AND' : ' WHERE') + ' k.NAZKL LIKE ?';
queryParams.push(`%${NAZKL}%`);
}
// Add patientID filter to the query if patientID parameter is provided
if (patientID) {
query += (search || NAZKL ? ' AND' : ' WHERE') + ' po.patientId = ?';
queryParams.push(patientID);
}
// Add timestamp filter to the query if timestamp parameter is provided
if (timestamp) {
query += (search || NAZKL || patientID ? ' AND' : ' WHERE') + ' po.timestampOperation = ?';
queryParams.push(timestamp);
}
// Construct the query to get the total count of records
let countQuery = `
SELECT COUNT(*) AS totalCount
FROM
patient_operations po
INNER JOIN
pacienti p
ON
po.patientId = p.patientID
INNER JOIN
klienti k
ON
p.id_kln = k.ID_ADR
`;
// Add the same search, NAZKL, patientID, and timestamp filters to the count query
if (search) {
countQuery += ' WHERE po.patientId LIKE ? OR po.timestampOperation LIKE ? OR p.Meno LIKE ? OR k.NAZKL LIKE ?';
}
if (NAZKL) {
countQuery += (search ? ' AND' : ' WHERE') + ' k.NAZKL LIKE ?';
}
if (patientID) {
countQuery += (search || NAZKL ? ' AND' : ' WHERE') + ' po.patientId = ?';
}
if (timestamp) {
countQuery += (search || NAZKL || patientID ? ' AND' : ' WHERE') + ' po.timestampOperation = ?';
}
// Execute the count query
pool.query(countQuery, queryParams, (countError, countResult) => {
if (countError) {
console.error('Error querying record count from MySQL:', countError);
res.status(500).json({ error: 'Internal server error' });
return;
}
// Retrieve total count
const totalCount = countResult[0].totalCount;
// Calculate total pages
const totalPages = Math.ceil(totalCount / pageSize);
// Add pagination limits to the main query
query += ' LIMIT ? OFFSET ?';
queryParams.push(pageSize, offset);
// Execute the main SQL query to fetch data
pool.query(query, queryParams, (error, results) => {
if (error) {
console.error('Error querying MySQL:', error);
res.status(500).json({ error: 'Internal server error' });
return;
}
// Send both the data and pagination information back to the frontend
res.json({ data: results, totalResults: totalCount, totalPages });
});
});
});
// Start the server
const PORT = process.env.PORT || 3500;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
Editor is loading...
Leave a Comment