-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from MatheusCoxxxta/development
Development -> deploy heroku
Showing
10 changed files
with
365 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,59 @@ | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const bcrypt = require('bcryptjs'); | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const User = mongoose.model('User'); | ||
|
||
const authConfig = require('../../config/auth'); | ||
const crypto = require('crypto'); | ||
|
||
function generateToken(params = {}) { | ||
return jwt.sign(params, authConfig.secret, { | ||
expiresIn: 86400 | ||
}) | ||
} | ||
|
||
|
||
module.exports = { | ||
async register (req, res) { | ||
const { email } = req.body; | ||
|
||
try { | ||
if(await User.findOne({ email })) { | ||
return res.status(400).send({ message: 'User already exists' }) | ||
} | ||
const user = await User.create(req.body) | ||
user.password = undefined | ||
|
||
return res.send(['Success', { | ||
user, | ||
token: generateToken({ id: user.id }) | ||
}]) | ||
} catch (error) { | ||
return res.status(400).send({ message: error }); | ||
} | ||
}, | ||
|
||
async auth (req, res) { | ||
const { email, password } = req.body; | ||
|
||
try { | ||
const user = await await User.findOne({ email }).select('+password') | ||
|
||
if(!user) return res.status(400).send({ message: 'User not found'}) | ||
|
||
if(!await bcrypt.compare(password, user.password)) return res.status(400).send({ message: 'Invalid password'}) | ||
|
||
user.password = undefined | ||
|
||
return res.send({ | ||
userId: user.id, | ||
token: generateToken({ id: user.id}) | ||
}) | ||
} catch (error) { | ||
return res.send(error) | ||
} | ||
|
||
} | ||
} |
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,73 @@ | ||
const mongoose = require('mongoose') | ||
|
||
const Pet = mongoose.model('Pet') | ||
|
||
|
||
module.exports = { | ||
async index (req, res) { | ||
const pets = await Pet.find().sort({ 'createdAt': -1 }) | ||
|
||
return res.json(pets) | ||
}, | ||
|
||
async show (req, res) { | ||
const pet = await Pet.findById(req.params.id) | ||
|
||
return res.json(pet) | ||
}, | ||
|
||
async store (req, res) { | ||
const { name, type, color, age, born, breed, castrationDate, owner, ownerId } = req.body; | ||
|
||
try { | ||
const pet = await Pet.create({ | ||
name, | ||
type, | ||
color, | ||
age, | ||
born, | ||
breed, | ||
castrationDate, | ||
owner, | ||
ownerId | ||
}) | ||
return res.json(pet) | ||
} catch (error) { | ||
return res.json(error) | ||
} | ||
}, | ||
|
||
async update (req, res) { | ||
const { name, type, color, age, born, breed, castrationDate, owner, ownerId } = req.body; | ||
|
||
try { | ||
const pet = await Pet.findByIdAndUpdate(req.params.id, { | ||
name, | ||
type, | ||
color, | ||
age, | ||
born, | ||
breed, | ||
castrationDate, | ||
owner, | ||
ownerId | ||
}, { new: true }) | ||
|
||
return res.json(pet) | ||
} catch (error) { | ||
return res.json(error) | ||
} | ||
|
||
}, | ||
|
||
async destroy (req, res) { | ||
try { | ||
const pet = await Pet.findByIdAndRemove(req.params.id) | ||
|
||
return res.json({ message: "Success" }) | ||
} catch (error) { | ||
return res.json(error) | ||
} | ||
|
||
} | ||
} |
Empty file.
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,8 +1,22 @@ | ||
const express = require('express') | ||
const routes = express.Router() | ||
|
||
const AuthController = require('../Controllers/Auth/Auth'); | ||
const PetController = require('../Controllers/PetController'); | ||
|
||
routes.get('/', async(req, res) => { | ||
res.send({ 'message': 'Bem vindo a API do iPet'}) | ||
}) | ||
|
||
routes.post('/auth/register', AuthController.register) | ||
routes.post('/auth/login', AuthController.auth) | ||
|
||
|
||
|
||
routes.get('/pet', PetController.index) | ||
routes.get('/pet/show/:id', PetController.show) | ||
routes.post('/pet/create', PetController.store) | ||
routes.put('/pet/update/:id', PetController.update) | ||
routes.delete('/pet/destroy/:id', PetController.destroy) | ||
|
||
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 @@ | ||
{ | ||
"secret": "99698aea920c3886528f05fb49c12385" | ||
} |
Oops, something went wrong.