From 17f951dcbdce4470ff010fa09a8e42a58d7baba1 Mon Sep 17 00:00:00 2001 From: sundowndev Date: Mon, 3 Sep 2018 15:36:47 +0200 Subject: [PATCH] [Fix #5] User position parameter --- index.js | 67 +++++++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 40 deletions(-) diff --git a/index.js b/index.js index 9e2f231..4fdeda6 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,12 @@ "use strict"; -//import ApiClient from './src/ApiClient'; -//import JsonResponse from './src/JsonResponse'; +import Position from './src/Position'; const express = require('express'); -const bodyParser = require('body-parser'); const app = express(); -//const api = new ApiClient(); -//const json = new JsonResponse(); const axios = require('axios'); +const position = new Position(); /** * Config @@ -23,26 +20,6 @@ const port = 3000; */ const ApiUrl = 'https://jsonplaceholder.typicode.com'; -/** - * Check position - * @param geo - */ -const checkPos = (geo) => { - //TODO: check position properly - switch (geo.lat | geo.lng) { - case geo.lat === 0: - return 'eq'; - case geo.lat === 23 | geo.lng >= 27: - return 'cancer'; - case geo.lat === 23 | geo.lng <= 27: - return 'cap'; - case geo.lat === 66 | geo.lng >= 33: - return 'arctic'; - case geo.lat === 66 | geo.lng <= 33: - return 'antarctic'; - } -}; - /** * Routes */ @@ -67,13 +44,18 @@ app.get('/posts', async (req, res) => { try { const posts = await axios(PostQuery); - const formattedPosts = posts.data.map(async (post) => { - const userId = post.userId; - const user = await axios(ApiUrl + '/users/' + userId); + const promises = posts.data.map(async (post) => { + const UserId = post.userId; + const User = await axios(ApiUrl + '/users/' + UserId); - if (req.query.userPos && checkPos(user.data.address.geo) !== req.query.userPos) { - //TODO: delete current element - return {}; + if (req.query.userPos && !position.CheckPos(User.data.address.geo, req.query.userPos)) { + console.log({ + param: req.query.userPos, + CheckPos: position.CheckPos(User.data.address.geo, 'eq'), + userPos: User.data.address.geo.lat + }); + + return null; } const commentsCount = await axios(ApiUrl + '/posts/' + post.id + '/comments'); @@ -84,20 +66,25 @@ app.get('/posts', async (req, res) => { body: '

' + post.body + '

', comments_count: commentsCount.data.length, user: { - id: user.data.id, - firstname: user.data.name, - lastname: user.data.name, - email: user.data.email, - geo: user.data.address.geo + id: User.data.id, + firstname: User.data.name, + lastname: User.data.name, + email: User.data.email, + geo: User.data.address.geo } }; }); - const response = await Promise.all(formattedPosts); + const formattedPosts = await Promise.all(promises); - return res.json({data: response}) - } catch(err){ - return res.json({error: err}) + const data = formattedPosts.filter((p) => { + return (p !== null); + }); + + return res.json({data: data}) + } catch (err) { + console.error(err); + return res.json({error: {code: 500, message: err}}) } });