Não tou conseguindo resolver... Help!
src/config/express.js
const express = require('express');
const consign = require('consign');
const bodyParser = require('body-parser');
module.exports = () => {
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
consign()
.include('src/controllers')
.then('src/database')
.into(app);
return app;
}
src/database/connectionFactory.js
const mysql = require('mysql');
function createDBConnection() {
return mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "db_soupublicador"
});
}
module.exports = function(){
return createDBConnection;
}
src/database/PublisherDAO.js
function PublisherDAO(connection) {
this._connection = connection;
}
PublisherDAO.prototype.save = (publisher, callback) => {
this._connection.query('INSERT INTO publishers SET ?', publisher, callback);
}
PublisherDAO.prototype.listAll = (callback) => {
this._connection.query('select * from publishers', callback);
}
PublisherDAO.prototype.findByID = (id, callback) => {
this._connection.query("select * from publishers where id = ?",[id], callback);
}
module.exports = () => {
return PublisherDAO;
};
src/controllers/publisher.js
module.exports = (app) => {
app.get("/publisher", (req, resp) => {
let connection = app.database.connectionFactory();
let PublisherDAO = new app.PublisherDAO(connection);
PublisherDAO.listAll((errors, results) => {
console.log(results);
});
});
}