-
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
9 changed files
with
157 additions
and
6 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,6 +1,7 @@ | ||
{ | ||
"dependencies": { | ||
"axios": "^0.19.0", | ||
"cors": "^2.8.5", | ||
"express": "^4.17.1", | ||
"mongoose": "^5.6.9" | ||
}, | ||
|
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,21 @@ | ||
const Dev = require('../models/Dev'); | ||
|
||
module.exports = { | ||
async store(req, res) { | ||
const { devId } = req.params; | ||
const { user } = req.headers; | ||
|
||
const loggedDev = await Dev.findById(user); | ||
const targetDev = await Dev.findById(devId); | ||
|
||
if (!targetDev) { | ||
return res.status(400).json({ error: 'Dev not Exists' }); | ||
} | ||
|
||
loggedDev.deslikes.push(targetDev._id); | ||
|
||
await loggedDev.save(); | ||
|
||
return res.json(loggedDev); | ||
}, | ||
}; |
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,42 @@ | ||
const api = require('../services/api'); | ||
const Dev = require('../models/Dev.js'); | ||
|
||
module.exports = { | ||
async index(req, res) { | ||
const { user } = req.headers; | ||
|
||
const loggedDev = await Dev.findById(user); | ||
|
||
const users = await Dev.find({ | ||
$and: [ | ||
{ _id: { $ne: user } }, | ||
{ _id: { $nin: loggedDev.likes } }, | ||
{ _id: { $nin: loggedDev.dislikes } }, | ||
], | ||
}); | ||
|
||
return res.json(users); | ||
}, | ||
async store(req, res) { | ||
const { username } = req.body; | ||
|
||
const userExist = await Dev.findOne({ user: username }); | ||
|
||
if (userExist) { | ||
return res.json(userExist); | ||
} | ||
|
||
const response = await api.get(`/users/${username}`); | ||
|
||
const { name, bio, avatar_url: avatar } = response.data; | ||
|
||
const dev = await Dev.create({ | ||
name, | ||
user: username, | ||
bio, | ||
avatar, | ||
}); | ||
|
||
return res.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,29 @@ | ||
const Dev = require('../models/Dev'); | ||
|
||
module.exports = { | ||
async store(req, res) { | ||
const { devId } = req.params; | ||
const { user } = req.headers; | ||
|
||
if (devId === user) { | ||
return res.status(400).json({ error: "You can't like yourself" }); | ||
} | ||
|
||
const loggedDev = await Dev.findById(user); | ||
const targetDev = await Dev.findById(devId); | ||
|
||
if (!targetDev) { | ||
return res.status(400).json({ error: 'Dev not Exists' }); | ||
} | ||
|
||
if (targetDev.likes.includes(loggedDev._id)) { | ||
console.log('DEU MATCH'); | ||
} | ||
|
||
loggedDev.likes.push(targetDev._id); | ||
|
||
await loggedDev.save(); | ||
|
||
return res.json(loggedDev); | ||
}, | ||
}; |
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,36 @@ | ||
const { Schema, model } = require('mongoose'); | ||
|
||
const DevSchema = new Schema( | ||
{ | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
user: { | ||
type: String, | ||
required: true, | ||
}, | ||
bio: String, | ||
avatar: { | ||
type: String, | ||
required: true, | ||
}, | ||
likes: [ | ||
{ | ||
type: Schema.Types.ObjectId, | ||
ref: 'devs', | ||
}, | ||
], | ||
dislikes: [ | ||
{ | ||
type: Schema.Types.ObjectId, | ||
ref: 'devs', | ||
}, | ||
], | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
|
||
module.exports = model('devs', 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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
const { Router } = require('express'); | ||
|
||
const DevController = require('./controllers/DevController'); | ||
const LikeController = require('./controllers/LikeController'); | ||
const DesLikeController = require('./controllers/DesLikeController'); | ||
|
||
const routes = Router(); | ||
|
||
routes.get('/', (req, res) => { | ||
res.json({ ok: true }); | ||
}); | ||
routes.get('/devs', DevController.index); | ||
routes.post('/devs', DevController.store); | ||
routes.post('/devs/:devId/likes', LikeController.store); | ||
routes.post('/devs/:devId/deslikes', DesLikeController.store); | ||
|
||
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 |
---|---|---|
@@ -1,10 +1,17 @@ | ||
const express = require('express'); | ||
const cors = require('cors'); | ||
const mongoose = require('mongoose'); | ||
|
||
const server = express(); | ||
|
||
const routes = require('./routes'); | ||
|
||
mongoose.connect( | ||
'mongodb+srv://gugadavi:[email protected]/tindev?retryWrites=true&w=majority', | ||
{ useNewUrlParser: true } | ||
); | ||
|
||
server.use(cors()); | ||
server.use(express.json()); | ||
server.use(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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
const axios = require('axios'); | ||
|
||
const api = axios.create({ | ||
baseURL: '', | ||
baseURL: 'https://api.github.com', | ||
}); | ||
|
||
module.exports = api; |
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