Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions __tests__/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = [
{
Expand Down Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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 = [
{
Expand All @@ -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 = [
{
Expand All @@ -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 = [
{
Expand All @@ -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 =
{
Expand Down Expand Up @@ -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 =
{
Expand Down
41 changes: 21 additions & 20 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
Expand All @@ -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;
Expand All @@ -56,34 +57,34 @@ 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 });
}
});

// 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';

Expand All @@ -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';
Expand All @@ -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';

Expand All @@ -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 });
}
Expand All @@ -158,15 +159,15 @@ 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`;

const response = await request.get(URL);
const newResponse = mungeLocation(response.body);

res.json(newResponse);
} catch (e) {
} catch(e) {

res.status(500).json({ error: e.message });
}
Expand Down
22 changes: 12 additions & 10 deletions lib/mungingFunctions.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
12 changes: 6 additions & 6 deletions utils.js
Original file line number Diff line number Diff line change
@@ -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
};
mungeLocation
};