Skip to content

Commit

Permalink
Initial commit of utils.js
Browse files Browse the repository at this point in the history
- Added helper functions to validate post data and to transforma geojson data.
- Added helper functions to apagon and lugar post endpoints
  • Loading branch information
Froilan Irizarry committed Apr 14, 2019
1 parent b37da3a commit a00ef01
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const getApagonesDBClient = require('./apagones-client');
const { validatePostData,transformGeoData } = require('./utils')

module.exports = {
getApagonesDBClient
getApagonesDBClient,
validatePostData,
transformGeoData
}
38 changes: 38 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function validatePostData(data) {
if(data.hasOwnProperty('location')) {
if(!data.location.hasOwnProperty('lat')) {
throw Error('`lat` property missing from location');
}
if(!data.location.hasOwnProperty('lng')) {
throw Error('`lat` property missing from location');
}
} else {
throw Error('`location` property is required');
}

if(!data.hasOwnProperty('type')) {
throw Error('`type` property is required');
}
if(!data.hasOwnProperty('source')) {
throw Error('`source` property is required');
}

return true;
}

function transformGeoData({ geoDataType="Point", data }) {
const transformedData = data;
const { lat, lng } = data.location;

transformedData.location = {
type: geoDataType,
coordinates: [ lng, lat ]
}

return transformedData;
}

module.exports = {
validatePostData,
transformGeoData
}
17 changes: 11 additions & 6 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const express = require('express');
const apagonesCollection = 'apagones';
const lugaresCollection = 'lugares';
const router = express.Router();
const { getApagonesDBClient }= require('../lib');
const {
getApagonesDBClient,
validatePostData,
transformGeoData
} = require('../lib');

/* GET home page. */
router.get('/', function(req, res, next) {
Expand All @@ -17,20 +21,19 @@ router.get('/apagon', async (req, res, next) => {
res.json({ data: results });
});


router.post('/apagon', async (req, res, next) => {
const dbClient = await getApagonesDBClient();
const data = req.body;
try {

const results = await dbClient.insert(apagonesCollection, data);
validatePostData(data);
const transformedData = transformGeoData({ data } );
const results = await dbClient.insert(apagonesCollection, transformedData);
res.json({ data: results });
} catch(error) {
console.error(error);
}
});


router.get('/apagon/:id', async (req, res, next) => {
const dbClient = await getApagonesDBClient();
const results = await dbClient.find(apagonesCollection ,{
Expand Down Expand Up @@ -60,7 +63,9 @@ router.post('/lugar', async (req, res, next) => {
const dbClient = await getApagonesDBClient();
const data = req.body;
try {
const results = await dbClient.insert(lugaresCollection, data);
validatePostData(data);
const transformedData = transformGeoData({ data } );
const results = await dbClient.insert(lugaresCollection, transformedData);
res.json({ data: results });
} catch (error) {
console.error(error);
Expand Down

0 comments on commit a00ef01

Please sign in to comment.