Skip to content

Instantly share code, notes, and snippets.

@leleofg
Last active October 22, 2019 14:31
Show Gist options
  • Select an option

  • Save leleofg/1f737789168a06b29c97475962ebf407 to your computer and use it in GitHub Desktop.

Select an option

Save leleofg/1f737789168a06b29c97475962ebf407 to your computer and use it in GitHub Desktop.
Demonstração do crud de usuários com async/await em node.js para o post no medium https://medium.com/@leoo.farias/crud-com-async-await-no-node-js-4e1032da5a33
exports.get = async(req, res, next) => {
try {
const data = await repository.get();
res.status(200).send({data: data});
} catch (e) {
res.status(500).send({
message: 'Falha ao processar sua requisição'
});
}
}
exports.getById = async(req, res, next) => {
try {
const data = await repository.getById(req.params.id);
res.status(200).send({data: data});
} catch (e) {
res.status(500).send({
message: 'Falha ao processar sua requisição'
});
}
}
exports.post = async(req, res, next) => {
try {
await repository.create({
name: req.body.name,
email: req.body.email,
password: req.body.password
});
res.status(201).send({
message: 'Cadastro realizado com sucesso!'
});
} catch (e) {
res.status(400).send({
message: 'Falha ao realizar cadastro'
});
}
}
exports.put = async(req, res, next) => {
try {
await repository.update(req.params.id, req.body);
res.status(200).send({
message: 'Atualização realizada com sucesso!'
});
} catch (e) {
res.status(400).send({
message: 'Falha ao editar'
});
}
}
exports.delete = async(req, res, next) => {
try {
await repository.delete(req.params.id)
res.status(200).send({
message: 'Atualização realizada com sucesso!'
})
} catch (e) {
res.status(400).send({
message: 'Falha ao deletar'
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment