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); + }) +);