mirror of
https://github.com/franjsco/umbrello-api
synced 2025-02-16 11:40:46 +01:00
Rename api module
This commit is contained in:
parent
c20d67455e
commit
47dd4b4a74
@ -1,10 +1,11 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import config from './config';
|
import config from './config';
|
||||||
import logger from './logger';
|
import logger from './logger';
|
||||||
|
import citySchema from './db';
|
||||||
|
|
||||||
|
|
||||||
export const getCurrentWeatherById = (cityId) => {
|
export const getCurrentWeatherById = (cityId) => (
|
||||||
return axios.get(`${config.owm.baseurl}/weather`, {
|
axios.get(`${config.owm.baseurl}/weather`, {
|
||||||
params: {
|
params: {
|
||||||
appid: config.owm.token,
|
appid: config.owm.token,
|
||||||
id: cityId,
|
id: cityId,
|
||||||
@ -29,23 +30,22 @@ export const getCurrentWeatherById = (cityId) => {
|
|||||||
const { status } = error.response;
|
const { status } = error.response;
|
||||||
logger.error(`getCurrentWeatherById (cityId=${cityId}) - Status:${status}`);
|
logger.error(`getCurrentWeatherById (cityId=${cityId}) - Status:${status}`);
|
||||||
throw new Error(status);
|
throw new Error(status);
|
||||||
});
|
})
|
||||||
};
|
);
|
||||||
|
|
||||||
|
|
||||||
export const getForecastWeatherById = (cityId) => {
|
export const getForecastWeatherById = (cityId) => (
|
||||||
return axios.get(`${config.owm.baseurl}/forecast`, {
|
axios.get(`${config.owm.baseurl}/forecast`, {
|
||||||
params: {
|
params: {
|
||||||
appid: config.owm.token,
|
appid: config.owm.token,
|
||||||
id: cityId,
|
id: cityId,
|
||||||
units: config.owm.units,
|
units: config.owm.units,
|
||||||
lang: config.owm.lang,
|
lang: config.owm.lang,
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
const { data } = res;
|
const { data } = res;
|
||||||
let date = new Date(Date.now());
|
let date = new Date(Date.now());
|
||||||
|
|
||||||
date = date.toISOString().substr(0, 10);
|
date = date.toISOString().substr(0, 10);
|
||||||
|
|
||||||
const forecastList = data.list
|
const forecastList = data.list
|
||||||
@ -62,7 +62,7 @@ export const getForecastWeatherById = (cityId) => {
|
|||||||
|
|
||||||
const forecastWeather = {
|
const forecastWeather = {
|
||||||
city: `${data.city.name}, ${data.city.country}`,
|
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,
|
list: forecastList,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -72,5 +72,19 @@ export const getForecastWeatherById = (cityId) => {
|
|||||||
const { status } = error.response;
|
const { status } = error.response;
|
||||||
logger.error(`getCurrentWeatherById (cityId=${cityId}) - Status:${status}`);
|
logger.error(`getCurrentWeatherById (cityId=${cityId}) - Status:${status}`);
|
||||||
throw new Error(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);
|
||||||
|
})
|
||||||
|
);
|
Loading…
x
Reference in New Issue
Block a user