-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from VamosJuntas/30-search-risk
30 search risk
- Loading branch information
Showing
7 changed files
with
133 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,33 @@ | ||
var Place = require('../domains/place.model'); | ||
var isNumeric = require('is-numeric'); | ||
module.exports = function (request, response) { | ||
|
||
module.exports = function(request, response) { | ||
if (Object.keys(request.params).length === 0) { | ||
return response.send(400, 'Invalid params.'); | ||
} | ||
|
||
if (!isNumeric(request.params.latitude) || !isNumeric(request.params.longitude) ) { | ||
var longitude = request.params.longitude; | ||
var latitute = request.params.latitude; | ||
if (!isNumeric(latitute) || !isNumeric(longitude)) { | ||
return response.send(400, 'Invalid latitude or longitude.'); | ||
} | ||
|
||
var maxDistance = 100; | ||
var coords = []; | ||
coords[0] = request.params.longitude; | ||
coords[1] = request.params.latitude; | ||
|
||
var radiusInKm = 1; | ||
var kilometersPerDegree = 111.2; | ||
Place.find({ | ||
loc: { | ||
$near: coords, | ||
$maxDistance: maxDistance | ||
$near: [longitude, latitute], | ||
$maxDistance: radiusInKm / kilometersPerDegree | ||
} | ||
}).exec(function (err, places) { | ||
}).exec(function(err, places) { | ||
if (err) { | ||
return console.error(err); | ||
} | ||
console.log(places); | ||
console.log(places.length); | ||
|
||
if (places.length === 0) { | ||
return response.send(204); | ||
return response.send(204); | ||
} | ||
|
||
return response.send(places); | ||
}); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
var mongoose = require('mongoose'); | ||
|
||
var placeSchema = mongoose.Schema({ | ||
loc: { | ||
type: [Number], | ||
index: '2d' | ||
}, | ||
address: String, | ||
reports: [ | ||
{ | ||
category: String, | ||
date: Date, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
loc: { | ||
type: [Number], | ||
index: '2d' | ||
}, | ||
address: String, | ||
reports: [ | ||
{ | ||
category: String, | ||
date: Date, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
] | ||
} | ||
] | ||
}); | ||
|
||
module.exports = mongoose.model('Place', placeSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
// var frisby = require('frisby'); | ||
// | ||
// frisby.create('Needs latitude and logitude params') | ||
// .get('http://localhost:8080/risks-around') | ||
// .expectStatus(400) | ||
// .toss(); | ||
// | ||
// | ||
// frisby.create('Search around a place') | ||
// .get('http://localhost:8080/risks-around?latitude=1&longitude=2') | ||
// .expectStatus(200) | ||
// .expectHeaderContains('content-type', 'application/json') | ||
// .toss(); | ||
// | ||
// frisby.create('Should fail gracefully when latitude and longitude does not exists') | ||
// .get('http://localhost:8080/risks-around?latitude=90&longitude=90') | ||
// .expectStatus(204) | ||
// .toss(); | ||
var frisby = require('frisby'); | ||
|
||
var risksAroundUrl = 'http://localhost:8080/risks-around'; | ||
|
||
frisby.create('should have latitude and logitude params') | ||
.get(risksAroundUrl) | ||
.expectStatus(400) | ||
.toss(); | ||
|
||
frisby.create('should return place if in 1km radius') | ||
.get(risksAroundUrl + '?latitude=-30.057725&longitude=-51.175642') | ||
.expectStatus(200) | ||
.expectHeaderContains('content-type', 'application/json') | ||
.toss(); | ||
|
||
frisby.create('should return no content if not in 1km radius') | ||
.get(risksAroundUrl + '?latitude=90&longitude=90') | ||
.expectStatus(204) | ||
.toss(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var riskAround = require('../../../src/controllers/risks-around.js'); | ||
|
||
describe('Create a new risk report', function () { | ||
|
||
beforeEach(function() { | ||
restifyMock = { | ||
next: jasmine.createSpy(), | ||
response: jasmine.createSpyObj('response', ['send']), | ||
request: { | ||
params: {} | ||
} | ||
}; | ||
}); | ||
|
||
it('should return 400 when params does not exist', function() { | ||
riskAround(restifyMock.request, restifyMock.response, restifyMock.next); | ||
expect(restifyMock.response.send).toHaveBeenCalledWith(400, 'Invalid params.'); | ||
}); | ||
|
||
|
||
it('should return 400 when invalid params', function() { | ||
restifyMock.request.params = { | ||
latitude: "a", | ||
longitude:"b" | ||
}; | ||
|
||
riskAround(restifyMock.request, restifyMock.response, restifyMock.next); | ||
expect(restifyMock.response.send).toHaveBeenCalledWith(400, 'Invalid latitude or longitude.'); | ||
}); | ||
|
||
}); |
This file was deleted.
Oops, something went wrong.