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.
 
 
 

106 lines
2.2 KiB

const { database } = 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 query = database.prepare('INSERT INTO niveaus (nom) VALUES (?)')
try {
let responses = []
for (const name of nom_multiple) {
// Insert each name and collect the response
let response = await query.run(name)
responses.push(response)
}
if (nom_multiple.length === responses.length) {
return JSON.stringify({
last_row: nom_multiple.length,
changes: 1
})
} else return false
} catch (error) {
return error // Return the error if any occurs
}
}
/**
* function to get single niveau for updating
* @param {*} id
* @returns Promise
*/
async function getSingleNiveau(id) {
const query = database.prepare('SELECT * FROM niveaus WHERE id = ?')
try {
let response = await query.get(id)
return response
} catch (error) {
return error
}
}
/**
* function used to update the niveau
* @param {*} nom
* @param {*} id
* @returns Promise
*/
async function updateNiveau(nom, id) {
const query = database.prepare('UPDATE niveaus SET nom = ? WHERE id = ?')
try {
let response = query.run(nom, id)
return response
} catch (error) {
return error
}
}
/**
* function to get all niveau
*/
async function getNiveau() {
const query = database.prepare('SELECT * FROM niveaus')
try {
let response = query.all()
return response
} catch (error) {
return error
}
}
async function deleteNiveau(id) {
const query = database.prepare('DELETE FROM niveaus WHERE id = ?')
try {
let response = await query.run(id)
return response
} catch (error) {
return error
}
}
module.exports = {
getNiveau,
insertNiveau,
getSingleNiveau,
updateNiveau,
deleteNiveau
}