From 6588d74e3a4848fb067a9270ef812ef5abd0a14c Mon Sep 17 00:00:00 2001 From: harriet-the-spy Date: Wed, 9 Dec 2020 14:38:19 -0800 Subject: [PATCH] initial feedback DO NOT MERGE (has not been thoroughly validated) --- __tests__/app.test.js | 15 ++++++++------- lib/app.js | 41 +++++++++++++++++++++-------------------- lib/mungingFunctions.js | 22 ++++++++++++---------- utils.js | 12 ++++++------ 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/__tests__/app.test.js b/__tests__/app.test.js index d471563..929f297 100644 --- a/__tests__/app.test.js +++ b/__tests__/app.test.js @@ -6,6 +6,7 @@ const fakeRequest = require('supertest'); const app = require('../lib/app'); const client = require('../lib/client'); +// looks great--i love to see so many tested routes! describe('app routes', () => { describe('routes', () => { let token; @@ -31,7 +32,7 @@ describe('app routes', () => { return client.end(done); }); - test('adds 2 locations to Jon\'s favorite list POST and GET', async () => { + test('adds 2 locations to Jon\'s favorite list POST and GET', async() => { const expectation = [ { @@ -76,7 +77,7 @@ describe('app routes', () => { expect(data.body).toEqual(expectation); }); - test.skip('deletes 1 location from Jon\'s favorite list DELETE', async () => { + test.skip('deletes 1 location from Jon\'s favorite list DELETE', async() => { const expectation = [ { @@ -104,7 +105,7 @@ describe('app routes', () => { expect(data.body).toEqual(expectation); }); - test('fetch Spin data GET', async () => { + test('fetch Spin data GET', async() => { const expectation = [ { @@ -123,7 +124,7 @@ describe('app routes', () => { expect(data.body[0]).toEqual(expectation[0]); }); - test('fetch lime data GET', async () => { + test('fetch lime data GET', async() => { const expectation = [ { @@ -142,7 +143,7 @@ describe('app routes', () => { expect(data.body[0]).toEqual(expectation[0]); }); - test('fetch nike data GET', async () => { + test('fetch nike data GET', async() => { const expectation = [ { @@ -161,7 +162,7 @@ describe('app routes', () => { expect(data.body[0]).toEqual(expectation[0]); }); - test('fetch trimet data GET', async () => { + test('fetch trimet data GET', async() => { const expectation = { @@ -194,7 +195,7 @@ describe('app routes', () => { expect(data.body).toEqual(expectation); }); - test('fetch trimet stop data GET', async () => { + test('fetch trimet stop data GET', async() => { const expectation = { diff --git a/lib/app.js b/lib/app.js index 154b945..d8cf46c 100644 --- a/lib/app.js +++ b/lib/app.js @@ -10,6 +10,7 @@ const createAuthRoutes = require('./auth/create-auth-routes'); // const favorites = require('../data/favorites.js'); const { scooterMunger } = require('./mungingFunctions.js'); +// nice routes! not a lot to say here app.use(cors()); app.use(express.json()); app.use(express.urlencoded({ extended: false })); @@ -33,18 +34,18 @@ app.get('/api/test', (req, res) => { }); // Routes for favorites locations -app.get('/api/favorites', async (req, res) => { +app.get('/api/favorites', async(req, res) => { try { const data = await client.query('SELECT * from favorites WHERE favorites.owner_id = $1', [req.userId]); res.json(data.rows); - } catch (e) { + } catch(e) { res.status(500).json({ error: e.message }); } }); -app.post('/api/favorites', async (req, res) => { +app.post('/api/favorites', async(req, res) => { try { const newName = req.body.name; @@ -56,26 +57,26 @@ app.post('/api/favorites', async (req, res) => { const data = await client.query(`INSERT into favorites (name, lat, lng, address, owner_id) VALUES ($1, $2, $3, $4, $5) RETURNING * `, - [newName, newLat, newLng, newAddress, newOwnerId]); + [newName, newLat, newLng, newAddress, newOwnerId]); res.json(data.rows[0]); - } catch (e) { + } catch(e) { res.status(500).json({ error: e.message }); } }); -app.delete('/api/favorites/:id', async (req, res) => { +app.delete('/api/favorites/:id', async(req, res) => { try { const favoriteId = req.params.id; const data = await client.query(` DELETE from favorites WHERE favorites.owner_id=$1 AND favorites.id=$2`, - [req.userId, favoriteId]); + [req.userId, favoriteId]); res.json(data.rows[0]); - } catch (e) { + } catch(e) { res.status(500).json({ error: e.message }); } @@ -83,7 +84,7 @@ app.delete('/api/favorites/:id', async (req, res) => { // Routes for transportation modes -app.get('/api/spin', async (req, res) => { +app.get('/api/spin', async(req, res) => { try { const URL = 'https://web.spin.pm/api/gbfs/v1/portland/free_bike_status'; @@ -92,13 +93,13 @@ app.get('/api/spin', async (req, res) => { const filteredArray = scooterMunger(response, req.query.lat, req.query.lon); res.json(filteredArray); - } catch (e) { + } catch(e) { res.status(500).json({ error: e.message }); } }); -app.get('/api/lime', async (req, res) => { +app.get('/api/lime', async(req, res) => { //sample request http://localhost:7890/api/lime?lat=45.523&lon=-122.680 try { const URL = 'https://data.lime.bike/api/partners/v1/gbfs/portland/free_bike_status'; @@ -108,13 +109,13 @@ app.get('/api/lime', async (req, res) => { const filteredArray = scooterMunger(response, req.query.lat, req.query.lon); res.json(filteredArray); - } catch (e) { + } catch(e) { res.status(500).json({ error: e.message }); } }); -app.get('/api/nike', async (req, res) => { +app.get('/api/nike', async(req, res) => { try { const URL = 'https://gbfs.biketownpdx.com/gbfs/en/free_bike_status.json'; @@ -123,33 +124,33 @@ app.get('/api/nike', async (req, res) => { const filteredArray = scooterMunger(response, req.query.lat, req.query.lon); res.json(filteredArray); - } catch (e) { + } catch(e) { res.status(500).json({ error: e.message }); } }); -app.get('/api/trimet', async (req, res) => { +app.get('/api/trimet', async(req, res) => { try { const URL = `https://developer.trimet.org/ws/V1/stops?appID=${process.env.TRIMET_APP_ID}&ll=${req.query.lat},${req.query.lng}&feet=1000`; const response = await request.get(URL); res.json(response); - } catch (e) { + } catch(e) { res.status(500).json({ error: e.message }); } }); -app.get('/api/trimet/detail', async (req, res) => { +app.get('/api/trimet/detail', async(req, res) => { try { const URL = `https://developer.trimet.org/ws/v2/arrivals?appID=${process.env.TRIMET_APP_ID}&locIDs=${req.query.locid}`; const response = await request.get(URL); res.json(response); - } catch (e) { + } catch(e) { res.status(500).json({ error: e.message }); } @@ -158,7 +159,7 @@ app.get('/api/trimet/detail', async (req, res) => { // Route to retrieve lat and lng of address -app.get('/api/location', async (req, res) => { +app.get('/api/location', async(req, res) => { try { const URL = `https://us1.locationiq.com/v1/search.php?key=${process.env.LOCATION_IQ_KEY}&q=${req.query.search}&format=json`; @@ -166,7 +167,7 @@ app.get('/api/location', async (req, res) => { const newResponse = mungeLocation(response.body); res.json(newResponse); - } catch (e) { + } catch(e) { res.status(500).json({ error: e.message }); } diff --git a/lib/mungingFunctions.js b/lib/mungingFunctions.js index 5fe9830..e608f1e 100644 --- a/lib/mungingFunctions.js +++ b/lib/mungingFunctions.js @@ -1,17 +1,19 @@ function scooterMunger(response, latitude, longitude) { const preMungedResponse = JSON.parse(response.text).data.bikes; - const filteredArray = []; //Use 0.003degrees as 300m in lat/long coordinates alchemy coords 45.5233858, -122.6809206 - preMungedResponse.map(oneScoot => { - if(Number(oneScoot.lat) < (Number(latitude) + 0.003) && Number(oneScoot.lat) > (Number(latitude) - 0.003) && Number(oneScoot.lon) < (Number(longitude) + 0.003) && Number(oneScoot.lon) > (Number(longitude) - 0.003)) { - const filteredObj = {}; - filteredObj.lat = oneScoot.lat; - filteredObj.lon = oneScoot.lon; - filteredArray.push(filteredObj); - } - }); - return filteredArray; + // this could have been managed with a .filter and a .map. right now you're basically using .map as a foreach, instead of using the array which .map returns. + return preMungedResponse + .filter(oneScoot => + Number( + oneScoot.lat) < (Number(latitude) + 0.003) + && Number(oneScoot.lat) > (Number(latitude) - 0.003) + && Number(oneScoot.lon) < (Number(longitude) + 0.003) + && Number(oneScoot.lon) > (Number(longitude) - 0.003) + ).map(oneScoot => ({ + lat: oneScoot.lat, + lon: oneScoot.lon + })); } module.exports = { diff --git a/utils.js b/utils.js index c1b777c..40b9729 100644 --- a/utils.js +++ b/utils.js @@ -1,11 +1,11 @@ function mungeLocation(location) { - return { - lat: location[0].lat, - lng: location[0].lon - } + return { + lat: location[0].lat, + lng: location[0].lon + }; } module.exports = { - mungeLocation -}; \ No newline at end of file + mungeLocation +};