backend: Ajout de la fonction weatherRequest + Hotfix: armstrongNumber → NaN

This commit is contained in:
Divlo 2020-03-17 19:37:45 +01:00
parent fd3224ee42
commit 27d6531a98
8 changed files with 61 additions and 3 deletions

1
backend/.gitignore vendored
View File

@ -13,6 +13,7 @@
# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local

View File

@ -1,4 +1,5 @@
/* Modules */
require('dotenv').config();
const express = require('express');
const helmet = require('helmet');
const cors = require('cors');

View File

@ -1,5 +1,6 @@
const config = {
PORT: process.env.PORT || 8080
PORT: process.env.PORT || 8080,
WEATHER_API_KEY: process.env.OpenWeatherMap_API_KEY
};
module.exports = config;

View File

@ -3,6 +3,7 @@ const { convertArabicToRomanOutput, convertRomanToArabicOutput } = require('./ma
const { convertDistanceOutput } = require('./main/convertDistance');
const { convertTemperatureOutput } = require('./main/convertTemperature');
const { armstrongNumberOutput } = require('./main/armstrongNumber');
const { weatherRequestOutput } = require('./main/weatherRequest');
const functionObject = {
randomNumber : randomNumberOutput,
@ -10,7 +11,8 @@ const functionObject = {
convertRomanToArabic: convertRomanToArabicOutput,
convertDistance : convertDistanceOutput,
convertTemperature : convertTemperatureOutput,
armstrongNumber : armstrongNumberOutput
armstrongNumber : armstrongNumberOutput,
weatherRequest : weatherRequestOutput
};
// Choisi la fonction à exécuter

View File

@ -38,7 +38,7 @@ exports.armstrongNumberOutput = (res, argsObject) => {
// Si ce n'est pas un nombre
number = parseInt(number);
if (isNaN(number) && number >= 0) {
if (isNaN(number) || number <= 0) {
return sendResponse(res, { result: "Veuillez rentré un nombre valide.", httpStatus: 400 });
}

View File

@ -0,0 +1,19 @@
const axios = require('axios');
const sendResponse = require('../../utils/sendResponse');
const { requiredFields } = require('../../config/errors');
const { WEATHER_API_KEY } = require('../../config/config');
/* OUTPUTS */
exports.weatherRequestOutput = async (res, argsObject) => {
let { cityName } = argsObject;
// S'il n'y a pas les champs obligatoire
if (!(cityName)) {
return sendResponse(res, requiredFields);
}
// Récupère les données météo grâce à l'API : openweathermap.org.
axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&lang=fr&units=metric&appid=${WEATHER_API_KEY}`)
.then((response) => sendResponse(res, { result: response.data }, true))
.catch(() => sendResponse(res, { result: "La ville n'existe pas (dans l'API de openweathermap.org)." }));
}

View File

@ -58,6 +58,14 @@
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
},
"axios": {
"version": "0.19.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz",
"integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==",
"requires": {
"follow-redirects": "1.5.10"
}
},
"balanced-match": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
@ -342,6 +350,12 @@
"is-obj": "^1.0.0"
}
},
"dotenv": {
"version": "8.2.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz",
"integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==",
"dev": true
},
"duplexer3": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz",
@ -459,6 +473,24 @@
"unpipe": "~1.0.0"
}
},
"follow-redirects": {
"version": "1.5.10",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
"integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
"requires": {
"debug": "=3.1.0"
},
"dependencies": {
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"requires": {
"ms": "2.0.0"
}
}
}
},
"forwarded": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",

View File

@ -10,11 +10,13 @@
"author": "Divlo",
"license": "MIT",
"dependencies": {
"axios": "^0.19.2",
"cors": "^2.8.5",
"express": "^4.17.1",
"helmet": "^3.21.3"
},
"devDependencies": {
"dotenv": "^8.2.0",
"morgan": "^1.9.1",
"nodemon": "^2.0.2"
}