Skip to content

Commit

Permalink
Merge pull request #19 from VamosJuntas/30-search-risk
Browse files Browse the repository at this point in the history
30 search risk
  • Loading branch information
tayanefernandes authored Jun 29, 2016
2 parents c4192f9 + 1801bb8 commit 71cea50
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 76 deletions.
24 changes: 10 additions & 14 deletions src/controllers/risks-around.js
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);
});

};
28 changes: 14 additions & 14 deletions src/domains/place.model.js
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);
37 changes: 19 additions & 18 deletions tests/integration/risks-around_spec.js
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();
63 changes: 58 additions & 5 deletions tests/test_setup.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,66 @@
var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost/vamosjuntas_test',function(error, db) {
if (error){
var enviroment = process.env.NODE_ENV || 'test';
var connectionURL = {
test: 'mongodb://localhost/vamosjuntas_test',
app: 'mongodb://localhost/vamosjuntas'
};

MongoClient.connect(connectionURL[enviroment], function(error, db) {
if (error) {
console.log('error mongo');
} else {
var places = [{loc:['-30.060653, -51.171270'],address: 'Rua blá',
reports:[{category:'local deserto', date:'2016-03-16T12:41:51.002Z'}]}];
var places = [{
loc: [-51.196491, -30.042510],
address: 'Rua 1',
reports: [{
category: 'local deserto',
date: '2016-03-16T12:41:51.002Z'
}]
},
{
loc: [-51.209619, -30.059620],
address: 'Rua 2',
reports: [{
category: 'local deserto',
date: '2016-03-16T12:41:51.002Z'
}]
},
{
loc: [-51.230576, -30.072586],
address: 'Rua 3',
reports: [{
category: 'local deserto',
date: '2016-03-16T12:41:51.002Z'
}]
},
{
loc: [-51.195136, -30.017054],
address: 'Rua 4',
reports: [{
category: 'local deserto',
date: '2016-03-16T12:41:51.002Z'
}]
},
{
loc: [-49.549665, -28.688702],
address: 'Rua 5',
reports: [{
category: 'local deserto',
date: '2016-03-16T12:41:51.002Z'
}]
},
{
loc: [-51.169215, -30.056248],
address: 'Av Cristiano Fischer',
reports: [{
category: 'local deserto',
date: '2016-03-16T12:41:51.002Z'
}]
}];

db.collection('places').remove(function() {
db.collection('places').insert(places,function() {
db.collection('places').insert(places, function() {
db.close();
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/app/app_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('App', function () {
expect(place.find).toHaveBeenCalledWith({
loc: {
$near: [1.2, 3.4],
$maxDistance: 100
$maxDistance: 0.008992805755395683
}
});
});
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/controllers/risk-around-spec.js
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.');
});

});
24 changes: 0 additions & 24 deletions tests/unit/controllers/risks-around_spec.js

This file was deleted.

0 comments on commit 71cea50

Please sign in to comment.