You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
2.9 KiB
134 lines
2.9 KiB
const { pool } = require('../database')
|
|
|
|
/**
|
|
* function to insert niveau to database
|
|
*/
|
|
async function insertNiveau(nom) {
|
|
// Initialize an array to hold the individual names
|
|
let nom_multiple = []
|
|
|
|
// Check if 'nom' contains a comma and split it into multiple names
|
|
if (nom.includes(',')) {
|
|
nom_multiple = nom.split(',' || ', ').map((name) => name.trim()) // Trim to remove extra spaces
|
|
} else {
|
|
nom_multiple = [nom] // Treat it as a single name
|
|
}
|
|
|
|
// Prepare the query for insertion
|
|
const sql = `INSERT INTO niveaus (nom) VALUES (?)`
|
|
|
|
try {
|
|
let responses = []
|
|
|
|
for (const name of nom_multiple) {
|
|
// Insert each name and collect the response
|
|
const [result] = await pool.query(sql, [name])
|
|
responses.push(result)
|
|
}
|
|
if (nom_multiple.length === responses.length) {
|
|
return JSON.stringify({
|
|
last_row: nom_multiple.length,
|
|
changes: 1
|
|
})
|
|
} else return false
|
|
} catch (error) {
|
|
return { success: false, error: 'Erreur veullez réeseyer' }
|
|
}
|
|
}
|
|
|
|
/**
|
|
* function to get single niveau for updating
|
|
* @param {*} id
|
|
* @returns Promise
|
|
*/
|
|
async function getSingleNiveau(id) {
|
|
const sql = 'SELECT * FROM niveaus WHERE id = ?'
|
|
|
|
try {
|
|
let [rows] = await pool.query(sql, [id])
|
|
|
|
if (rows.length > 0) {
|
|
return rows[0]
|
|
} else {
|
|
return null
|
|
}
|
|
} catch (error) {
|
|
return { success: false, error: 'Erreur veullez réeseyer' }
|
|
}
|
|
}
|
|
|
|
/**
|
|
* function used to update the niveau
|
|
* @param {*} nom
|
|
* @param {*} id
|
|
* @returns Promise
|
|
*/
|
|
async function updateNiveau(nom, id) {
|
|
const sql = 'UPDATE niveaus SET nom = ? WHERE id = ?'
|
|
|
|
try {
|
|
let [result] = await pool.query(sql, [nom, id])
|
|
|
|
if (result.affectedRows === 0) {
|
|
return {
|
|
success: false,
|
|
message: 'Niveau non trouvé ou aucune modification effectuée.'
|
|
}
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
message: 'Utilisateur mis à jour avec succès.'
|
|
}
|
|
} catch (error) {
|
|
return { success: false, error: 'Erreur veullez réeseyer' + error }
|
|
}
|
|
}
|
|
|
|
/**
|
|
* function to get all niveau
|
|
*/
|
|
async function getNiveau() {
|
|
const sql = 'SELECT * FROM niveaus'
|
|
|
|
try {
|
|
let [rows] = await pool.query(sql)
|
|
|
|
return rows
|
|
} catch (error) {
|
|
return { success: false, error: 'Erreur veullez réeseyer' }
|
|
}
|
|
}
|
|
|
|
async function deleteNiveau(id) {
|
|
const sql = 'DELETE FROM niveaus WHERE id = ?'
|
|
|
|
try {
|
|
let [result] = await pool.query(sql, [id])
|
|
|
|
if (result.affectedRows === 0) {
|
|
return {
|
|
success: false,
|
|
message: 'Niveau non trouvé.'
|
|
}
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
message: 'Niveau supprimé avec succès.'
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
error: 'Erreur lors de la suppression, veuillez réessayer.'
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
getNiveau,
|
|
insertNiveau,
|
|
getSingleNiveau,
|
|
updateNiveau,
|
|
deleteNiveau
|
|
}
|
|
|