|
| 1 | +const querystring = require('querystring'); |
| 2 | + |
1 | 3 | const bcrypt = require('bcrypt'); |
2 | 4 | const { get } = require('lodash'); |
3 | 5 | const uuid = require('uuidv4').default; |
| 6 | +const axios = require('axios'); |
4 | 7 |
|
| 8 | +const config = require('./config'); |
5 | 9 | const db = require('./db'); |
6 | 10 |
|
7 | 11 |
|
@@ -92,12 +96,12 @@ const hashPassword = (password) => new Promise((resolve, reject) => ( |
92 | 96 |
|
93 | 97 | const createUser = async (args) => { |
94 | 98 | const { |
95 | | - password, name, username, email, |
| 99 | + password, name, username, email, location, |
96 | 100 | } = get(args, 'input', {}); |
97 | 101 | const hashedPassword = await hashPassword(password); |
98 | 102 | const { rows } = await db.query({ |
99 | | - text: 'INSERT INTO users(id, name, username, email, password_digest) VALUES($1, $2, $3, $4, $5) RETURNING *', |
100 | | - values: [uuid(), name, username, email, hashedPassword], |
| 103 | + text: 'INSERT INTO users(id, name, username, email, password_digest, location) VALUES($1, $2, $3, $4, $5, $6) RETURNING *', |
| 104 | + values: [uuid(), name, username, email, hashedPassword, location], |
101 | 105 | }); |
102 | 106 | const user = rows[0]; |
103 | 107 | delete user.password_digest; |
@@ -125,13 +129,39 @@ const createComment = async (args) => { |
125 | 129 | }; |
126 | 130 |
|
127 | 131 |
|
128 | | -// TODO fire a request to the open weather API to fetch the today's weather |
129 | | -// https://api.openweathermap.org/data/2.5/weather |
130 | | -// "config.openWeatherMapAPIKey" should contain your API key from the environment |
131 | | -// Make sure you only return items that you have declared in your schema! |
132 | | -function getWeather() { |
133 | | - // eslint-disable-next-line no-console |
134 | | - console.log('JS Conf Budapest 2019!'); |
| 132 | +async function getWeather({ location }) { |
| 133 | + const APPID = config.openWeatherMapAPIKey; |
| 134 | + const query = querystring.stringify({ |
| 135 | + q: location, |
| 136 | + units: 'metric', |
| 137 | + APPID, |
| 138 | + }); |
| 139 | + |
| 140 | + const url = new URL('https://api.openweathermap.org/data/2.5/weather'); |
| 141 | + url.search = query; |
| 142 | + |
| 143 | + const { data } = await axios |
| 144 | + .get(url.toString()); |
| 145 | + |
| 146 | + const { |
| 147 | + coord: { |
| 148 | + lat, |
| 149 | + lon, |
| 150 | + }, |
| 151 | + main: { |
| 152 | + humidity, |
| 153 | + temp, |
| 154 | + pressure, |
| 155 | + }, |
| 156 | + } = data; |
| 157 | + |
| 158 | + return { |
| 159 | + lat, |
| 160 | + lon, |
| 161 | + humidity, |
| 162 | + pressure, |
| 163 | + temp, |
| 164 | + }; |
135 | 165 | } |
136 | 166 |
|
137 | 167 | module.exports = { |
|
0 commit comments