Added benchmark and hardware models; added routes and views for hardware

This commit is contained in:
Gregory Ballantine 2023-11-26 12:54:09 -05:00
parent e5602e660d
commit 2f016d3062
10 changed files with 181 additions and 2 deletions

View File

@ -35,14 +35,22 @@ app.use(express.static('public'));
// load routes // load routes
const indexRoutes = require('./src/routes/index'); const indexRoutes = require('./src/routes/index');
const projectRoutes = require('./src/routes/project'); const projectRoutes = require('./src/routes/project');
const hardwareRoutes = require('./src/routes/hardware');
// register routes // register routes
app.get('/', indexRoutes.getIndex); app.get('/', indexRoutes.getIndex);
// project routes
app.get('/project', projectRoutes.getIndex); app.get('/project', projectRoutes.getIndex);
app.get('/project/list', projectRoutes.getList); app.get('/project/list', projectRoutes.getList);
app.get('/project/:project_id', projectRoutes.getView);
app.get('/project/add', projectRoutes.getAdd); app.get('/project/add', projectRoutes.getAdd);
app.post('/project/add', projectRoutes.postAdd); 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);
app.get('/hardware/add', hardwareRoutes.getAdd);
app.post('/hardware/add', hardwareRoutes.postAdd);
app.get('/hardware/:hardware_id', hardwareRoutes.getView);
app.listen(port, () => { app.listen(port, () => {
console.log(`Leviathan listening on port ${port}`); console.log(`Leviathan listening on port ${port}`);

View File

@ -7,6 +7,9 @@ body{
} }
body{ body{
height: auto;
min-height: 100%;
box-sizing: border-box;
padding-top: 80px; padding-top: 80px;
padding-bottom: 80px; padding-bottom: 80px;
background: #eee; background: #eee;
@ -30,6 +33,7 @@ textarea{
height: 64px; height: 64px;
background: teal; background: teal;
color: white; color: white;
z-index: 20;
} }
#main-nav ul{ #main-nav ul{
@ -61,6 +65,7 @@ textarea{
padding: 14px 20px; padding: 14px 20px;
background: white; background: white;
border-radius: 8px; border-radius: 8px;
z-index: 10;
} }
#main-footer{ #main-footer{
@ -69,6 +74,7 @@ textarea{
left: 0; left: 0;
width: 100%; width: 100%;
height: 64px; height: 64px;
} }
#main-footer p{ #main-footer p{

21
src/models/benchmark.js Normal file
View File

@ -0,0 +1,21 @@
const { Sequelize } = require("sequelize");
module.exports = (sequelize) => {
const Benchmark = sequelize.define('Benchmark', {
name: {
type: Sequelize.STRING,
null: false,
},
description: {
type: Sequelize.TEXT,
},
scoring: {
type: Sequelize.STRING,
null: false,
},
},
{
tableName: 'benchmarks'
});
return Benchmark;
};

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

@ -0,0 +1,18 @@
const { Sequelize } = require("sequelize");
module.exports = (sequelize) => {
const Hardware = sequelize.define('Hardware', {
name: {
type: Sequelize.STRING,
null: false,
},
type: {
type: Sequelize.STRING,
null: false,
},
},
{
tableName: 'hardware'
});
return Hardware;
};

View File

@ -6,5 +6,7 @@ const sequelize = new Sequelize({
}); });
const Project = require('./project')(sequelize); const Project = require('./project')(sequelize);
const Hardware = require('./hardware')(sequelize);
const Benchmark = require('./benchmark')(sequelize);
module.exports = sequelize; module.exports = sequelize;

View File

@ -10,6 +10,8 @@ module.exports = (sequelize) => {
type: Sequelize.TEXT, type: Sequelize.TEXT,
}, },
}, },
{}); {
tableName: 'projects'
});
return Project; return Project;
}; };

41
src/routes/hardware.js Normal file
View File

@ -0,0 +1,41 @@
const Hardware = require('../models').models.Hardware;
// GET /hardware - redirects to project list
exports.getIndex = async function(req, res) {
res.redirect('/hardware/list');
};
// GET /hardware/list - list of hardware
exports.getList = async function(req, res) {
var hardware = await Hardware.findAll();
res.render('hardware/list', {
hardware: hardware
});
};
// GET /hardware/:hardware_id - view information about a piece of hardware
exports.getView = async function(req, res) {
var hardware = await Hardware.findAll({
where: {
id: req.params.hardware_id
}
});
res.render('hardware/view', {
hardware: hardware[0]
});
};
// GET /hardware/add - add a new hardware
exports.getAdd = async function(req, res) {
res.render('hardware/add');
};
// POST /hardware/add - add the hardware to the database
exports.postAdd = async function(req, res) {
var hardware = await Hardware.create({
name: req.body.hardware_name,
type: req.body.hardware_type
});
res.redirect('/hardware');
};

35
views/hardware/add.twig Normal file
View File

@ -0,0 +1,35 @@
{% extends 'layouts/default.twig' %}
{% block title %}Add Hardware{% endblock %}
{% block content %}
<div class="row">
<h2>Add hardware</h2>
<form class="twelve columns" action="/hardware/add" method="POST">
<div class="row">
<div class="nine columns">
<label for="hardware_name">
Hardware name:
<input id="hardware_name" class="u-full-width" type="text" name="hardware_name" placeholder="EVGA RTX 3080 Ti">
</label>
</div>
<div class="three columns">
<label for="hardware_type">
Hardware type:
<select id="hardware_type" class="u-full-width" name="hardware_type">
<option value="cpu">Processor</option>
<option value="mem">Memory</option>
<option value="gpu">Graphics card</option>
<option value="ssd">SSD</option>
<option value="hdd">HDD</option>
</select>
</label>
</div>
</div>
<input class="button-primary u-full-width" type="submit" value="Submit">
</form>
</div>
{% endblock %}

31
views/hardware/list.twig Normal file
View File

@ -0,0 +1,31 @@
{% extends 'layouts/default.twig' %}
{% block title %}List of Hardware{% endblock %}
{% block content %}
<div class="row">
<h2>Hardware</h2>
<a href="/hardware/add">Add new hardware</a>
<table class="twelve columns">
<thead>
<tr>
<td>Name</td>
<td>Type</td>
<td>Created at</td>
<td>Last updated</td>
</tr>
</thead>
<tbody>
{% for h in hardware %}
<tr>
<td><a href="/hardware/{{ h.id }}">{{ h.name }}</a></td>
<td>{{ h.type }}</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 %}

15
views/hardware/view.twig Normal file
View File

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