-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
20,029 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
node_modules/**/* | ||
.expo/* | ||
npm-debug.* | ||
*.jks | ||
*.p8 | ||
*.p12 | ||
*.key | ||
*.mobileprovision | ||
*.orig.* | ||
web-build/ | ||
|
||
# macOS | ||
.DS_Store | ||
|
||
node_modules/ | ||
npm-debug.log | ||
yarn-error.log |
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,19 @@ | ||
{ | ||
"name": "backend", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"axios": "^0.19.2", | ||
"cors": "^2.8.5", | ||
"express": "^4.17.1", | ||
"mongoose": "^5.9.25", | ||
"socket.io": "^2.3.0" | ||
}, | ||
"scripts": { | ||
"dev": "nodemon src/index.js" | ||
}, | ||
"devDependencies": { | ||
"nodemon": "^2.0.4" | ||
} | ||
} |
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,56 @@ | ||
const axios = require('axios'); | ||
const Dev = require('../models/Dev'); | ||
const parseStringAsArray = require('../utils/parseStringAsArray'); | ||
const { findConnections, sendMessage } = require('../websocket'); | ||
|
||
module.exports = { | ||
|
||
async index(request, response){ | ||
const devs = await Dev.find(); | ||
|
||
return response.json(devs); | ||
}, | ||
|
||
async store (request, response) { | ||
|
||
const { github_username, techs, latitude, longitude } = request.body; | ||
|
||
let dev = await Dev.findOne({ github_username }); | ||
|
||
if(!dev){ | ||
const apiResponse = await axios.get(`https://api.github.com/users/${github_username}`); | ||
|
||
const { name = login, avatar_url, bio } = apiResponse.data; | ||
|
||
const techsArray = parseStringAsArray(techs); | ||
|
||
const location = { | ||
type: 'Point', | ||
coordinates: [longitude, latitude], | ||
} | ||
|
||
dev = await Dev.create({ | ||
github_username, | ||
name, | ||
avatar_url, | ||
bio, | ||
techs: techsArray, | ||
location, | ||
}); | ||
|
||
// Filtrar as conexões que estão há no máximo 10km de distância | ||
// e que o novo dev tenha pelo menos uma das tecnologias | ||
sendSocketMessageTo = findConnections( | ||
{latitude, longitude}, | ||
techsArray, | ||
) | ||
|
||
sendMessage(sendSocketMessageTo, 'new-dev', dev); | ||
} | ||
|
||
return response.json(dev); | ||
|
||
} | ||
|
||
}; | ||
|
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,43 @@ | ||
const Dev = require('../models/Dev'); | ||
const parseStringAsArray = require('../utils/parseStringAsArray'); | ||
|
||
module.exports = { | ||
|
||
async index(request, response){ | ||
// Buscar devs num raio de 10km | ||
// Filtrar por tecnologias | ||
const { latitude, longitude, techs } = request.query; | ||
|
||
const techsArray = parseStringAsArray(techs); | ||
|
||
const devs = await Dev.find({ | ||
techs: { | ||
$in: techsArray, | ||
}, | ||
location: { | ||
$near: { | ||
$geometry: { | ||
type: 'Point', | ||
coordinates: [longitude, latitude], | ||
}, | ||
$maxDistance: 10000, | ||
}, | ||
}, | ||
}) | ||
|
||
return response.json({ devs }); | ||
|
||
}, | ||
|
||
// Desafio do Di: | ||
// Criar os métodos update() e destroy() | ||
// Não permitir q o user atualize o username pois não faz sentido NTC | ||
|
||
/* async update(){ | ||
}, | ||
async destroy(){ | ||
} */ | ||
} |
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,34 @@ | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const cors = require('cors'); | ||
const http = require('http'); | ||
|
||
const routes = require('./routes'); | ||
const { setupWebsocket } = require('./websocket'); | ||
|
||
const app = express(); | ||
const server = http.Server(app); | ||
|
||
setupWebsocket(server); | ||
|
||
mongoose.connect('mongodb+srv://omnistack:[email protected]/omnistack10', | ||
{ | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
} | ||
); | ||
|
||
app.use(cors()); | ||
app.use(express.json()); | ||
app.use(routes); | ||
|
||
// Métodos HTTP: GET, POST, PUT, DELETE | ||
|
||
// Tipos de parâmetros | ||
|
||
// Query Params: request.query (Filtros, odenação, paginação, ...) | ||
// Route Params: request.params (Identificar um recurso na alteração ou remoção) | ||
// Body Params: request.body (Dados para criação ou alteração de um registro) | ||
|
||
|
||
server.listen(3333); |
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,25 @@ | ||
const mongoose = require('mongoose'); | ||
const PointSchema = require('./utils/PointSchema'); | ||
|
||
mongoose.set('useCreateIndex', true); | ||
|
||
const DevSchema = new mongoose.Schema({ | ||
|
||
name: String, | ||
|
||
github_username: String, | ||
|
||
bio: String, | ||
|
||
avatar_url: String, | ||
|
||
techs: [String], | ||
|
||
location: { | ||
type: PointSchema, | ||
index: '2dsphere' | ||
} | ||
|
||
}); | ||
|
||
module.exports = mongoose.model('Dev', DevSchema); |
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,17 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const PointSchema = new mongoose.Schema({ | ||
|
||
type: { | ||
type: String, | ||
enum: ['Point'], | ||
required: true, | ||
}, | ||
|
||
coordinates: { | ||
type: [Number], | ||
required: true, | ||
}, | ||
}); | ||
|
||
module.exports = PointSchema; |
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,15 @@ | ||
const { Router } = require('express'); | ||
|
||
const DevController = require('./controllers/DevController'); | ||
const SearchController = require('./controllers/SearchController'); | ||
|
||
|
||
const routes = Router(); | ||
|
||
routes.get('/devs', DevController.index); | ||
routes.get('/search', SearchController.index); | ||
|
||
routes.post('/devs', DevController.store); | ||
routes.post('/') | ||
|
||
module.exports = routes; |
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,24 @@ | ||
function deg2rad(deg) { | ||
return deg * (Math.PI/180); | ||
} | ||
|
||
module.exports = function getDistanceFromLatLonInKm(centerCoordinates, pointCoordinates) { | ||
const radius = 6371; | ||
|
||
const { latitude: lat1, longitude: lon1 } = centerCoordinates; | ||
const { latitude: lat2, longitude: lon2 } = pointCoordinates; | ||
|
||
const dLat = deg2rad(lat2-lat1); | ||
const dLon = deg2rad(lon2-lon1); | ||
|
||
const a = | ||
Math.sin(dLat/2) * Math.sin(dLat/2) + | ||
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * | ||
Math.sin(dLon/2) * Math.sin(dLon/2) | ||
; | ||
|
||
const center = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); | ||
const distance = radius * center; | ||
|
||
return distance; | ||
} |
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,3 @@ | ||
module.exports = function parseStringAsArray(arrayAsString){ | ||
return arrayAsString.split(',').map(tech => tech.trim()); | ||
} |
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,39 @@ | ||
const socketio = require('socket.io'); | ||
const parseStringAsArray = require('./utils/parseStringAsArray'); | ||
const calculateDistance = require('./utils/calculateDistance'); | ||
|
||
let io; | ||
|
||
const connections = []; | ||
|
||
exports.setupWebsocket = (server) => { | ||
io = socketio(server); | ||
|
||
io.on('connection', socket => { | ||
const { latitude, longitude, techs } = socket.handshake.query; | ||
|
||
const techsArray = parseStringAsArray(techs); | ||
|
||
connections.push({ | ||
id: socket.id, | ||
coordinates: { | ||
latitude: Number(latitude), | ||
longitude: Number(longitude), | ||
}, | ||
techs: techsArray, | ||
}); | ||
}); | ||
}; | ||
|
||
exports.findConnections = (coordinates, techs) => { | ||
return connections.filter(connection => { | ||
return calculateDistance(coordinates, connection.coordinates) < 10 | ||
&& connection.techs.some(item => techs.includes(item)) | ||
}) | ||
} | ||
|
||
exports.sendMessage = (to, message, data) => { | ||
to.forEach(connection => { | ||
io.to(connection.id).emit(message, data) | ||
}); | ||
} |
Oops, something went wrong.