Ajout panel + récup infos depuis SQL + fix noms de fichiers

This commit is contained in:
Zachary Guénot
2023-05-10 01:53:55 +02:00
parent 172f811e91
commit de01c981f8
14 changed files with 162 additions and 58 deletions

View File

@@ -1,38 +1,30 @@
const mysql = require('mysql')
const mysql = require('mysql2/promise')
module.exports = function rewardRedemption(user_id, user_name) {
module.exports = async function (user_id, user_name) {
// Create a connection to the MySQL database
const connection = mysql.createConnection({
const connection = await mysql.createConnection({
host: process.env.MYSQL_HOST,
port: process.env.MYSQL_PORT,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE
})
// Connect to the database
connection.connect(error => {
if (error) return console.error(error)
console.log(`Connected to MySql database as id ${connection.threadId} !`)
})
// Check if the user already exists in the rewards table
connection.query('SELECT * FROM rewards WHERE user_id = ?', [user_id], (error, results) => {
if (error) return console.error(error)
if (results.length === 0) {
// User doesn't exist, insert a new row
connection.query('INSERT INTO rewards SET ?', { user_id, user_name, count: 1 }, error => {
if (error) return console.error(error)
})
} else {
// User exists, update the count
const newRow = { count: results[0].count + 1 }
connection.query('UPDATE rewards SET ? WHERE user_id = ?', [newRow, user_id], error => {
if (error) return console.error(error)
})
}
})
await connection.query('SELECT * FROM rewards WHERE user_id = ?', [user_id])
.then(async ([rows, fields]) => {
if (rows.length === 0) {
// User doesn't exist, insert a new row
await connection.query('INSERT INTO rewards SET ?', { user_id, user_name, count: 1, current_count: 1 })
.catch(error => { console.error(error) })
} else {
// User exists, update the count
const newRow = { count: rows[0].count + 1, current_count: rows[0].current_count + 1 }
await connection.query('UPDATE rewards SET ? WHERE user_id = ?', [newRow, user_id])
.catch(error => { console.error(error) })
}
}).catch(error => { console.error(error) })
// Terminate the connection to the database
connection.end()
await connection.end()
}