From 5e895aa3ca2ebd7d584f75bb5ed42ea0cc0ca1f8 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Thu, 3 Nov 2022 22:28:01 -0400 Subject: [PATCH] Fixed code style errors --- index.js | 8 ++++++-- src/models/index.js | 6 +++--- src/models/item.js | 30 ++++++++++++++---------------- src/routes/home.js | 6 +++--- src/routes/item.js | 36 +++++++++++++++++++++--------------- 5 files changed, 47 insertions(+), 39 deletions(-) diff --git a/index.js b/index.js index 1a6be91..9b043b1 100644 --- a/index.js +++ b/index.js @@ -7,12 +7,16 @@ const port = 3000; // initialize database connection (async () => { const db = require('./src/models'); - await db.sequelize.sync({ alter: true }); + await db.sequelize.sync({ + alter: true, + }); })(); // set up body POST parameters app.use(express.json()); -app.use(express.urlencoded({ extended: true })); +app.use(express.urlencoded({ + extended: true, +})); // load the template engine app.set('view engine', 'twig'); diff --git a/src/models/index.js b/src/models/index.js index 8db56ce..c6ed1f3 100644 --- a/src/models/index.js +++ b/src/models/index.js @@ -1,9 +1,9 @@ const dbConfig = require('config').get('database'); -const Sequelize = require("sequelize"); +const Sequelize = require('sequelize'); const sequelize = new Sequelize({ dialect: dbConfig.get('driver'), - storage: dbConfig.get('connection_string') + storage: dbConfig.get('connection_string'), }); const db = {}; @@ -11,6 +11,6 @@ const db = {}; db.Sequelize = Sequelize; db.sequelize = sequelize; -db.items = require("./item.js")(sequelize, Sequelize); +db.items = require('./item.js')(sequelize, Sequelize); module.exports = db; diff --git a/src/models/item.js b/src/models/item.js index 15e7d2f..6d5f142 100644 --- a/src/models/item.js +++ b/src/models/item.js @@ -1,37 +1,35 @@ module.exports = (sequelize, Sequelize) => { + const Item = sequelize.define('item', { - const Item = sequelize.define("item", { - name: { - type: Sequelize.STRING + type: Sequelize.STRING, }, - + manufacturer: { - type: Sequelize.STRING + type: Sequelize.STRING, }, serialNumber: { - type: Sequelize.STRING + type: Sequelize.STRING, }, skuNumber: { - type: Sequelize.STRING + type: Sequelize.STRING, }, - + type: { - type: Sequelize.STRING + type: Sequelize.STRING, }, purchasedFrom: { - type: Sequelize.STRING + type: Sequelize.STRING, }, purchasedAt: { - type: Sequelize.DATE - } - - }); - - return Item; + type: Sequelize.DATE, + }, + }); + + return Item; }; diff --git a/src/routes/home.js b/src/routes/home.js index 864dfd3..3bf02f5 100644 --- a/src/routes/home.js +++ b/src/routes/home.js @@ -2,12 +2,12 @@ const db = require('../models'); const Item = db.items; // GET - / -exports.getIndex = async function (req, res) { - let items = await Item.findAll({ +exports.getIndex = async function(req, res) { + const items = await Item.findAll({ limit: 10, order: [ ['updatedAt', 'DESC'], - ] + ], }); res.render('index.twig', { diff --git a/src/routes/item.js b/src/routes/item.js index c9e5f80..e27960f 100644 --- a/src/routes/item.js +++ b/src/routes/item.js @@ -2,12 +2,12 @@ const db = require('../models'); const Item = db.items; // GET - /item/add -exports.getAdd = async function (req, res) { +exports.getAdd = async function(req, res) { res.render('item/add.twig'); }; // POST - /item/add -exports.postAdd = async function (req, res) { +exports.postAdd = async function(req, res) { const item = await Item.create({ name: req.body.item_name, serialNumber: req.body.item_serial, @@ -24,10 +24,12 @@ exports.postAdd = async function (req, res) { }; // GET - /item/{id} -exports.getItem = async function (req, res) { - const item = await Item.findAll({ where: { - id: req.params.id, - }}); +exports.getItem = async function(req, res) { + const item = await Item.findAll({ + where: { + id: req.params.id, + }, + }); res.render('item/view.twig', { item: item[0], @@ -35,10 +37,12 @@ exports.getItem = async function (req, res) { }; // GET - /item/{id}/edit -exports.getItemEdit = async function (req, res) { - const item = await Item.findAll({ where: { - id: req.params.id, - }}); +exports.getItemEdit = async function(req, res) { + const item = await Item.findAll({ + where: { + id: req.params.id, + }, + }); res.render('item/edit.twig', { item: item[0], @@ -46,14 +50,16 @@ exports.getItemEdit = async function (req, res) { }; // POST - /item/{id}/edit -exports.postItemEdit = async function (req, res) { +exports.postItemEdit = async function(req, res) { // fetch item from DB - const itemSearch = await Item.findAll({ where: { - id: req.params.id, - }}); + const itemSearch = await Item.findAll({ + where: { + id: req.params.id, + }, + }); // retrieve the item record from the array for ease of use - let item = itemSearch[0]; + const item = itemSearch[0]; // update item attributes item.name = req.body.item_name;