Toggle navigation
Toggle navigation
This project
Loading...
Sign in
황선혁
/
weather_chatbot
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Eric Whale
2022-06-07 15:12:58 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
c12aacad1a82ff1766b9ae6e9ff76ebcf5353330
c12aacad
1 parent
8ef2a7d5
Edit weatherActions.js for code reusability
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
55 deletions
server/actions/weatherActions.js
server/actions/weatherActions.js
View file @
c12aaca
...
...
@@ -9,28 +9,8 @@ const asyncHandler = require("express-async-handler");
// @route Get /api/weather
// @access Public
const
getWeather
=
asyncHandler
(
async
(
req
,
res
)
=>
{
const
token
=
req
.
body
.
token
;
const
decoded
=
jwt
.
decode
(
token
,
{
complete
:
true
});
const
user
=
await
User
.
findById
(
decoded
.
payload
.
id
)
.
select
(
"-password"
)
.
select
(
"-email"
);
const
countryCode
=
user
.
country
;
const
city
=
user
.
city
;
const
limit
=
5
;
try
{
const
metaGeoData
=
await
axios
.
get
(
`http://api.openweathermap.org/geo/1.0/direct?q=
${
city
}
,
${
countryCode
}
&limit=
${
limit
}
&appid=
${
process
.
env
.
OPENWEATHER_API_KEY
}
`
);
const
data
=
metaGeoData
.
data
[
0
];
const
geoData
=
{
lat
:
data
.
lat
,
lon
:
data
.
lon
,
country
:
data
.
country
,
state
:
data
.
state
,
};
const
geoData
=
await
getUser_GeoData
(
req
,
res
);
const
metaData
=
await
axios
.
get
(
`https://api.openweathermap.org/data/2.5/weather?lat=
${
geoData
.
lat
}
&lon=
${
geoData
.
lon
}
&appid=
${
process
.
env
.
OPENWEATHER_API_KEY
}
`
);
...
...
@@ -47,28 +27,8 @@ const getWeather = asyncHandler(async (req, res) => {
// @route GET /api/weather/forecast
// @access Public
const
getForecast
=
asyncHandler
(
async
(
req
,
res
)
=>
{
const
token
=
req
.
body
.
token
;
const
decoded
=
jwt
.
decode
(
token
,
{
complete
:
true
});
const
user
=
await
User
.
findById
(
decoded
.
payload
.
id
)
.
select
(
"-password"
)
.
select
(
"-email"
);
const
countryCode
=
user
.
country
;
const
city
=
user
.
city
;
const
limit
=
5
;
try
{
const
metaGeoData
=
await
axios
.
get
(
`http://api.openweathermap.org/geo/1.0/direct?q=
${
city
}
,
${
countryCode
}
&limit=
${
limit
}
&appid=
${
process
.
env
.
OPENWEATHER_API_KEY
}
`
);
const
data
=
metaGeoData
.
data
[
0
];
const
geoData
=
{
lat
:
data
.
lat
,
lon
:
data
.
lon
,
country
:
data
.
country
,
state
:
data
.
state
,
};
const
geoData
=
await
getUser_GeoData
(
req
,
res
);
const
metaData
=
await
axios
.
get
(
`http://api.openweathermap.org/data/2.5/forecast?lat=
${
geoData
.
lat
}
&lon=
${
geoData
.
lon
}
&appid=
${
process
.
env
.
OPENWEATHER_API_KEY
}
`
);
...
...
@@ -85,6 +45,26 @@ const getForecast = asyncHandler(async (req, res) => {
// @route GET /api/weather/airpollution
// @access Public
const
getAirPollution
=
asyncHandler
(
async
(
req
,
res
)
=>
{
try
{
const
geoData
=
await
getUser_GeoData
(
req
,
res
);
const
metaData
=
await
axios
.
get
(
`http://api.openweathermap.org/data/2.5/air_pollution?lat=
${
geoData
.
lat
}
&lon=
${
geoData
.
lon
}
&appid=
${
process
.
env
.
OPENWEATHER_API_KEY
}
`
);
const
airPollutionData
=
metaData
.
data
;
res
.
json
({
country
:
geoData
.
country
,
state
:
geoData
.
state
,
airPollutionData
,
});
}
catch
(
err
)
{
res
.
status
(
400
);
throw
new
Error
(
"openweathermap API error"
);
}
});
// Get user & geodata
async
function
getUser_GeoData
(
req
,
res
)
{
const
token
=
req
.
body
.
token
;
const
decoded
=
jwt
.
decode
(
token
,
{
complete
:
true
});
const
user
=
await
User
.
findById
(
decoded
.
payload
.
id
)
...
...
@@ -106,22 +86,12 @@ const getAirPollution = asyncHandler(async (req, res) => {
country
:
data
.
country
,
state
:
data
.
state
,
};
const
metaData
=
await
axios
.
get
(
`http://api.openweathermap.org/data/2.5/air_pollution?lat=
${
geoData
.
lat
}
&lon=
${
geoData
.
lon
}
&appid=
${
process
.
env
.
OPENWEATHER_API_KEY
}
`
);
const
airPollutionData
=
metaData
.
data
;
res
.
json
({
country
:
geoData
.
country
,
state
:
geoData
.
state
,
airPollutionData
,
});
return
geoData
;
}
catch
(
err
)
{
res
.
status
(
400
);
throw
new
Error
(
"openweathermap API error"
);
throw
new
Error
(
"openweathermap
Geodata
API error"
);
}
}
);
}
module
.
exports
=
{
getWeather
,
...
...
Please
register
or
login
to post a comment