Skip to content

Commit

Permalink
Merge pull request #50 from nicoSix/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
nicoSix committed May 21, 2021
2 parents 965f904 + 8afd39a commit 9266d6f
Show file tree
Hide file tree
Showing 25 changed files with 8,863 additions and 6,547 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.eslintcache
uploads
api/certs
api/certs
.DS_Store
2 changes: 1 addition & 1 deletion api/data/architectures.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
try {
const components = await client.query("SELECT * FROM components_instances WHERE architecture_id = $1", [architectureId]);
const architecture = await client.query("SELECT * FROM architectures WHERE id = $1", [architectureId]);

return {
success: true,
result: {
Expand Down
23 changes: 7 additions & 16 deletions api/data/connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,13 @@ module.exports = {
storeConnection: async connection => {
const newConnectionId = uuidv4();
try {
const foundConnections = await client.query("SELECT * FROM connections WHERE (first_component = $1 AND second_component = $2) OR (first_component = $2 AND second_component = $1)", [connection.first_component, connection.second_component]);
if(foundConnections["rows"].length === 0) {
await client.query(`
INSERT INTO connections (id, first_component, second_component, datatype, direction, name)
VALUES ($1, $2, $3, $4, $5, $6)
`, [newConnectionId, connection.first_component, connection.second_component, connection.datatype, connection.direction, connection.name])
return {
success: true,
connectionId: newConnectionId
}
}
else {
return {
success: false,
errorMsg: 'Connection already exists for this component.'
};
await client.query(`
INSERT INTO connections (id, first_component, second_component, datatype, direction, name)
VALUES ($1, $2, $3, $4, $5, $6)
`, [newConnectionId, connection.first_component, connection.second_component, connection.datatype, connection.direction, connection.name])
return {
success: true,
connectionId: newConnectionId
}
}
catch(err) {
Expand Down
45 changes: 45 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"body-parser": "^1.19.0",
"express": "^4.17.1",
"express-fileupload": "^1.2.0",
"graphlib-dot": "^0.6.4",
"multer": "^1.4.2",
"njwt": "^1.0.0",
"pg": "^8.5.1",
Expand Down
68 changes: 61 additions & 7 deletions api/routes/architectures.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,81 @@
const express = require('express'), router = express.Router();
const db = require('../data/architectures');
var dot = require('graphlib-dot');
var Graph = require("graphlib").Graph;
const archDB = require('../data/architectures');
const compDB = require('../data/components');
const { authorizedOnly } = require('../utils/authorization');
const { parseDBResults } = require('../utils/helpers');

function resolveComponentName(id, components) {
for (let i = 0; i < components.length; i++) {
if (components[i].id === id) return `${components[i].name} (#${components[i].id.substring(0, 7)})`;
}
return `Unknown name (${components[i].id.substring(0, 7)})`;
};

router
.get('/', authorizedOnly, (req, res) => {
db.getArchitectures().then((queryResult) => {
archDB.getArchitectures().then((queryResult) => {
const parsedResult = parseDBResults(queryResult);
if(parsedResult.success) res.status(200).send(parsedResult);
else res.status(500).send(parsedResult);
})
})
.get('/:id', authorizedOnly, (req, res) => {
var id = req.params.id;
db.getArchitecture(id).then((parsedResult) => {
archDB.getArchitecture(id).then((parsedResult) => {
if(parsedResult.success) res.status(200).send(parsedResult);
else res.status(500).send(parsedResult);
})
})
.get('/:id/graph', authorizedOnly, async (req, res) => {
let digraph = new Graph();
let id = req.params.id;
let archConns = {};
if(id) {
const resArch = await archDB.getArchitecture(id);
if (!resArch.result) return res.status(500).send({ success: false, errorMsg: "Server error." });
for (let i = 0; i < resArch.result.components.length; i++) {
let c = resArch.result.components[i];
digraph.setNode(`${c.name} (#${c.id.substring(0, 7)})`);
let fullComp = await compDB.getComponentInstance(c.id);
if (!fullComp.result) return res.status(500).send({ success: false, errorMsg: "Server error." });
fullComp.result.connections.forEach(c => { archConns[c.id] = c; });
}

Object.keys(archConns).forEach(key => {
if (archConns[key].direction === "bidirectional" || archConns[key].direction === "first-to-second") {
digraph.setEdge(
resolveComponentName(archConns[key].first_component, resArch.result.components),
resolveComponentName(archConns[key].second_component, resArch.result.components),
{ label: (archConns[key].datatype ? archConns[key].datatype : "Any") }
);
}
if (archConns[key].direction === "bidirectional" || archConns[key].direction === "second-to-first") {
digraph.setEdge(
resolveComponentName(archConns[key].second_component, resArch.result.components),
resolveComponentName(archConns[key].first_component, resArch.result.components),
{ label: (archConns[key].datatype ? archConns[key].datatype : "Any") }
);
}
});
res.status(200).send({
success: true,
result: dot.write(digraph)
});

}
else {
res.status(500).send({
success: false,
errorMsg: "Missing fields."
})
}
})
.post('/', authorizedOnly, (req, res) => {
const newArchitecture = req.body;
if(newArchitecture.name && newArchitecture.paper_id) {
db.storeArchitecture(newArchitecture).then((parsedResult) => {
archDB.storeArchitecture(newArchitecture).then((parsedResult) => {
if(parsedResult.success) res.status(200).send(parsedResult);
else res.status(500).send(parsedResult);
})
Expand All @@ -38,7 +92,7 @@ router
const paperId = req.body.paperId;

if(architectureId && paperId) {
db.cloneArchitecture(architectureId, paperId).then((parsedResult) => {
archDB.cloneArchitecture(architectureId, paperId).then((parsedResult) => {
if(parsedResult.success) res.status(200).send(parsedResult);
else res.status(500).send(parsedResult);
})
Expand All @@ -51,15 +105,15 @@ router
}
})
.delete('/:id', authorizedOnly, (req, res) => {
db.deleteArchitecture(req.params.id).then((parsedResult) => {
archDB.deleteArchitecture(req.params.id).then((parsedResult) => {
if(parsedResult.success) res.status(200).send(parsedResult);
else res.status(500).send(parsedResult);
})
})
.put('/:id', authorizedOnly, (req, res) => {
const newArchitecture = req.body;
if(newArchitecture.name && newArchitecture.id) {
db.modifyArchitecture(newArchitecture).then((parsedResult) => {
archDB.modifyArchitecture(newArchitecture).then((parsedResult) => {
if(parsedResult.success) res.status(200).send(parsedResult);
else res.status(500).send(parsedResult);
})
Expand Down
Loading

0 comments on commit 9266d6f

Please sign in to comment.