diff --git a/db/brand.db.js b/db/brand.db.js index 4a80cf0..5693955 100644 --- a/db/brand.db.js +++ b/db/brand.db.js @@ -1,24 +1,46 @@ const pool = require("../config"); // 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 = ` - SELECT * - FROM m_brands - WHERE deleted_at IS NULL - ORDER BY brand_id ASC + 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); + const result = await pool.query(queryText, queryParams); return result.recordset; }; // Get brand by ID const getBrandByIdDb = async (id) => { const queryText = ` - SELECT * + 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 + WHERE brand_id = $1 AND deleted_at IS NULL `; const result = await pool.query(queryText, [id]); return result.recordset[0]; @@ -27,10 +49,17 @@ const getBrandByIdDb = async (id) => { // Get brand by name const getBrandByNameDb = async (name) => { const queryText = ` - SELECT * + 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 + WHERE brand_name = $1 AND deleted_at IS NULL `; const result = await pool.query(queryText, [name]); return result.recordset[0]; @@ -38,18 +67,24 @@ const getBrandByNameDb = async (name) => { // Create brand const createBrandDb = async (data) => { - const { query: queryText, values } = pool.buildDynamicInsert("m_brands", data); - const result = await pool.query(queryText, values); + 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: queryText, values } = pool.buildDynamicUpdate("m_brands", data, { brand_id: id }); - await pool.query(queryText, values); + const { query, values } = pool.buildDynamicUpdate( + "m_brands", + { ...data, updated_at: new Date() }, + { brand_id: id } + ); + await pool.query(query, values); return getBrandByIdDb(id); }; @@ -59,8 +94,7 @@ const softDeleteBrandDb = async (id, deletedBy) => { UPDATE m_brands SET deleted_at = GETDATE(), 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]); return true;