111 lines
2.6 KiB
JavaScript
111 lines
2.6 KiB
JavaScript
const pool = require("../config");
|
|
|
|
// Get all brands
|
|
const getAllBrandsDb = async (filters = {}) => {
|
|
const { whereConditions, queryParams } = pool.buildFilterQuery([
|
|
{ column: "b.brand_name", param: filters.brand_name, type: "string" },
|
|
{ column: "b.created_by", param: filters.created_by, type: "number" },
|
|
]);
|
|
|
|
const whereClause = whereConditions.length ? `AND ${whereConditions.join(" AND ")}` : "";
|
|
|
|
const queryText = `
|
|
SELECT
|
|
b.brand_id,
|
|
b.brand_name,
|
|
b.created_at,
|
|
b.updated_at,
|
|
b.deleted_at,
|
|
b.created_by,
|
|
b.updated_by,
|
|
b.deleted_by
|
|
FROM m_brands b
|
|
WHERE b.deleted_at IS NULL ${whereClause}
|
|
ORDER BY b.brand_id ASC
|
|
`;
|
|
const result = await pool.query(queryText, queryParams);
|
|
return result.recordset;
|
|
};
|
|
|
|
// Get brand by ID
|
|
const getBrandByIdDb = async (id) => {
|
|
const queryText = `
|
|
SELECT
|
|
brand_id,
|
|
brand_name,
|
|
created_at,
|
|
updated_at,
|
|
deleted_at,
|
|
created_by,
|
|
updated_by,
|
|
deleted_by
|
|
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
|
|
brand_id,
|
|
brand_name,
|
|
created_at,
|
|
updated_at,
|
|
deleted_at,
|
|
created_by,
|
|
updated_by,
|
|
deleted_by
|
|
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, values } = pool.buildDynamicInsert("m_brands", {
|
|
...data,
|
|
created_at: new Date(),
|
|
});
|
|
const result = await pool.query(query, values);
|
|
const insertedId = result.recordset[0]?.inserted_id;
|
|
if (!insertedId) return null;
|
|
return getBrandByIdDb(insertedId);
|
|
};
|
|
|
|
// Update brand
|
|
const updateBrandDb = async (id, data) => {
|
|
const { query, values } = pool.buildDynamicUpdate(
|
|
"m_brands",
|
|
{ ...data, updated_at: new Date() },
|
|
{ brand_id: id }
|
|
);
|
|
await pool.query(query, 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,
|
|
};
|