Added ability to edit item records

This commit is contained in:
2022-11-03 22:08:45 -04:00
parent 6c64e6c9a1
commit cb5083d0d7
6 changed files with 132 additions and 13 deletions

View File

@ -1,17 +1,6 @@
const db = require('../models');
const Item = db.items;
// GET - /item/{name}
exports.getItem = async function (req, res) {
const item = await Item.findAll({ where: {
id: req.params.id,
}});
res.render('item/view.twig', {
item: item[0],
});
};
// GET - /item/add
exports.getAdd = async function (req, res) {
res.render('item/add.twig');
@ -33,3 +22,51 @@ exports.postAdd = async function (req, res) {
res.redirect('/');
};
// GET - /item/{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],
});
};
// GET - /item/{id}/edit
exports.getItemEdit = async function (req, res) {
const item = await Item.findAll({ where: {
id: req.params.id,
}});
res.render('item/edit.twig', {
item: item[0],
});
};
// POST - /item/{id}/edit
exports.postItemEdit = async function (req, res) {
// fetch item from DB
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];
// update item attributes
item.name = req.body.item_name;
item.serialNumber = req.body.item_serial;
item.skuNumber = req.body.item_sku;
item.purchasedFrom = req.body.item_purchase_from;
item.purchasedAt = req.body.item_purchase_date;
item.manufacturer = req.body.item_manufacturer;
item.type = req.body.item_type;
// save attribute changes
await item.save();
// redirect user to item page
res.redirect('/item/' + item.id);
};