Guest User

Untitled

a guest
Jan 19th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. const express = require('express');
  2. const UsuarioController = require('./../controller/usuario.controller');
  3.  
  4. module.exports = class Routes {
  5.  
  6. constructor(server) {
  7. this.userCtrl = new UsuarioController();
  8. this.router = express.Router();
  9. server.use('/api', this.router);
  10. this.registrationRoutes();
  11. }
  12.  
  13. registrationRoutes() {
  14. //apenas para teste
  15. this.router.route('/teste').get((req, res, next) => {
  16. res.json(this.userCtrl.getUsers());
  17. });
  18. }
  19. }
  20.  
  21. const UsuarioBd = require('./../model/usuario.service');
  22.  
  23. module.exports = class UsuarioController {
  24. constructor(){
  25. this.user = new UsuarioBd();
  26. }
  27.  
  28. getUsers() {
  29. return this.user.getUsers();
  30. }
  31. }
  32.  
  33. const Dbsettings = require('./../settings/dbsettings');
  34. const Usuario = require('./usuario');
  35.  
  36. module.exports = class UsuarioBd {
  37.  
  38. constructor() {
  39. this.db = new Dbsettings();
  40. }
  41.  
  42. getUsers() {
  43. let usuarios = [];
  44. let sql = "SELECT id, nome, email FROM usuario";
  45.  
  46. this.db.abrir_conexao().query(sql, (err, result) => {
  47. if(err) console.log(err);
  48.  
  49. result.forEach((value) => {
  50. usuarios.push(new Usuario(value));
  51. });
  52. });
  53. console.log(usuarios);
  54. return usuarios;
  55. }
  56.  
  57.  
  58. }
  59.  
  60. const mysql = require('mysql');
  61.  
  62. module.exports = class Dbsettings {
  63.  
  64. constructor() {
  65. this.db_connection = mysql.createConnection({
  66. host: 'localhost',
  67. user: 'root',
  68. password: '',
  69. database: 'db_teste'
  70. });
  71. }
  72.  
  73. abrir_conexao() {
  74. this.db_connection.connect((err) => {
  75. if(err)
  76. console.log(err);
  77. });
  78.  
  79. return this.db_connection;
  80. }
  81.  
  82. fechar_conexao() {
  83. this.db_connection.end();
  84. }
  85.  
  86.  
  87. }
Add Comment
Please, Sign In to add comment