mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2025-06-28 04:45:36 -04:00
feat: basic crud complete
This commit is contained in:
parent
6fb88b315a
commit
77a93ae9b1
10 changed files with 105 additions and 27 deletions
|
@ -122,13 +122,25 @@ async function itemEditGet(req, res, next) {
|
|||
}
|
||||
|
||||
async function itemEditPost(req, res, next) {
|
||||
const itemId = req.params.id;
|
||||
console.log(itemId);
|
||||
const { id, name, qty, price, category, store } = req.body;
|
||||
const category_id = await db.getCategoryId({ name: category });
|
||||
const store_id = await db.getStoreId({ name: store });
|
||||
|
||||
const update = {
|
||||
id: id,
|
||||
name: name,
|
||||
qty: qty,
|
||||
price: utils.convertToInteger(price),
|
||||
category_id: category_id.category_id,
|
||||
store_id: store_id.store_id,
|
||||
};
|
||||
|
||||
const dbRes = await db.updateItem(update);
|
||||
|
||||
res.redirect("/");
|
||||
}
|
||||
|
||||
async function itemDelete(req, res) {
|
||||
console.log(req.body.name);
|
||||
const itemId = await db.getItemByName(req.body.name);
|
||||
if (itemId) db.deleteItem(itemId);
|
||||
console.log(`Delted item: ${itemId}`);
|
||||
|
|
|
@ -33,6 +33,22 @@ async function insertItem(data) {
|
|||
}
|
||||
}
|
||||
|
||||
async function updateItem(item) {
|
||||
const { id, name, price, qty, category_id, store_id } = item;
|
||||
|
||||
try {
|
||||
await pool.query(
|
||||
`UPDATE items
|
||||
SET name=$1, price=$2, qty=$3, category_id=$4, store_id=$5
|
||||
WHERE id=$6`,
|
||||
[name, price, qty, category_id, store_id, id],
|
||||
);
|
||||
return null;
|
||||
} catch (e) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteItem(item) {
|
||||
try {
|
||||
const res = await pool.query("DELETE FROM items WHERE id = $1", [item.id]);
|
||||
|
@ -46,7 +62,7 @@ async function getAllItemsWithRelationships() {
|
|||
const { rows } = await pool.query(
|
||||
`SELECT items.id, items.name, items.price, items.qty, category.name AS category_name, store.name AS store_name FROM items
|
||||
LEFT JOIN category ON items.category_id = category.category_id
|
||||
LEFT JOIN store ON items.store_id = store.store_id;`,
|
||||
LEFT JOIN store ON items.store_id = store.store_id ORDER BY items.id;`,
|
||||
);
|
||||
|
||||
return rows;
|
||||
|
@ -117,6 +133,7 @@ module.exports = {
|
|||
getAllItems,
|
||||
getItemByName,
|
||||
deleteItem,
|
||||
updateItem,
|
||||
getCategories,
|
||||
getCategoryId,
|
||||
getStores,
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
<h1 class="heading"><%= type %></h1>
|
||||
|
||||
<div class="container">
|
||||
<%- include("card", {items: data}) %>
|
||||
<button class="btn add-btn">New <%= type %></button>
|
||||
</div>
|
||||
<form method="POST" action="/<%= type %>" class="add-form">
|
||||
<div class="container">
|
||||
<div class="form-item">
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
<div class="container">
|
||||
<% items.forEach(item => { %>
|
||||
<div class="container flex">
|
||||
<p class="title">
|
||||
|
@ -11,4 +10,3 @@
|
|||
|
||||
</div>
|
||||
<% }); %>
|
||||
</div>
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
|
||||
<form method="POST" action="/item/id">
|
||||
<div class="form-item"><label>Name</label><input name="name" value="<%= item.name %>" /></div>
|
||||
<div class="form-item"><label>Price</label><input name="price" value=<%= item.price %> /></div>
|
||||
<div class="form-item"><label>QTY</label><input name="qty" value=<%= item.qty %> /></div>
|
||||
<input type="hidden" value=<%= item.id %> name="id">
|
||||
<div class="form-item"><label>Name</label><input name="name" value="<%= item.name %>" required /></div>
|
||||
<div class="form-item"><label>Price</label><input name="price" value=<%= (item.price/100).toFixed(2) %> type="number" step="0.01" required /></div>
|
||||
<div class="form-item"><label>QTY</label><input name="qty" value=<%= item.qty %> type="number" required /></div>
|
||||
<div class="form-item">
|
||||
<label>Categories</label>
|
||||
<select class="categories-list" name="category">
|
||||
|
@ -29,6 +30,6 @@
|
|||
<% }); %>
|
||||
</select>
|
||||
</div>
|
||||
<button type="submit" class="btn">Add Item</button><button>Close</button>
|
||||
<button type="submit" class="btn">UPDATE ITEM</button><button class="btn">RETURN</button>
|
||||
</form>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue