backend: Première route /functions/:functionName + Gestion des erreurs 404 et 500
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
module.exports = {
|
||||
const config = {
|
||||
PORT: process.env.PORT || 8080
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = config;
|
18
backend/assets/config/errors.js
Normal file
18
backend/assets/config/errors.js
Normal file
@ -0,0 +1,18 @@
|
||||
const errors = {
|
||||
generalError: {
|
||||
result: "Vous n'avez pas rentré de valeur valide.",
|
||||
httpStatus: 400
|
||||
},
|
||||
|
||||
serverError: {
|
||||
result: "Le serveur n'a pas pu traiter votre requête.",
|
||||
httpStatus: 500
|
||||
},
|
||||
|
||||
requiredFields: {
|
||||
result: "Vous devez remplir tous les champs...",
|
||||
httpStatus: 400
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = errors;
|
24
backend/assets/functions/functionObject.js
Normal file
24
backend/assets/functions/functionObject.js
Normal file
@ -0,0 +1,24 @@
|
||||
const { randomNumberOutput } = require('./main/randomNumber');
|
||||
const { convertArabicToRomanOutput, convertRomanToArabicOutput } = require('./main/convertRomanArabicNumbers');
|
||||
|
||||
const functionObject = {
|
||||
randomNumber: {
|
||||
functionOutput: randomNumberOutput,
|
||||
args: ["min", "max"]
|
||||
},
|
||||
convertArabicToRoman: {
|
||||
functionOutput: convertArabicToRomanOutput,
|
||||
args: ["number"]
|
||||
},
|
||||
convertRomanToArabic: {
|
||||
functionOutput: convertRomanToArabicOutput,
|
||||
args: ["romanNumber"]
|
||||
}
|
||||
};
|
||||
|
||||
// Choisi la fonction à exécuter
|
||||
function functionToExecute(option) {
|
||||
return functionObject[option];
|
||||
}
|
||||
|
||||
module.exports = functionToExecute;
|
109
backend/assets/functions/main/convertRomanArabicNumbers.js
Normal file
109
backend/assets/functions/main/convertRomanArabicNumbers.js
Normal file
@ -0,0 +1,109 @@
|
||||
const sendResponse = require('../../utils/sendResponse');
|
||||
const { requiredFields, generalError } = require('../../config/errors');
|
||||
|
||||
/* Variable pour convertRomanArabicNumbers */
|
||||
const correspondancesRomainArabe = [
|
||||
[1000, "M"],
|
||||
[900, "CM"],
|
||||
[500, "D"],
|
||||
[400, "CD"],
|
||||
[100, "C"],
|
||||
[90, "XC"],
|
||||
[50, "L"],
|
||||
[40, "XL"],
|
||||
[10, "X"],
|
||||
[9, "IX"],
|
||||
[5, "V"],
|
||||
[4, "IV"],
|
||||
[1, "I"],
|
||||
];
|
||||
|
||||
/**
|
||||
* @description Convertis un nombre arabe en nombre romain.
|
||||
* @requires {@link correspondancesRomainArabe}
|
||||
* @param {Number} nombre - Le nombre arabe à convertir
|
||||
* @returns {String}
|
||||
* @examples convertArabicToRoman(24) → 'XXIV'
|
||||
*/
|
||||
function convertArabicToRoman(nombre) {
|
||||
// Initialisation de la variable qui va contenir le résultat de la conversion
|
||||
let chiffresRomains = "";
|
||||
|
||||
function extraireChiffreRomain(valeurLettre, lettres) {
|
||||
while (nombre >= valeurLettre) {
|
||||
chiffresRomains = chiffresRomains + lettres;
|
||||
nombre = nombre - valeurLettre;
|
||||
}
|
||||
}
|
||||
|
||||
correspondancesRomainArabe.forEach(correspondance => {
|
||||
extraireChiffreRomain(correspondance[0], correspondance[1]);
|
||||
});
|
||||
|
||||
return chiffresRomains;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Convertis un nombre romain en nombre arabe.
|
||||
* @requires {@link correspondancesRomainArabe}
|
||||
* @param {String} str - Le nombre romain à convertir
|
||||
* @returns {Number}
|
||||
* @examples convertRomanToArabic('XXIV') → 24
|
||||
*/
|
||||
function convertRomanToArabic(str) {
|
||||
let result = 0;
|
||||
for (let i = 0;i < correspondancesRomainArabe.length; i++) {
|
||||
while (str.indexOf(correspondancesRomainArabe[i][1]) === 0) {
|
||||
// Ajout de la valeur décimale au résultat
|
||||
result += correspondancesRomainArabe[i][0];
|
||||
// Supprimer la lettre romaine correspondante du début
|
||||
str = str.replace(correspondancesRomainArabe[i][1],'');
|
||||
}
|
||||
}
|
||||
if (str != '') {
|
||||
result = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* OUTPUTS */
|
||||
exports.convertRomanToArabicOutput = (res, argsObject) => {
|
||||
let { romanNumber } = argsObject;
|
||||
|
||||
// S'il n'y a pas les champs obligatoire
|
||||
if (!(romanNumber)) {
|
||||
return sendResponse(res, requiredFields);
|
||||
}
|
||||
|
||||
// Formate le paramètre
|
||||
try {
|
||||
romanNumber = romanNumber.toUpperCase();
|
||||
}
|
||||
catch {
|
||||
return sendResponse(res, generalError);
|
||||
}
|
||||
|
||||
const result = convertRomanToArabic(romanNumber);
|
||||
if (result === 0) {
|
||||
return sendResponse(res, generalError);
|
||||
}
|
||||
|
||||
return sendResponse(res, { result, httpStatus: 200 }, true);
|
||||
}
|
||||
|
||||
exports.convertArabicToRomanOutput = (res, argsObject) => {
|
||||
let { number } = argsObject;
|
||||
number = parseInt(number);
|
||||
|
||||
// S'il n'y a pas les champs obligatoire
|
||||
if (!(number)) {
|
||||
return sendResponse(res, requiredFields);
|
||||
}
|
||||
|
||||
// Si ce n'est pas un nombre
|
||||
if (isNaN(number)) {
|
||||
return sendResponse(res, { result: "Veuillez rentré un nombre valide.", httpStatus: 400 });
|
||||
}
|
||||
|
||||
return sendResponse(res, { result: convertArabicToRoman(number) }, true);
|
||||
}
|
32
backend/assets/functions/main/randomNumber.js
Normal file
32
backend/assets/functions/main/randomNumber.js
Normal file
@ -0,0 +1,32 @@
|
||||
const sendResponse = require('../../utils/sendResponse');
|
||||
const { requiredFields } = require('../../config/errors');
|
||||
|
||||
/**
|
||||
* @description Génère un nombre aléatoire entre un minimum inclus et un maximum inclus.
|
||||
* @param {Number} min Nombre Minimum
|
||||
* @param {Number} max Nombre Maximum
|
||||
* @returns {Number} Nombre aléatoire
|
||||
* @examples randomNumber(1, 2) → retourne soit 1 ou 2
|
||||
*/
|
||||
function randomNumber(min, max) {
|
||||
return Math.floor(Math.random() * (max - min +1)) + min;
|
||||
}
|
||||
|
||||
/* OUTPUTS */
|
||||
exports.randomNumberOutput = (res, argsObject) => {
|
||||
let { min, max } = argsObject;
|
||||
min = parseInt(min);
|
||||
max = parseInt(max);
|
||||
|
||||
// S'il n'y a pas les champs obligatoire
|
||||
if (!(min && max)) {
|
||||
return sendResponse(res, requiredFields);
|
||||
}
|
||||
|
||||
// Si ce n'est pas des nombres
|
||||
if (isNaN(min) || isNaN(max)) {
|
||||
return sendResponse(res, { result: "Les paramètres min et max doivent être des nombres...", httpStatus: 400 });
|
||||
}
|
||||
|
||||
return sendResponse(res, { result: randomNumber(min, max) }, true);
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
* @description Envoie la réponse au client
|
||||
* @param {Response} res Objet réponse d'une réponse http/express
|
||||
* @param {Object} object { httpStatus, customProperties{Object}, result }
|
||||
* @param {Boolean} isSuccess
|
||||
* @param {Boolean} isSuccess (false par defaut)
|
||||
*/
|
||||
function sendResponse (res, object, isSuccess = false) {
|
||||
res.status(object.httpStatus || 200).send({ isSuccess, ...object.customProperties, result: object.result });
|
||||
|
Reference in New Issue
Block a user