Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sobreirami committed Jan 15, 2020
0 parents commit 51683b7
Show file tree
Hide file tree
Showing 10 changed files with 1,418 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
node_modules/
18 changes: 18 additions & 0 deletions package.json
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"
}
}
95 changes: 95 additions & 0 deletions src/controllers/DevController.js
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);
},

};
31 changes: 31 additions & 0 deletions src/controllers/SearchController.js
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
});

}
};
20 changes: 20 additions & 0 deletions src/index.js
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);
16 changes: 16 additions & 0 deletions src/models/Dev.js
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);
15 changes: 15 additions & 0 deletions src/models/utils/PointSchema.js
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;
20 changes: 20 additions & 0 deletions src/routes.js
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;
3 changes: 3 additions & 0 deletions src/utils/parseStringAsArray.js
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());
}
Loading

0 comments on commit 51683b7

Please sign in to comment.