Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotrajano committed Jun 30, 2020
1 parent 5ce4113 commit 03d7fb8
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 149 deletions.
53 changes: 4 additions & 49 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const project = require(`./routes/project`);
const feature = require(`./routes/feature`);
const admin = require(`./routes/admin`);
const bug = require(`./routes/bug`);
const error = require(`./routes/error`);
const api = require(`./routes/api`);
const session = require("express-session");
const flash = require("connect-flash");
const StatusProject = require(`./models/StatusProject`);
const Project = require(`./models/Project`);
const FeatureStatus = require(`./models/FeatureStatus`);
const StatusProgress = require(`./models/StatusProgress`);
Expand Down Expand Up @@ -58,54 +59,8 @@ app.use(`/project`, project);
app.use(`/feature`, feature);
app.use(`/admin`, admin);
app.use(`/bug`, bug);

app.get("/", function (req, res) {
res.render(`index`);
});

app.get("/css", function (req, res) {
res.render(`teste_css`);
});

app.get(`/api`, (req, res) => {
StatusProject.findAll(async function (results) {
res.send(results);
});
});

app.get(`/api/getActiveProjects`, (req, res) => {
Project.findAll(async function (results) {
res.send(results);
});
});

app.get(`/api/getFeatureStatus`, (req, res) => {
FeatureStatus.findAll(async function (results) {
res.send(results);
});
});

app.get(`/api/getStatusProgress`, (req, res) => {
StatusProgress.findAll(async function (results) {
res.send(results);
});
});

app.get(`/api/getStatus`, (req, res) => {
StatusProgress.getStatus()
.then(function (results) {
res.send(results);
})
.catch(function (err) {
req.flash("error_msg", "ERROR" + err);
res.redirect(`/project`);
});
});

app.get(`/api/getStatus2`, async (req, res) => {
let result = await StatusProgress.getStatus();
res.send(result);
});
app.use(`/error`, error);
app.use(`/api`, api);

app.listen(SERVER_PORT, () => {
console.log("Server listening on port " + SERVER_PORT);
Expand Down
14 changes: 7 additions & 7 deletions models/Bug.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const mysql = require("mysql");
const connection = require("../db/connection");

const create = async function (bug, callback) {
Expand Down Expand Up @@ -47,13 +48,12 @@ const findAll = async function (callback) {
});
};

const getBugsByProject = async function (id, callback) {
let sql = `select * from bugs where projectid=${id}`;

connection.query(sql, function (err, results) {
if (err) throw err;
console.log(sql);
return callback(results);
const getBugsByProject = (id) => {
return new Promise((resolve, reject) => {
let sql = `select * from bugs where projectid=${mysql.escape(id)}`;
connection.query(sql, (err, results) => {
err ? reject(err) : resolve(results);
});
});
};

Expand Down
13 changes: 6 additions & 7 deletions models/FeatureStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ const STATUS = {
CLOSED: 4,
};

const findAll = async function (callback) {
let sql = `SELECT * FROM FeatureStatus`;

connection.query(sql, async function (err, result) {
if (err) throw err;
console.log(sql);
return callback(result);
const findAll = () => {
return new Promise((resolve, reject) => {
let sql = `SELECT * FROM FeatureStatus`;
connection.query(sql, (err, result) => {
err ? reject(err) : resolve(result);
});
});
};

Expand Down
13 changes: 6 additions & 7 deletions models/Priority.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const connection = require("../db/connection");

const findAll = async function (callback) {
let sql = `SELECT * FROM Priority`;

connection.query(sql, async function (err, result) {
if (err) throw err;
console.log(sql);
return callback(result);
const findAll = () => {
return new Promise((resolve, reject) => {
let sql = `SELECT * FROM Priority`;
connection.query(sql, (err, result) => {
err ? reject(err) : resolve(result);
});
});
};

Expand Down
13 changes: 7 additions & 6 deletions models/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ const update = async function (obj, callback) {
});
};

const findAll = async function (callback) {
let sql = `select p.ID as ID, p.NameProject, DATE_FORMAT(p.StartDate,'%Y-%m-%d') as StartDate , DATE_FORMAT(p.EndDate,'%Y-%m-%d') as EndDate, p.DescriptionProject, sp.ID as StatusID, sp.StatusName
const findAll = () => {
return new Promise((resolve, reject) => {
let sql = `select p.ID as ID, p.NameProject, DATE_FORMAT(p.StartDate,'%Y-%m-%d') as StartDate , DATE_FORMAT(p.EndDate,'%Y-%m-%d') as EndDate, p.DescriptionProject, sp.ID as StatusID, sp.StatusName
from projects as p
inner join statusproject as sp
on p.StatusID = sp.ID
ORDER BY ID DESC`;
connection.query(sql, async function (err, result) {
if (err) throw err;
console.log(sql);
return callback(result);

connection.query(sql, (err, result) => {
err ? reject(err) : resolve(result);
});
});
};

Expand Down
13 changes: 6 additions & 7 deletions models/Severity.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const connection = require("../db/connection");

const findAll = async function (callback) {
let sql = `SELECT * FROM Severity`;

connection.query(sql, async function (err, result) {
if (err) throw err;
console.log(sql);
return callback(result);
const findAll = () => {
return new Promise((resolve, reject) => {
let sql = `SELECT * FROM Severity`;
connection.query(sql, (err, result) => {
err ? reject(err) : resolve(result);
});
});
};

Expand Down
24 changes: 4 additions & 20 deletions models/StatusProgress.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,16 @@ const StatusProgressValue = {
DONE: 10,
};

const findAll = async function (callback) {
let sql = `SELECT * FROM StatusProgress`;

connection.query(sql, async function (err, result) {
if (err) throw err;
console.log(sql);
return callback(result);
});
};

async function getStatus() {
const findAll = () => {
return new Promise((resolve, reject) => {
let sql = `SELECT * FROM StatusProgress`;
connection.cachedPool().query(sql, (err, results) => {
//if (err) throw err;
if (err) {
reject(err);
} else {
resolve(results);
}
connection.query(sql, (err, results) => {
err ? reject(err) : resolve(results);
});
});
}
};

module.exports = {
StatusProgressValue: StatusProgressValue,
findAll: findAll,
getStatus: getStatus,
};
23 changes: 6 additions & 17 deletions models/StatusProject.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
const mysql = require("mysql");
const connection = require("../db/connection");

const create = async function (obj, callback) {
let sql = `INSERT INTO StatusProject (${obj.field}) VALUES ('${obj.value}')`;
connection.query(sql, function (err, result) {
if (err) throw err;
console.log(sql);
findByPK(result.insertId, function (result) {
return callback(result);
const findAll = () => {
return new Promise((resolve, reject) => {
let sql = `SELECT * FROM statusproject`;
connection.query(sql, (err, result) => {
err ? reject(err) : resolve(result);
});
});
};

const findAll = async function (callback) {
let sql = `SELECT * FROM statusproject`;
connection.query(sql, async function (err, result) {
if (err) throw err;
console.log(sql);
return callback(result);
});
};

const findByPK = async function (id, callback) {
let sql = `SELECT * FROM statusproject
where id=${mysql.escape(id)}`;
Expand All @@ -32,4 +21,4 @@ const findByPK = async function (id, callback) {
});
};

module.exports = { create: create, findByPK: findByPK, findAll: findAll };
module.exports = { findByPK: findByPK, findAll: findAll };
81 changes: 81 additions & 0 deletions routes/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const express = require(`express`);
const router = express.Router();
const StatusProject = require(`../models/StatusProject`);
const Project = require(`../models/Project`);
const FeatureStatus = require(`../models/FeatureStatus`);
const StatusProgress = require(`../models/StatusProgress`);
const Severity = require(`../models/Severity`);
const Priority = require(`../models/Priority`);
const Bug = require(`../models/Bug`);

router.get(`/getStatusProject`, (req, res) => {
StatusProject.findAll()
.then((results) => {
res.send(results);
})
.catch((err) => {
req.flash("error_msg", "ERROR" + err);
});
});

router.get(`/getActiveProjects`, (req, res) => {
Project.findAll()
.then((result) => {
res.send(result);
})
.catch((err) => {
req.flash("error_msg", "ERROR" + err);
});
});

router.get(`/getFeatureStatus`, (req, res) => {
FeatureStatus.findAll()
.then((Status) => {
res.send(Status);
})
.catch((err) => {
req.flash("error_msg", "ERROR" + err);
});
});

router.get(`/getStatusProgress`, (req, res) => {
StatusProgress.findAll()
.then((results) => {
res.send(results);
})
.catch((err) => {
req.flash("error_msg", "ERROR" + err);
});
});

router.get(`/getSeverity`, (req, res) => {
Severity.findAll()
.then((results) => {
res.send(results);
})
.catch((err) => {
req.flash("error_msg", "ERROR" + err);
});
});

router.get(`/getPriority`, (req, res) => {
Priority.findAll()
.then((results) => {
res.send(results);
})
.catch((err) => {
req.flash("error_msg", "ERROR" + err);
});
});

router.get(`/getBugsByProject/:id`, (req, res) => {
Bug.getBugsByProject(req.params.id)
.then((results) => {
res.send(results);
})
.catch((err) => {
req.flash("error_msg", "ERROR" + err);
});
});

module.exports = router;
20 changes: 0 additions & 20 deletions routes/bug.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const express = require(`express`);
const router = express.Router();
const Bug = require(`../models/Bug`);
const Severity = require(`../models/Severity`);
const Priority = require(`../models/Priority`);
const Project = require(`../models/Project`);
const moment = require("moment");

Expand Down Expand Up @@ -53,22 +51,4 @@ router.post(`/solveIssue`, (req, res) => {
});
});

router.get(`/getSeverity`, (req, res) => {
Severity.findAll((results) => {
res.send(results);
});
});

router.get(`/getPriority`, (req, res) => {
Priority.findAll((results) => {
res.send(results);
});
});

router.get(`/getBugsByProject/:id`, (req, res) => {
Bug.getBugsByProject(req.params.id, (results) => {
res.send(results);
});
});

module.exports = router;
8 changes: 8 additions & 0 deletions routes/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require(`express`);
const router = express.Router();

router.get(`/`, (req, res) => {
res.render(`error/error`);
});

module.exports = router;
10 changes: 7 additions & 3 deletions routes/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ const router = express.Router();
const Project = require(`../models/Project`);

router.get(`/`, (req, res) => {
Project.findAll(function (results) {
res.render("project/project", { Projects: results });
});
Project.findAll()
.then((result) => {
res.render("project/project", { Projects: result });
})
.catch((err) => {
req.flash("error_msg", "ERROR" + err);
});
});

router.get(`/new`, (req, res) => {
Expand Down
Loading

0 comments on commit 03d7fb8

Please sign in to comment.