diff --git a/assets/styles/gargoyle.scss b/assets/styles/gargoyle.scss index 441675c..12956af 100644 --- a/assets/styles/gargoyle.scss +++ b/assets/styles/gargoyle.scss @@ -89,7 +89,9 @@ input[type="submit"].button-primary{ #item-header{ margin-bottom: 25px; - h1{ + h1, + p{ + display: inline-block; margin-bottom: 7px; } diff --git a/index.js b/index.js index 77127d5..1a6be91 100644 --- a/index.js +++ b/index.js @@ -29,6 +29,8 @@ app.get('/', homeRoutes.getIndex); app.get('/item/add', itemRoutes.getAdd); 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); // start app app.listen(port, () => { diff --git a/src/routes/item.js b/src/routes/item.js index 7ff6e1e..c9e5f80 100644 --- a/src/routes/item.js +++ b/src/routes/item.js @@ -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); +}; diff --git a/views/index.twig b/views/index.twig index eb97e2d..3d53738 100644 --- a/views/index.twig +++ b/views/index.twig @@ -40,6 +40,7 @@ Name Manufacturer Type + Updated at @@ -48,6 +49,7 @@ {{ item.name }} {{ item.manufacturer }} {{ item.type }} + {{ item.updatedAt | date("m/d/Y h:i:s A") }} {% endfor %} diff --git a/views/item/edit.twig b/views/item/edit.twig new file mode 100644 index 0000000..c364169 --- /dev/null +++ b/views/item/edit.twig @@ -0,0 +1,73 @@ +{% extends 'layout.twig' %} + +{% block title %}Edit Item{% endblock %} + +{% block content %} + + +
+
+

Editing "{{ item.name }}"

+
+
+ +
+
+
+
+
+ + +
+
+ +
+
+ + +
+ +
+ + +
+
+ +
+
+ + +
+ +
+ + +
+
+ +
+
+ + +
+ +
+ + +
+
+ + +
+
+
+ +{% endblock %} diff --git a/views/item/view.twig b/views/item/view.twig index 1931636..f32c269 100644 --- a/views/item/view.twig +++ b/views/item/view.twig @@ -7,7 +7,10 @@
-

{{ item.name }}

+ +

{{ item.name }}

+

Edit

+

Item added at: {{ item.createdAt }}

Last updated at: {{ item.updatedAt }}