UBS/routes/api.js
2024-12-04 20:57:28 -07:00

63 lines
2.2 KiB
JavaScript

const express = require('express');
const router = express.Router();
const mariadb = require('mariadb');
// Create a MariaDB connection pool
const pool = mariadb.createPool({
host: process.env.DB_HOST, // Replace with your database host
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER, // Replace with your database username
password: process.env.DB_PASS, // Replace with your database password
database: process.env.DB_DATABASE, // Replace with your database name
connectionLimit: 5 // Adjust connection limit as needed
});
// Route to fetch all bans
router.get('/v1/bans', async (req, res) => {
try {
// Get a connection from the pool
const connection = await pool.getConnection();
try {
// Execute the query to fetch all rows from the `bans` table
const rows = await connection.query('SELECT * FROM bans');
// Send the results as a JSON response
res.json(rows);
} finally {
// Release the connection back to the pool
connection.release();
}
} catch (err) {
console.error('Error fetching bans:', err);
// Respond with a 500 Internal Server Error status if something goes wrong
res.status(500).json({ error: 'An error occurred while fetching the bans.' });
}
});
router.get("/v1/ban/roblox/:uid", async (req, res) => {
if (!req.params.uid) return res.status(400).json({error: "Specify user ID!"});
try {
// Get a connection from the pool
const connection = await pool.getConnection();
try {
// Execute the query to fetch all rows from the `bans` table
const rows = await connection.query('SELECT * FROM bans WHERE robloxId = ?', [req.params.uid]);
// Send the results as a JSON response
res.json(rows);
} finally {
// Release the connection back to the pool
connection.release();
}
} catch (err) {
console.error('Error fetching bans:', err);
// Respond with a 500 Internal Server Error status if something goes wrong
res.status(500).json({ error: 'An error occurred while fetching the bans.' });
}
})
module.exports = router;