Removed projects; added tests

This commit is contained in:
Gregory Ballantine 2023-12-02 23:41:04 -05:00
parent 286219c5ea
commit 75552eccee
16 changed files with 209 additions and 146 deletions

View File

@ -28,6 +28,10 @@ app.use(bodyParser.urlencoded({ extended: true }));
// enable the Twig template engine
app.set('view engine', 'twig');
app.set('twig options', {
allowAsync: true,
strict_variables: false
});
// enable serving static files
app.use(express.static('public'));

View File

@ -5,8 +5,16 @@ const sequelize = new Sequelize({
storage: 'data/leviathan.db'
});
const Project = require('./project')(sequelize);
const Hardware = require('./hardware')(sequelize);
const Benchmark = require('./benchmark')(sequelize);
const Test = require('./test')(sequelize);
// Hardware/Test one-to-many
Hardware.hasMany(Test);
Test.belongsTo(Hardware);
// Benchmark/Test many-to-many
Benchmark.belongsToMany(Test, { through: 'tests_benchmarks' });
Test.belongsToMany(Benchmark, { through: 'tests_benchmarks' });
module.exports = sequelize;

View File

@ -1,17 +0,0 @@
const { Sequelize } = require("sequelize");
module.exports = (sequelize) => {
const Project = sequelize.define('Project', {
title: {
type: Sequelize.STRING,
null: false
},
description: {
type: Sequelize.TEXT,
},
},
{
tableName: 'projects'
});
return Project;
};

18
src/models/test.js Normal file
View File

@ -0,0 +1,18 @@
const { Sequelize } = require("sequelize");
module.exports = (sequelize) => {
const Test = sequelize.define('Test', {
dateTag: {
type: Sequelize.STRING,
null: false,
},
description: {
type: Sequelize.STRING,
null: true,
}
},
{
tableName: 'tests'
});
return Test;
};

View File

@ -1,6 +1,6 @@
// load routes
const topRoutes = require('./toplevel');
const projectRoutes = require('./project');
const testRoutes = require('./test');
const hardwareRoutes = require('./hardware');
const benchmarkRoutes = require('./benchmark');
@ -9,13 +9,6 @@ module.exports = function(app) {
// top-level routes
app.get('/', topRoutes.getIndex);
// project routes
app.get('/project', projectRoutes.getIndex);
app.get('/project/list', projectRoutes.getList);
app.get('/project/add', projectRoutes.getAdd);
app.post('/project/add', projectRoutes.postAdd);
app.get('/project/:project_id', projectRoutes.getView);
// hardware routes
app.get('/hardware', hardwareRoutes.getIndex);
app.get('/hardware/list', hardwareRoutes.getList);
@ -30,4 +23,11 @@ module.exports = function(app) {
app.post('/benchmark/add', benchmarkRoutes.postAdd);
app.get('/benchmark/:benchmark_id', benchmarkRoutes.getView);
// test routes
app.get('/test', testRoutes.getIndex);
app.get('/test/list', testRoutes.getList);
app.get('/test/add', testRoutes.getAdd);
app.post('/test/add', testRoutes.postAdd);
app.get('/test/:test_id', testRoutes.getView);
};

View File

@ -1,41 +0,0 @@
const Project = require('../models').models.Project;
// GET /project - redirects to project list
exports.getIndex = async function(req, res) {
res.redirect('/project/list');
};
// GET /project/list - list of projects
exports.getList = async function(req, res) {
var projects = await Project.findAll();
res.render('project/list', {
projects: projects
});
};
// GET /project/:project_id - view information about a project
exports.getView = async function(req, res) {
var project = await Project.findAll({
where: {
id: req.params.project_id
}
});
res.render('project/view', {
project: project[0]
});
};
// GET /project/add - add a new project
exports.getAdd = async function(req, res) {
res.render('project/add');
};
// POST /project/add - add the project to the database
exports.postAdd = async function(req, res) {
var project = await Project.create({
title: req.body.project_title,
description: req.body.project_description
});
res.redirect('/project');
};

59
src/routes/test.js Normal file
View File

@ -0,0 +1,59 @@
const Test = require('../models').models.Test;
const Hardware = require('../models').models.Hardware;
const Benchmark = require('../models').models.Benchmark;
// GET /test - redirects to test list
exports.getIndex = async function(req, res) {
res.redirect('/test/list');
};
// GET /test/list - list of tests
exports.getList = async function(req, res) {
var tests = await Test.findAll();
res.render('test/list', {
tests: tests
});
};
// GET /test/:test_id - view information about a test
exports.getView = async function(req, res) {
var test = await Test.findAll({
where: {
id: req.params.test_id
}
});
res.render('test/view', {
test: test[0]
});
};
// GET /test/add - add a new test
exports.getAdd = async function(req, res) {
var hardware = await Hardware.findAll();
var benchmarks = await Benchmark.findAll();
res.render('test/add', {
hardware: hardware,
benchmarks: benchmarks
});
};
// POST /test/add - add the test to the database
exports.postAdd = async function(req, res) {
var test = await Test.create({
dateTag: req.body.test_date_tag,
description: req.body.test_description,
});
// add link to hardware
let hardware = await Hardware.findByPk(req.body.test_hardware);
test.setHardware(hardware);
// add links to benchmarks
for (let b = 0; b < req.body.test_benchmarks.length; b++) {
let benchmark = await Benchmark.findByPk(req.body.test_benchmarks[b]);
test.addBenchmark(benchmark);
}
res.redirect('/test/' + test.id);
};

View File

@ -1,9 +1,9 @@
const Project = require('../models').models.Project;
const Test = require('../models').models.Test;
// GET / - primary app dashboard
exports.getIndex = async function(req, res) {
var projects = await Project.findAll();
var tests = await Test.findAll();
res.render('index/dashboard', {
projects: projects
tests: tests
});
};

View File

@ -4,7 +4,7 @@
{% block content %}
<div class="row">
<h2>Recently updated projects:</h2>
<h2>Recently updated tests:</h2>
<table class="twelve columns">
<thead>
<tr>
@ -13,9 +13,9 @@
</tr>
</thead>
<tbody>
{% for p in projects %}
{% for p in tests %}
<tr>
<td><a href="/project/{{ p.id }}">{{ p.title }}</a></td>
<td><a href="/test/{{ p.id }}">{{ p.date_tag }}</a></td>
<td>{{ p.updatedAt | date('m/d/Y g:ia') }}</td>
</tr>
{% endfor %}

View File

@ -3,9 +3,9 @@
<h4>Leviathan</h4>
<ul>
<li><a href="/">Dashboard</a></li>
<li><a href="/project">Projects</a></li>
<li><a href="/test">Tests</a></li>
<li><a href="/hardware">Hardware</a></li>
<li><a href="/benchmark">Benchmarks</a></li>
</ul>
</div>
</nav>
</nav>

View File

@ -1,27 +0,0 @@
{% extends 'layouts/default.twig' %}
{% block title %}Add a Project{% endblock %}
{% block content %}
<div class="row">
<h2>Add a project</h2>
<form class="twelve columns" action="/project/add" method="POST">
<div class="row">
<label for="project_title">
Project title:
<input id="project_title" class="u-full-width" type="text" name="project_title" placeholder="My hardware benchmarking project">
</label>
</div>
<div class="row">
<label for="project_description">
Project description:
<textarea id="project_description" class="twelve columns" cols="30" rows="10" name="project_description"></textarea>
</label>
</div>
<input class="button-primary u-full-width" type="submit" value="Submit">
</form>
</div>
{% endblock %}

View File

@ -1,29 +0,0 @@
{% extends 'layouts/default.twig' %}
{% block title %}List of Projects{% endblock %}
{% block content %}
<div class="row">
<h2>Projects</h2>
<a href="/project/add">Add a project</a>
<table class="twelve columns">
<thead>
<tr>
<td>Title</td>
<td>Created at</td>
<td>Last updated</td>
</tr>
</thead>
<tbody>
{% for p in projects %}
<tr>
<td><a href="/project/{{ p.id }}">{{ p.title }}</a></td>
<td>{{ p.createdAt | date('m/d/Y g:ia') }}</td>
<td>{{ p.updatedAt | date('m/d/Y g:ia') }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}

View File

@ -1,15 +0,0 @@
{% extends 'layouts/default.twig' %}
{% block title %}{{ project.title }}{% endblock %}
{% block content %}
<div class="row">
<h2>{{ project.title }}</h2>
<p>{{ project.description }}</p>
<hr>
<p><a href="/project">Back</a></p>
</div>
{% endblock %}

53
views/test/add.twig Normal file
View File

@ -0,0 +1,53 @@
{% extends 'layouts/default.twig' %}
{% block title %}Add a Test{% endblock %}
{% block content %}
<div class="row">
<h2>Add a test</h2>
<form class="twelve columns" action="/test/add" method="POST">
<div class="row">
<div class="three columns">
<label for="test_date_tag">
Test date tag:
<input id="test_date_tag" class="u-full-width" type="text" name="test_date_tag" placeholder="(XX/YY)">
</label>
</div>
<div class="nine columns">
<label for="test_hardware">
Test hardware:
<select id="test_hardware" class="u-full-width" name="test_hardware">
{% for h in hardware %}
<option value="{{ h.id }}">{{ h.name }}</option>
{% endfor %}
</select>
</label>
</div>
</div>
<div class="row">
<div class="twelve columns">
<label for="test_benchmarks">
Test benchmarks:
<select id="test_benchmarks" class="u-full-width" name="test_benchmarks[]" multiple>
{% for b in benchmarks %}
<option value="{{ b.id }}">{{ b.name }}</option>
{% endfor %}
</select>
</label>
</div>
</div>
<div class="row">
<label for="test_description">
Test description:
<textarea id="test_description" class="twelve columns" cols="30" rows="10" name="test_description"></textarea>
</label>
</div>
<input class="button-primary u-full-width" type="submit" value="Submit">
</form>
</div>
{% endblock %}

29
views/test/list.twig Normal file
View File

@ -0,0 +1,29 @@
{% extends 'layouts/default.twig' %}
{% block title %}List of Tests{% endblock %}
{% block content %}
<div class="row">
<h2>Tests</h2>
<a href="/test/add">Add a test</a>
<table class="twelve columns">
<thead>
<tr>
<td>Date Tag</td>
<td>Created at</td>
<td>Last updated</td>
</tr>
</thead>
<tbody>
{% for t in tests %}
<tr>
<td><a href="/test/{{ t.id }}">{{ t.dateTag }} {{ t.getHardware().name }}</a></td>
<td>{{ t.createdAt | date('m/d/Y g:ia') }}</td>
<td>{{ t.updatedAt | date('m/d/Y g:ia') }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}

21
views/test/view.twig Normal file
View File

@ -0,0 +1,21 @@
{% extends 'layouts/default.twig' %}
{% block title %}Test: {{ test.getHardware().name }} - {{ test.dateTag }}{% endblock %}
{% block content %}
<div class="row">
<h2>Test: {{ test.getHardware().name }} - {{ test.dateTag }}</h2>
<div class="twelve columns">
<ul>
{% for b in test.getBenchmarks() %}
<li>{{ b.name }}</li>
{% endfor %}
</ul>
</div>
<hr>
<p><a href="/test">Back</a></p>
</div>
{% endblock %}