Skip to content

Commit

Permalink
refactored reverseGeo and config
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarieay committed Mar 2, 2022
1 parent 5ef09cf commit 82f1eca
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
28 changes: 1 addition & 27 deletions controllers/campgrounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,12 @@ const { cloudinary } = require("../cloudinary")
const mbxGeocoding = require("@mapbox/mapbox-sdk/services/geocoding");
const mapBoxToken = process.env.MAPBOX_TOKEN;
const axios = require("axios");
const key = process.env.API_KEY;
const geocoder = mbxGeocoding({ accessToken: mapBoxToken });
const {config, reverseGeo} = require("../tools/index");
/*
** TODO:IMPROVE ACCESSIBLITY
** AFTER NEED TO REFACTOR ASYNCS TO MIDDLEWARE
*/
//SETUP A COOKIE FOR SEARCH MODE
mbxGeocoding({ accessToken: mapBoxToken });
const config = {
params:
{
api_key : key
}
};
const reverseGeo = async (coordinates) => {
try {
const geoData = await geocoder.reverseGeocode({
query: coordinates,
limit: 1
}).send()

if(geoData.body.features[0]){
return geoData.body.features[0].text;
} else{
return 'NO LOCATION'
}
} catch (error) {
console.log("ERROR!:", error)
}
}

//TODO: MAKE A MIDDLEWARE FOR RENDERING INDEX
module.exports.index = async (req, res) => {
const result = {};
result.results = [];
Expand Down
1 change: 1 addition & 0 deletions seeds/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const axios = require("axios");
const key = process.env.API_KEY;
const mainAuth = process.env.OWNER_ID;
const { cloudinary } = require("../cloudinary");
const {reverseGeo} = require("../tools/index");
// const mainAuth = '62040b04c7e98a10d8c2d8ac';

//get the model
Expand Down
28 changes: 28 additions & 0 deletions tools/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const mbxGeocoding = require("@mapbox/mapbox-sdk/services/geocoding");
const mapBoxToken = process.env.MAPBOX_TOKEN;
const key = process.env.API_KEY;
const geocoder = mbxGeocoding({ accessToken: mapBoxToken });

module.exports.config = {
params:
{
api_key : key
}
};

module.exports.reverseGeo = async (coordinates) => {
try {
const geoData = await geocoder.reverseGeocode({
query: coordinates,
limit: 1
}).send()

if(geoData.body.features[0]){
return geoData.body.features[0].text;
} else{
return 'NO LOCATION'
}
} catch (error) {
console.log("ERROR!:", error)
}
}

0 comments on commit 82f1eca

Please sign in to comment.