add: brand db
This commit is contained in:
116
db/brand.db.js
116
db/brand.db.js
@@ -1,65 +1,66 @@
|
|||||||
const pool = require("../config");
|
const pool = require("../config");
|
||||||
|
|
||||||
// Get all brands
|
// Get all brands
|
||||||
const getAllBrandsDb = async (filters = {}) => {
|
const getAllBrandsDb = async (searchParams = {}) => {
|
||||||
const { whereConditions, queryParams } = pool.buildFilterQuery([
|
let queryParams = [];
|
||||||
{ 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 ")}` : "";
|
if (searchParams.limit) {
|
||||||
|
const page = Number(searchParams.page ?? 1) - 1;
|
||||||
|
queryParams = [Number(searchParams.limit ?? 10), page];
|
||||||
|
}
|
||||||
|
|
||||||
|
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
|
||||||
|
["b.brand_name"],
|
||||||
|
searchParams.criteria,
|
||||||
|
queryParams
|
||||||
|
);
|
||||||
|
|
||||||
|
queryParams = whereParamOr ? whereParamOr : queryParams;
|
||||||
|
|
||||||
|
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
|
||||||
|
[
|
||||||
|
{ column: "b.brand_name", param: searchParams.name, type: "string" },
|
||||||
|
{ column: "b.created_by", param: searchParams.created_by, type: "number" },
|
||||||
|
],
|
||||||
|
queryParams
|
||||||
|
);
|
||||||
|
|
||||||
|
queryParams = whereParamAnd ? whereParamAnd : queryParams;
|
||||||
|
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT
|
SELECT COUNT(*) OVER() AS total_data, b.*
|
||||||
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
|
FROM m_brands b
|
||||||
WHERE b.deleted_at IS NULL ${whereClause}
|
WHERE b.deleted_at IS NULL
|
||||||
|
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
|
||||||
|
${whereOrConditions ? whereOrConditions : ""}
|
||||||
ORDER BY b.brand_id ASC
|
ORDER BY b.brand_id ASC
|
||||||
|
${searchParams.limit ? `OFFSET $2 ROWS FETCH NEXT $1 ROWS ONLY` : ""};
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const result = await pool.query(queryText, queryParams);
|
const result = await pool.query(queryText, queryParams);
|
||||||
return result.recordset;
|
|
||||||
|
const total = result?.recordset.length > 0 ? parseInt(result.recordset[0].total_data, 10) : 0;
|
||||||
|
|
||||||
|
return { data: result.recordset, total };
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get brand by ID
|
// Get brand by ID
|
||||||
const getBrandByIdDb = async (id) => {
|
const getBrandByIdDb = async (id) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT
|
SELECT b.*
|
||||||
brand_id,
|
FROM m_brands b
|
||||||
brand_name,
|
WHERE b.brand_id = $1 AND b.deleted_at IS NULL
|
||||||
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]);
|
const result = await pool.query(queryText, [id]);
|
||||||
return result.recordset[0];
|
return result.recordset;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get brand by name
|
// Get brand by name
|
||||||
const getBrandByNameDb = async (name) => {
|
const getBrandByNameDb = async (name) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT
|
SELECT b.*
|
||||||
brand_id,
|
FROM m_brands b
|
||||||
brand_name,
|
WHERE b.brand_name = $1 AND b.deleted_at IS NULL
|
||||||
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]);
|
const result = await pool.query(queryText, [name]);
|
||||||
return result.recordset[0];
|
return result.recordset[0];
|
||||||
@@ -67,33 +68,38 @@ const getBrandByNameDb = async (name) => {
|
|||||||
|
|
||||||
// Create brand
|
// Create brand
|
||||||
const createBrandDb = async (data) => {
|
const createBrandDb = async (data) => {
|
||||||
const { query, values } = pool.buildDynamicInsert("m_brands", {
|
const store = {
|
||||||
...data,
|
...data,
|
||||||
created_at: new Date(),
|
created_at: new Date(),
|
||||||
});
|
};
|
||||||
const result = await pool.query(query, values);
|
|
||||||
|
const { query: queryText, values } = pool.buildDynamicInsert("m_brands", store);
|
||||||
|
const result = await pool.query(queryText, values);
|
||||||
const insertedId = result.recordset[0]?.inserted_id;
|
const insertedId = result.recordset[0]?.inserted_id;
|
||||||
if (!insertedId) return null;
|
return insertedId ? await getBrandByIdDb(insertedId) : null;
|
||||||
return getBrandByIdDb(insertedId);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Update brand
|
// Update brand
|
||||||
const updateBrandDb = async (id, data) => {
|
const updateBrandDb = async (id, data) => {
|
||||||
const { query, values } = pool.buildDynamicUpdate(
|
const store = {
|
||||||
"m_brands",
|
...data,
|
||||||
{ ...data, updated_at: new Date() },
|
updated_at: new Date(),
|
||||||
{ brand_id: id }
|
};
|
||||||
);
|
|
||||||
await pool.query(query, values);
|
const whereData = {
|
||||||
|
brand_id: id,
|
||||||
|
};
|
||||||
|
|
||||||
|
const { query: queryText, values } = pool.buildDynamicUpdate("m_brands", store, whereData);
|
||||||
|
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
|
||||||
return getBrandByIdDb(id);
|
return getBrandByIdDb(id);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Soft delete brand
|
// Soft delete brand
|
||||||
const softDeleteBrandDb = async (id, deletedBy) => {
|
const deleteBrandDb = async (id, deletedBy) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
UPDATE m_brands
|
UPDATE m_brands
|
||||||
SET deleted_at = GETDATE(),
|
SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1
|
||||||
deleted_by = $1
|
|
||||||
WHERE brand_id = $2 AND deleted_at IS NULL
|
WHERE brand_id = $2 AND deleted_at IS NULL
|
||||||
`;
|
`;
|
||||||
await pool.query(queryText, [deletedBy, id]);
|
await pool.query(queryText, [deletedBy, id]);
|
||||||
@@ -106,5 +112,5 @@ module.exports = {
|
|||||||
getBrandByNameDb,
|
getBrandByNameDb,
|
||||||
createBrandDb,
|
createBrandDb,
|
||||||
updateBrandDb,
|
updateBrandDb,
|
||||||
softDeleteBrandDb,
|
deleteBrandDb,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user