Add product and storage management routes and database functions

This commit is contained in:
2026-05-26 13:57:37 +02:00
parent 4c0c441e92
commit b3c3be5590
8 changed files with 233 additions and 18 deletions
@@ -9,4 +9,59 @@ const pool = mysql
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
})
.promise();
.promise();
export const newProduct = async (
name,
description,
price,
amount,
storage_location,
expiry_date,
bottling_date,
) => {
const [result] = await pool.query(
"INSERT INTO products (name, description, price, amount, storage_location, expiry_date, bottling_date) VALUES (?, ?, ?, ?, UUID_TO_BIN(?), ?, ?)",
[
name,
description,
price,
amount,
storage_location,
expiry_date,
bottling_date,
],
);
if (result.affectedRows > 0) {
return { code: "sp001" }; // success
} else {
return { code: "ep001" }; // error
}
};
export const allProducts = async () => {
const [result] = await pool.query(`
SELECT
BIN_TO_UUID(p.uuid) AS uuid,
p.name,
p.description,
p.price,
p.amount,
BIN_TO_UUID(s.uuid) AS storage_location_uuid,
s.name AS storage_location_name,
p.expiry_date,
p.bottling_date,
p.picture,
p.created_at,
p.updated_at
FROM products p
JOIN storage_locations s ON p.storage_location = s.uuid
`);
if (result.length > 0) {
return { code: "sp002", data: result };
} else {
return { code: "ep002" };
}
};
@@ -0,0 +1,37 @@
import mysql from "mysql2";
import dotenv from "dotenv";
dotenv.config();
const pool = mysql
.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
})
.promise();
export const allStorages = async () => {
const [result] = await pool.query(
"SELECT BIN_TO_UUID(uuid) AS uuid, name, description, created_at, updated_at FROM storage_locations;",
);
if (result.length > 0) {
return { code: "ss001", data: result };
} else {
return { code: "es001" };
}
};
export const newStorage = async (name, description) => {
const [result] = await pool.query(
"INSERT INTO storage_locations (name, description) VALUES (?, ?)",
[name, description],
);
if (result.affectedRows > 0) {
return { code: "ss002" };
} else {
return { code: "es002" };
}
};
@@ -18,14 +18,14 @@ export const findUser = async (username, password) => {
);
if (result.length <= 0) {
return { code: "e001" }; // username or password is wrong
return { code: "eu001" }; // username or password is wrong
}
if (!result[0].is_active) {
return { code: "e002" }; // user is deactivated
return { code: "eu002" }; // user is deactivated
}
return { code: "s001", data: result[0] }; // user found
return { code: "su001", data: result[0] }; // user found
};
export const loginUser = async (username) => {
@@ -35,8 +35,8 @@ export const loginUser = async (username) => {
);
if (result.affectedRows > 0) {
return { code: "s002" };
return { code: "su002" };
} else {
return { code: "e003" };
return { code: "eu003" };
}
};