more code

This commit is contained in:
Smigz 2025-01-17 22:45:33 -05:00
parent 17d664eaeb
commit 6fb88b315a
10 changed files with 130 additions and 14 deletions

View file

@ -1,3 +1,4 @@
const utils = require("../utils");
const db = require("../db/queries");
const { links } = require("../config");
@ -32,6 +33,13 @@ async function storePost(req, res) {
const r = await handlePostRequets({ reqBody: req.body, dbName: "store" });
res.redirect("/store");
}
async function storeDelete(req, res) {
const query = await db.deleteStore(req.body.id);
if (query) {
res.send(`${query}`);
}
res.send("store deleted");
}
async function categoryGet(req, res) {
const categories = await db.getCategories();
@ -75,9 +83,10 @@ async function itemPost(req, res) {
const storeId = await db.getStoreId({ name: req.body.store });
let formData = req.body;
const data = {
name: formData.name,
price: formData.price,
price: utils.convertToInteger(formData.price),
qty: formData.qty,
store_id: storeId.store_id,
category_id: categoryId.category_id,
@ -142,6 +151,7 @@ module.exports = {
itemDelete,
storeGet,
storePost,
storeDelete,
storesGetAll,
categoryGet,
categoryPost,

View file

@ -103,6 +103,14 @@ async function insertStore(data) {
}
}
async function deleteStore(itemId) {
try {
await pool.query("DELETE FROM store where store_id = $1", [itemId]);
} catch (e) {
return e;
}
}
module.exports = {
insertItem,
getItemById,
@ -113,6 +121,7 @@ module.exports = {
getCategoryId,
getStores,
getStoreId,
deleteStore,
insertStore,
insertCategory,
deleteCategory,

View file

@ -8,6 +8,7 @@ indexRouter.get("/", indexController.indexGet);
indexRouter.get("/store", indexController.storeGet);
indexRouter.get("/stores", indexController.storesGetAll);
indexRouter.post("/store", indexController.storePost);
indexRouter.delete("/store", indexController.storeDelete);
indexRouter.get("/category", indexController.categoryGet);
indexRouter.get("/categories", indexController.categoryGetAll);
indexRouter.post("/category", indexController.categoryPost);

3
inventory/src/utils.js Normal file
View file

@ -0,0 +1,3 @@
exports.convertToInteger = (num) => {
return num * 100;
};

View file

@ -1,5 +1,8 @@
<h1><%= type %></h1>
<form method="POST" action="/<%= type %>">
<h1 class="heading"><%= type %></h1>
<%- include("card", {items: data}) %>
<button class="btn add-btn">New <%= type %></button>
<form method="POST" action="/<%= type %>" class="add-form">
<div class="container">
<div class="form-item">
<label for="name">Name</label>
@ -13,4 +16,3 @@
</form>
<%- include("card", {items: data}) %>

View file

@ -1,5 +1,6 @@
<%- include('header', {links: links}); %>
<main>
<% if (page === "add") { %>
<%- include('addForm', {type: type}); %>
<% } else if (page === "edit") { %>
@ -7,4 +8,5 @@
<% } else { %>
<%- include('itemTable', {items: items}); %>
<% }; %>
</main>
<%- include('footer'); %>

View file

@ -1,4 +1,4 @@
<table class="items">
<table class="items-table">
<thead>
<tr>
<th scope="col">Name</th>
@ -6,6 +6,8 @@
<th scope="col">QTY</th>
<th scope="col">Store</th>
<th scope="col">Category</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@ -13,12 +15,12 @@
<% items.forEach(item => { %>
<tr data-item-id="<%=item.id %>">
<td><%= item.name %></td>
<td><%= item.price %></td>
<td>$<%= (item.price/100).toFixed(2) %></td>
<td><%= item.qty %></td>
<td><%= item.store_name %></td>
<td><%= item.category_name %></td>
<td class="edit-button">Edit</td>
<td class="delete-button" data-id="<%= item.name %>">Delete</td>
<td><button class="edit-button btn">Edit</button></td>
<td><button data-id="<%= item.name %>" class="delete-button btn">Delete</button></td>
</tr>
<% }); %>
</tbody>