Files
cod-api/db/brand.db.js
2025-10-07 13:45:38 +07:00

77 lines
1.7 KiB
JavaScript

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,
};