-
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
0 parents
commit 51683b7
Showing
10 changed files
with
1,418 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,2 @@ | ||
.env | ||
node_modules/ |
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,18 @@ | ||
{ | ||
"name": "backend", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"dev": "nodemon src/index.js" | ||
}, | ||
"dependencies": { | ||
"axios": "^0.19.1", | ||
"dotenv": "^8.2.0", | ||
"express": "^4.17.1", | ||
"mongoose": "^5.8.7" | ||
}, | ||
"devDependencies": { | ||
"nodemon": "^2.0.2" | ||
} | ||
} |
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,95 @@ | ||
const axios = require('axios'); | ||
const Dev = require('../models/Dev'); | ||
const parseStringAsArray = require('../utils/parseStringAsArray'); | ||
|
||
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 responseGithub = await axios.get(`https://api.github.com/users/${github_username}`); | ||
|
||
const { name = login, avatar_url, bio } = responseGithub.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 | ||
}); | ||
} | ||
|
||
return response.json(dev); | ||
}, | ||
|
||
async update(request, response) { | ||
const github_username = request.params.github_username; | ||
|
||
let dev = await Dev.findOne({ github_username }); | ||
|
||
if(dev) { | ||
|
||
const { | ||
techs = dev.techs, | ||
name = dev.name, | ||
avatar_url = dev.avatar_url, | ||
bio = dev.bio, | ||
latitude = dev.location.coordinates[1], | ||
longitude = dev.location.coordinates[0] | ||
} = request.body; | ||
|
||
const techsArray = parseStringAsArray(techs); | ||
|
||
const location = { | ||
type: 'Point', | ||
coordinates: [ longitude, latitude ], | ||
}; | ||
|
||
dev = await Dev.update({ | ||
name, | ||
avatar_url, | ||
bio, | ||
techs: techsArray, | ||
location | ||
}); | ||
|
||
} | ||
|
||
return response.json(dev); | ||
|
||
}, | ||
|
||
async destroy(request, response) { | ||
|
||
const github_username = request.params.github_username; | ||
|
||
let dev = await Dev.findOne({ github_username }); | ||
|
||
if(dev) { | ||
dev.deleteOne(); | ||
} | ||
|
||
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,31 @@ | ||
const Dev = require('../models/Dev'); | ||
const parseStringAsArray = require('../utils/parseStringAsArray'); | ||
|
||
module.exports = { | ||
async index(request, response) { | ||
|
||
const { latitude, longitude, techs} = request.query; | ||
|
||
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: devs | ||
}); | ||
|
||
} | ||
}; |
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,20 @@ | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const routes = require('./routes'); | ||
require('dotenv').config() | ||
|
||
const app = express(); | ||
|
||
const dbUserName = process.env.DB_USERNAME; | ||
const dbPassword = process.env.DB_PASSWORD; | ||
const dbName = process.env.DB_NAME; | ||
|
||
mongoose.connect(`mongodb+srv://${dbUserName}:${dbPassword}@cluster0-5v7r9.mongodb.net/${dbName}?retryWrites=true&w=majority`, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}); | ||
|
||
app.use(express.json()); | ||
app.use(routes); | ||
|
||
app.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,16 @@ | ||
const mongoose = require('mongoose'); | ||
const PointSchema = require('./utils/PointSchema'); | ||
|
||
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,15 @@ | ||
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,20 @@ | ||
const { Router } = require('express'); | ||
const DevController = require('./controllers/DevController'); | ||
const SearchController = require('./controllers/SearchController'); | ||
|
||
const routes = Router(); | ||
|
||
routes.get('/', (request, response) => { | ||
return response.json({ | ||
message: 'Hello OmniStack 10', | ||
}); | ||
}); | ||
|
||
routes.get('/devs', DevController.index); | ||
routes.post('/devs', DevController.store); | ||
routes.put('/devs/:github_username', DevController.update); | ||
routes.delete('/devs/:github_username', DevController.destroy); | ||
|
||
routes.get('/search', SearchController.index); | ||
|
||
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,3 @@ | ||
module.exports = function parseStringAsArray(arrayAsString) { | ||
return arrayAsString.split(",").map(tech => tech.trim()); | ||
} |
Oops, something went wrong.