diff --git a/backendV2/routes/api/api.database.js b/backendV2/routes/api/api.database.js index 507453b..4478d00 100644 --- a/backendV2/routes/api/api.database.js +++ b/backendV2/routes/api/api.database.js @@ -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 }; +}; diff --git a/backendV2/routes/api/api.route.js b/backendV2/routes/api/api.route.js index 4d1dcee..8edec6e 100644 --- a/backendV2/routes/api/api.route.js +++ b/backendV2/routes/api/api.route.js @@ -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; diff --git a/backendV2/schemeV2.sql b/backendV2/schemeV2.sql index 934ed5a..05c9b24 100644 --- a/backendV2/schemeV2.sql +++ b/backendV2/schemeV2.sql @@ -37,14 +37,12 @@ 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 TABLE apiKeys ( @@ -55,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; \ No newline at end of file