From 47dd4b4a74e727d2f09526baa4467715ac263910 Mon Sep 17 00:00:00 2001 From: Francesco Esposito <33671357+frsposito@users.noreply.github.com> Date: Thu, 22 Aug 2019 17:41:54 +0200 Subject: [PATCH] Rename api module --- src/{apiOWM.js => api.js} | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) rename src/{apiOWM.js => api.js} (73%) diff --git a/src/apiOWM.js b/src/api.js similarity index 73% rename from src/apiOWM.js rename to src/api.js index 29b471c..d27bc98 100644 --- a/src/apiOWM.js +++ b/src/api.js @@ -1,10 +1,11 @@ import axios from 'axios'; import config from './config'; import logger from './logger'; +import citySchema from './db'; -export const getCurrentWeatherById = (cityId) => { - return axios.get(`${config.owm.baseurl}/weather`, { +export const getCurrentWeatherById = (cityId) => ( + axios.get(`${config.owm.baseurl}/weather`, { params: { appid: config.owm.token, id: cityId, @@ -29,23 +30,22 @@ export const getCurrentWeatherById = (cityId) => { const { status } = error.response; logger.error(`getCurrentWeatherById (cityId=${cityId}) - Status:${status}`); throw new Error(status); - }); -}; + }) +); -export const getForecastWeatherById = (cityId) => { - return axios.get(`${config.owm.baseurl}/forecast`, { +export const getForecastWeatherById = (cityId) => ( + axios.get(`${config.owm.baseurl}/forecast`, { params: { appid: config.owm.token, id: cityId, units: config.owm.units, lang: config.owm.lang, - } + }, }) .then((res) => { const { data } = res; let date = new Date(Date.now()); - date = date.toISOString().substr(0, 10); const forecastList = data.list @@ -62,7 +62,7 @@ export const getForecastWeatherById = (cityId) => { const forecastWeather = { city: `${data.city.name}, ${data.city.country}`, - rain: forecastList.some((e) => e.flag_rain === 'Y') ? 'Y' : 'N', + rain: forecastList.some((e) => e.rain_flag === 'Y') ? 'Y' : 'N', list: forecastList, }; @@ -72,5 +72,19 @@ export const getForecastWeatherById = (cityId) => { const { status } = error.response; logger.error(`getCurrentWeatherById (cityId=${cityId}) - Status:${status}`); throw new Error(status); - }); -}; + }) +); + +export const getCityIdByName = (cityName) => ( + new Promise((resolve, reject) => { + citySchema.find({ name: new RegExp(`^${cityName}`, 'i') }, { + _id: 0, name: 1, country: 1, id: 1, + }, (err, cities) => { + if (err) { + reject(err); + } + + resolve(cities); + }).sort({ name: 1 }).limit(5); + }) +);