Started work on adding license tracking
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Gregory Ballantine 2022-11-06 01:37:56 -05:00
parent b98fbddd9e
commit 4167130f0c
8 changed files with 162 additions and 25 deletions

View File

@ -47,6 +47,7 @@ app.use(express.static('public'));
// load route handlers
const homeRoutes = require('./src/routes/home');
const itemRoutes = require('./src/routes/item');
const licenseRoutes = require('./src/routes/license');
// register route handlers
app.get('/', homeRoutes.getIndex);
@ -55,6 +56,8 @@ app.post('/item/add', itemRoutes.postAdd);
app.get('/item/:id', itemRoutes.getItem);
app.get('/item/:id/edit', itemRoutes.getItemEdit);
app.post('/item/:id/edit', itemRoutes.postItemEdit);
app.get('/license/add', licenseRoutes.getAdd);
app.post('/license/add', licenseRoutes.postAdd);
// start app
app.listen(config.get('server.port'), config.get('server.address'), () => {

View File

@ -9,6 +9,7 @@ db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.items = require('./item.js')(sequelize, Sequelize);
db.licenses = require('./license.js')(sequelize, Sequelize);
module.exports = db;

27
src/models/license.js Normal file
View File

@ -0,0 +1,27 @@
module.exports = (sequelize, Sequelize) => {
const License = sequelize.define('license', {
name: {
type: Sequelize.STRING,
},
key: {
type: Sequelize.STRING,
},
manufacturer: {
type: Sequelize.STRING,
},
purchasedFrom: {
type: Sequelize.STRING,
},
purchasedAt: {
type: Sequelize.DATE,
},
});
return License;
};

View File

@ -1,8 +1,10 @@
const db = require('../models');
const Item = db.items;
const License = db.licenses;
// GET - /
exports.getIndex = async function(req, res) {
// fetch inventory items from database
const items = await Item.findAll({
limit: 10,
order: [
@ -10,9 +12,18 @@ exports.getIndex = async function(req, res) {
],
});
// fetch licenses from database
const licenses = await License.findAll({
limit: 10,
order: [
['updatedAt', 'DESC'],
],
});
// req.flash('info', 'This is a test flash message.');
res.render('index.twig', {
inventory: items,
licenses: licenses,
});
};

22
src/routes/license.js Normal file
View File

@ -0,0 +1,22 @@
const db = require('../models');
const License = db.licenses;
// GET - /license/add
exports.getAdd = async function(req, res) {
res.render('license/add.twig');
};
// POST - /license/add
exports.postAdd = async function(req, res) {
const license = await License.create({
name: req.body.license_name,
key: req.body.license_key,
manufacturer: req.body.license_manufacturer,
purchasedFrom: req.body.license_purchase_from,
purchasedAt: req.body.license_purchase_date,
});
console.log(`Saved license ${license.name} to the database.`);
res.redirect('/');
};

View File

@ -28,32 +28,51 @@
<hr>
<section class="row">
<div class="columns twelve">
<h3>Recently updated records:</h3>
<div class="columns six">
<h3>Recently updated hardware:</h3>
<table class="u-full-width">
<thead>
<tr>
<th>Name</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Updated at</th>
</tr>
</thead>
<tbody>
{% for item in inventory %}
<tr>
<td><a href="/item/{{ item.id }}">{{ item.name }}</a></td>
<td>{{ item.manufacturer }}</td>
<td>{{ item.type }}</td>
<td>{{ item.updatedAt | date("m/d/Y h:i:s A") }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="columns six">
<h3>Recently updated licenses:</h3>
<table class="u-full-width">
<thead>
<tr>
<th>Name</th>
<th>Manufacturer</th>
<th>Updated at</th>
</tr>
</thead>
<tbody>
{% for license in licenses %}
<tr>
<td><a href="/license/{{ license.id }}">{{ license.name }}</a></td>
<td>{{ license.manufacturer }}</td>
<td>{{ license.updatedAt | date("m/d/Y h:i:s A") }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</section>
<section class="row">
<table class="columns twelve">
<thead>
<tr>
<th>Name</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Updated at</th>
</tr>
</thead>
<tbody>
{% for item in inventory %}
<tr>
<td><a href="/item/{{ item.id }}">{{ item.name }}</a></td>
<td>{{ item.manufacturer }}</td>
<td>{{ item.type }}</td>
<td>{{ item.updatedAt | date("m/d/Y h:i:s A") }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</section>
{% endblock %}

View File

@ -20,6 +20,7 @@
<li class="nav-link"><a href="/">Home</a></li>
<li class="nav-link"><a href="/item/search">Search</a></li>
<li class="nav-link"><a href="/item/add">Add Item</a></li>
<li class="nav-link"><a href="/license/add">Add License</a></li>
</ul>
</div>
</nav>

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

@ -0,0 +1,53 @@
{% extends 'layout.twig' %}
{% block title %}Add New License{% endblock %}
{% block content %}
<!-- page header -->
<header class="row">
<div class="columns twelve">
<h1>Add new license</h1>
</div>
</header>
<section class="row">
<div class="columns twelve">
<form action="/license/add" method="POST">
<div class="row">
<div class="columns twelve">
<label for="license_name">License name:</label>
<input class="u-full-width" type="text" placeholder="My new license" id="license_name" name="license_name" required>
</div>
</div>
<div class="row">
<div class="six columns">
<label for="license_key">License Key:</label>
<input class="u-full-width" type="text" placeholder="ABCD-EFGH-1234-5678" id="license_key" name="license_key" required>
</div>
<div class="six columns">
<label for="license_manufacturer">Manufacturer:</label>
<input class="u-full-width" type="text" placeholder="Manufacturer" id="license_manufacturer" name="license_manufacturer">
</div>
</div>
<div class="row">
<div class="six columns">
<label for="license_purchase_from">Purchased from:</label>
<input class="u-full-width" type="text" placeholder="Newegg" id="license_purchase_from" name="license_purchase_from">
</div>
<div class="six columns">
<label for="license_purchase_date">Purchased at:</label>
<input class="u-full-width" type="datetime-local" id="license_purchase_date" name="license_purchase_date">
</div>
</div>
<input class="button-primary u-full-width" type="submit" value="Submit">
</form>
</div>
</section>
{% endblock %}