[Fix #5] User position parameter

develop
sundowndev 2018-09-03 15:36:47 +02:00
parent 1a463c718b
commit 17f951dcbd
1 changed files with 27 additions and 40 deletions

View File

@ -1,15 +1,12 @@
"use strict"; "use strict";
//import ApiClient from './src/ApiClient'; import Position from './src/Position';
//import JsonResponse from './src/JsonResponse';
const express = require('express'); const express = require('express');
const bodyParser = require('body-parser');
const app = express(); const app = express();
//const api = new ApiClient();
//const json = new JsonResponse();
const axios = require('axios'); const axios = require('axios');
const position = new Position();
/** /**
* Config * Config
@ -23,26 +20,6 @@ const port = 3000;
*/ */
const ApiUrl = 'https://jsonplaceholder.typicode.com'; 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 * Routes
*/ */
@ -67,13 +44,18 @@ app.get('/posts', async (req, res) => {
try { try {
const posts = await axios(PostQuery); const posts = await axios(PostQuery);
const formattedPosts = posts.data.map(async (post) => { const promises = posts.data.map(async (post) => {
const userId = post.userId; const UserId = post.userId;
const user = await axios(ApiUrl + '/users/' + userId); const User = await axios(ApiUrl + '/users/' + UserId);
if (req.query.userPos && checkPos(user.data.address.geo) !== req.query.userPos) { if (req.query.userPos && !position.CheckPos(User.data.address.geo, req.query.userPos)) {
//TODO: delete current element console.log({
return {}; 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'); const commentsCount = await axios(ApiUrl + '/posts/' + post.id + '/comments');
@ -84,20 +66,25 @@ app.get('/posts', async (req, res) => {
body: '<p>' + post.body + '</p>', body: '<p>' + post.body + '</p>',
comments_count: commentsCount.data.length, comments_count: commentsCount.data.length,
user: { user: {
id: user.data.id, id: User.data.id,
firstname: user.data.name, firstname: User.data.name,
lastname: user.data.name, lastname: User.data.name,
email: user.data.email, email: User.data.email,
geo: user.data.address.geo geo: User.data.address.geo
} }
}; };
}); });
const response = await Promise.all(formattedPosts); const formattedPosts = await Promise.all(promises);
return res.json({data: response}) const data = formattedPosts.filter((p) => {
} catch(err){ return (p !== null);
return res.json({error: err}) });
return res.json({data: data})
} catch (err) {
console.error(err);
return res.json({error: {code: 500, message: err}})
} }
}); });