Compare commits

..

2 Commits

Author SHA1 Message Date
857e9ecf63 add: role db 2025-10-07 13:45:48 +07:00
315537fc6e add: brand db 2025-10-07 13:45:38 +07:00
2 changed files with 135 additions and 0 deletions

76
db/brand.db.js Normal file
View File

@@ -0,0 +1,76 @@
const pool = require("../config");
// Get all brands
const getAllBrandsDb = async () => {
const queryText = `
SELECT *
FROM m_brands
WHERE deleted_at IS NULL
ORDER BY brand_id ASC
`;
const result = await pool.query(queryText);
return result.recordset;
};
// Get brand by ID
const getBrandByIdDb = async (id) => {
const queryText = `
SELECT *
FROM m_brands
WHERE brand_id = $1
AND deleted_at IS NULL
`;
const result = await pool.query(queryText, [id]);
return result.recordset[0];
};
// Get brand by name
const getBrandByNameDb = async (name) => {
const queryText = `
SELECT *
FROM m_brands
WHERE brand_name = $1
AND deleted_at IS NULL
`;
const result = await pool.query(queryText, [name]);
return result.recordset[0];
};
// Create brand
const createBrandDb = async (data) => {
const { query: queryText, values } = pool.buildDynamicInsert("m_brands", data);
const result = await pool.query(queryText, values);
const insertedId = result.recordset[0]?.inserted_id;
if (!insertedId) return null;
return getBrandByIdDb(insertedId);
};
// Update brand
const updateBrandDb = async (id, data) => {
const { query: queryText, values } = pool.buildDynamicUpdate("m_brands", data, { brand_id: id });
await pool.query(queryText, values);
return getBrandByIdDb(id);
};
// Soft delete brand
const softDeleteBrandDb = async (id, deletedBy) => {
const queryText = `
UPDATE m_brands
SET deleted_at = GETDATE(),
deleted_by = $1
WHERE brand_id = $2
AND deleted_at IS NULL
`;
await pool.query(queryText, [deletedBy, id]);
return true;
};
module.exports = {
getAllBrandsDb,
getBrandByIdDb,
getBrandByNameDb,
createBrandDb,
updateBrandDb,
softDeleteBrandDb,
};

59
db/role.db.js Normal file
View File

@@ -0,0 +1,59 @@
const pool = require("../config");
// Get all roles
const getAllRolesDb = async () => {
const queryText = `
SELECT *
FROM m_roles
WHERE deleted_at IS NULL
ORDER BY role_id ASC
`;
const result = await pool.query(queryText);
return result.recordset;
};
// Get role by ID
const getRoleByIdDb = async (roleId) => {
const queryText = `
SELECT *
FROM m_roles
WHERE role_id = $1 AND deleted_at IS NULL
`;
const result = await pool.query(queryText, [roleId]);
return result.recordset[0];
};
// Create role
const createRoleDb = async (data) => {
const { query: queryText, values } = pool.buildDynamicInsert("m_roles", data);
const result = await pool.query(queryText, values);
return result.recordset[0]?.inserted_id || null;
};
// Update role
const updateRoleDb = async (roleId, data) => {
const { query: queryText, values } = pool.buildDynamicUpdate("m_roles", data, { role_id: roleId });
await pool.query(queryText, values);
return true;
};
// Soft delete role
const deleteRoleDb = async (roleId, deletedBy) => {
const queryText = `
UPDATE m_roles
SET deleted_at = GETDATE(),
deleted_by = $1
WHERE role_id = $2
`;
await pool.query(queryText, [deletedBy, roleId]);
return true;
};
module.exports = {
getAllRolesDb,
getRoleByIdDb,
createRoleDb,
updateRoleDb,
deleteRoleDb,
};