Last active
October 22, 2019 14:31
-
-
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
This file contains hidden or 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
| 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