fixed bugs

This commit is contained in:
2025-11-23 21:39:18 +01:00
parent 4b9f55268c
commit 85e6d7fe00
5 changed files with 22 additions and 9 deletions

View File

@@ -68,7 +68,7 @@ const ItemTable: React.FC = () => {
const handleLockerNumberChange = (id: number, value: string) => {
setItems((prev) =>
prev.map((it) => (it.id === id ? { ...it, lockerNumber: value } : it))
prev.map((it) => (it.id === id ? { ...it, safe_nr: value } : it))
);
};

View File

@@ -200,7 +200,6 @@ export const handleEditItems = async (
safe_nr: string | null,
can_borrow_role: string
) => {
const newSafeNr = Number(safe_nr || 0);
try {
const response = await fetch(
`${API_BASE}/api/admin/item-data/edit-item/${itemId}`,
@@ -210,7 +209,7 @@ export const handleEditItems = async (
"Content-Type": "application/json",
Authorization: `Bearer ${Cookies.get("token")}`,
},
body: JSON.stringify({ item_name, newSafeNr, can_borrow_role }),
body: JSON.stringify({ item_name, safe_nr, can_borrow_role }),
}
);
if (!response.ok) {

View File

@@ -32,10 +32,21 @@ export const createItem = async (item_name, can_borrow_role, lockerNumber) => {
return { success: false };
};
export const editItemById = async (itemId, item_name, can_borrow_role) => {
export const editItemById = async (
itemId,
item_name,
can_borrow_role,
safe_nr
) => {
let newSafeNr;
if (safe_nr === null || safe_nr === "") {
newSafeNr = null;
} else {
newSafeNr = safe_nr;
}
const [result] = await pool.query(
"UPDATE items SET item_name = ?, can_borrow_role = ?, entry_updated_at = NOW() WHERE id = ?",
[item_name, can_borrow_role, itemId]
"UPDATE items SET item_name = ?, can_borrow_role = ?, safe_nr = ?, entry_updated_at = NOW() WHERE id = ?",
[item_name, can_borrow_role, newSafeNr, itemId]
);
if (result.affectedRows > 0) return { success: true };
return { success: false };

View File

@@ -41,11 +41,13 @@ router.post("/create-item", authenticateAdmin, async (req, res) => {
router.post("/edit-item/:id", authenticateAdmin, async (req, res) => {
const itemId = req.params.id;
const { item_name, can_borrow_role } = req.body;
const { item_name, can_borrow_role, safe_nr } = req.body;
const result = await editItemById(
itemId,
item_name,
can_borrow_role
can_borrow_role,
safe_nr
);
if (result.success) {
return res.status(200).json({ message: "Item edited successfully" });

View File

@@ -43,7 +43,8 @@ CREATE TABLE items (
last_borrowed_person varchar(255) DEFAULT NULL,
currently_borrowing varchar(255) DEFAULT NULL,
PRIMARY KEY (id),
CHECK (safe_nr REGEXP '^[0-9]{2}$' OR safe_nr IS NULL)
CHECK (safe_nr REGEXP '^[0-9]{2}$' OR safe_nr IS NULL),
UNIQUE KEY ux_items_safe_nr (safe_nr)
) ENGINE=InnoDB;
CREATE UNIQUE INDEX ux_items_safe_nr_not_null