Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const UsuarioController = require('./../controller/usuario.controller');
- module.exports = class Routes {
- constructor(server) {
- this.userCtrl = new UsuarioController();
- this.router = express.Router();
- server.use('/api', this.router);
- this.registrationRoutes();
- }
- registrationRoutes() {
- //apenas para teste
- this.router.route('/teste').get((req, res, next) => {
- res.json(this.userCtrl.getUsers());
- });
- }
- }
- const UsuarioBd = require('./../model/usuario.service');
- module.exports = class UsuarioController {
- constructor(){
- this.user = new UsuarioBd();
- }
- getUsers() {
- return this.user.getUsers();
- }
- }
- const Dbsettings = require('./../settings/dbsettings');
- const Usuario = require('./usuario');
- module.exports = class UsuarioBd {
- constructor() {
- this.db = new Dbsettings();
- }
- getUsers() {
- let usuarios = [];
- let sql = "SELECT id, nome, email FROM usuario";
- this.db.abrir_conexao().query(sql, (err, result) => {
- if(err) console.log(err);
- result.forEach((value) => {
- usuarios.push(new Usuario(value));
- });
- });
- console.log(usuarios);
- return usuarios;
- }
- }
- const mysql = require('mysql');
- module.exports = class Dbsettings {
- constructor() {
- this.db_connection = mysql.createConnection({
- host: 'localhost',
- user: 'root',
- password: '',
- database: 'db_teste'
- });
- }
- abrir_conexao() {
- this.db_connection.connect((err) => {
- if(err)
- console.log(err);
- });
- return this.db_connection;
- }
- fechar_conexao() {
- this.db_connection.end();
- }
- }
Add Comment
Please, Sign In to add comment