2 Commits

5 changed files with 39 additions and 16 deletions

View File

@@ -30,7 +30,7 @@ const AddItemForm: React.FC<AddItemFormProps> = ({ onClose, alert }) => {
</Field.Root>
<Field.Root>
<Field.Label>Schließfachnummer (immer zwei Zahlen)</Field.Label>
<Input id="lockerNumber" placeholder="Nummer 01 - 06" />
<Input id="safe_nr" placeholder="Nummer 01 - 06" />
</Field.Root>
<Field.Root>
<Field.Label>Ausleih-Berechtigung (Rolle)</Field.Label>
@@ -57,17 +57,16 @@ const AddItemForm: React.FC<AddItemFormProps> = ({ onClose, alert }) => {
(document.getElementById("can_borrow_role") as HTMLInputElement)
?.value
);
const lockerValue = (
document.getElementById("lockerNumber") as HTMLInputElement
const safeNrValue = (
document.getElementById("safe_nr") as HTMLInputElement
)?.value.trim();
const lockerNumber =
lockerValue === "" ? null : Number(lockerValue);
const safeNr = safeNrValue === "" ? null : safeNrValue;
if (!name || Number.isNaN(role)) return;
if (lockerNumber !== null && Number.isNaN(lockerNumber)) return;
if (safeNr !== null && !/^\d{2}$/.test(safeNr)) return;
const res = await createItem(name, role, lockerNumber);
const res = await createItem(name, role, safeNr);
if (res.success) {
alert(
"success",

View File

@@ -165,7 +165,7 @@ export const deleteItem = async (itemId: number) => {
export const createItem = async (
item_name: string,
can_borrow_role: number,
lockerNumber: number | null
lockerNumber: string | null
) => {
console.log(JSON.stringify({ item_name, can_borrow_role, lockerNumber }));
try {

View File

@@ -114,3 +114,14 @@ export const getAllLoansV2 = async () => {
}
return { success: false };
};
export const openDoor = async (doorKey) => {
const [result] = await pool.query(
"SELECT lockers FROM doorKeys WHERE door_key = ?;",
[doorKey]
);
if (result.length > 0) {
return { success: true, data: result[0] };
}
return { success: false };
};

View File

@@ -10,6 +10,7 @@ import {
setTakeDateV2,
setReturnDateV2,
getLoanByCodeV2,
openDoor,
} from "./api.database.js";
// Route for API to get all items from the database
@@ -79,4 +80,15 @@ router.post(
}
);
router.get("/open-door/:key/:doorKey", authenticate, async (req, res) => {
const doorKey = req.params.doorKey;
const result = await openDoor(doorKey);
if (result.success) {
res.status(200).json({ data: result.data });
} else {
res.status(500).json({ message: "Failed to open door" });
}
});
export default router;

View File

@@ -37,20 +37,14 @@ CREATE TABLE items (
item_name varchar(255) NOT NULL UNIQUE,
can_borrow_role INT NOT NULL,
in_safe bool NOT NULL DEFAULT true,
safe_nr CHAR(2) DEFAULT NULL,
safe_nr INT DEFAULT NULL UNIQUE,
entry_created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
entry_updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
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),
UNIQUE KEY ux_items_safe_nr (safe_nr)
PRIMARY KEY (id)
) ENGINE=InnoDB;
CREATE UNIQUE INDEX ux_items_safe_nr_not_null
ON items (safe_nr)
WHERE safe_nr IS NOT NULL;
CREATE TABLE apiKeys (
id INT NOT NULL AUTO_INCREMENT,
api_key CHAR(8) NOT NULL UNIQUE,
@@ -59,4 +53,11 @@ CREATE TABLE apiKeys (
entry_created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
CHECK (api_key REGEXP '^[0-9]{8}$')
) ENGINE=InnoDB;
CREATE TABLE doorKeys (
id int NOT NULL AUTO_INCREMENT,
door_key INT NOT NULL,
lockers INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB;