add: brand.db.js
This commit is contained in:
@@ -1,24 +1,46 @@
|
|||||||
const pool = require("../config");
|
const pool = require("../config");
|
||||||
|
|
||||||
// Get all brands
|
// Get all brands
|
||||||
const getAllBrandsDb = async () => {
|
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 = `
|
const queryText = `
|
||||||
SELECT *
|
SELECT
|
||||||
FROM m_brands
|
b.brand_id,
|
||||||
WHERE deleted_at IS NULL
|
b.brand_name,
|
||||||
ORDER BY brand_id ASC
|
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);
|
const result = await pool.query(queryText, queryParams);
|
||||||
return result.recordset;
|
return result.recordset;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get brand by ID
|
// Get brand by ID
|
||||||
const getBrandByIdDb = async (id) => {
|
const getBrandByIdDb = async (id) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT *
|
SELECT
|
||||||
|
brand_id,
|
||||||
|
brand_name,
|
||||||
|
created_at,
|
||||||
|
updated_at,
|
||||||
|
deleted_at,
|
||||||
|
created_by,
|
||||||
|
updated_by,
|
||||||
|
deleted_by
|
||||||
FROM m_brands
|
FROM m_brands
|
||||||
WHERE brand_id = $1
|
WHERE brand_id = $1 AND deleted_at IS NULL
|
||||||
AND deleted_at IS NULL
|
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText, [id]);
|
const result = await pool.query(queryText, [id]);
|
||||||
return result.recordset[0];
|
return result.recordset[0];
|
||||||
@@ -27,10 +49,17 @@ const getBrandByIdDb = async (id) => {
|
|||||||
// Get brand by name
|
// Get brand by name
|
||||||
const getBrandByNameDb = async (name) => {
|
const getBrandByNameDb = async (name) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT *
|
SELECT
|
||||||
|
brand_id,
|
||||||
|
brand_name,
|
||||||
|
created_at,
|
||||||
|
updated_at,
|
||||||
|
deleted_at,
|
||||||
|
created_by,
|
||||||
|
updated_by,
|
||||||
|
deleted_by
|
||||||
FROM m_brands
|
FROM m_brands
|
||||||
WHERE brand_name = $1
|
WHERE brand_name = $1 AND deleted_at IS NULL
|
||||||
AND deleted_at IS NULL
|
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText, [name]);
|
const result = await pool.query(queryText, [name]);
|
||||||
return result.recordset[0];
|
return result.recordset[0];
|
||||||
@@ -38,18 +67,24 @@ const getBrandByNameDb = async (name) => {
|
|||||||
|
|
||||||
// Create brand
|
// Create brand
|
||||||
const createBrandDb = async (data) => {
|
const createBrandDb = async (data) => {
|
||||||
const { query: queryText, values } = pool.buildDynamicInsert("m_brands", data);
|
const { query, values } = pool.buildDynamicInsert("m_brands", {
|
||||||
const result = await pool.query(queryText, values);
|
...data,
|
||||||
|
created_at: new Date(),
|
||||||
|
});
|
||||||
|
const result = await pool.query(query, values);
|
||||||
const insertedId = result.recordset[0]?.inserted_id;
|
const insertedId = result.recordset[0]?.inserted_id;
|
||||||
if (!insertedId) return null;
|
if (!insertedId) return null;
|
||||||
|
|
||||||
return getBrandByIdDb(insertedId);
|
return getBrandByIdDb(insertedId);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Update brand
|
// Update brand
|
||||||
const updateBrandDb = async (id, data) => {
|
const updateBrandDb = async (id, data) => {
|
||||||
const { query: queryText, values } = pool.buildDynamicUpdate("m_brands", data, { brand_id: id });
|
const { query, values } = pool.buildDynamicUpdate(
|
||||||
await pool.query(queryText, values);
|
"m_brands",
|
||||||
|
{ ...data, updated_at: new Date() },
|
||||||
|
{ brand_id: id }
|
||||||
|
);
|
||||||
|
await pool.query(query, values);
|
||||||
return getBrandByIdDb(id);
|
return getBrandByIdDb(id);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -59,8 +94,7 @@ const softDeleteBrandDb = async (id, deletedBy) => {
|
|||||||
UPDATE m_brands
|
UPDATE m_brands
|
||||||
SET deleted_at = GETDATE(),
|
SET deleted_at = GETDATE(),
|
||||||
deleted_by = $1
|
deleted_by = $1
|
||||||
WHERE brand_id = $2
|
WHERE brand_id = $2 AND deleted_at IS NULL
|
||||||
AND deleted_at IS NULL
|
|
||||||
`;
|
`;
|
||||||
await pool.query(queryText, [deletedBy, id]);
|
await pool.query(queryText, [deletedBy, id]);
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user