diff --git a/db/device.db.js b/db/device.db.js index 1b2581c..0852aff 100644 --- a/db/device.db.js +++ b/db/device.db.js @@ -1,12 +1,13 @@ const pool = require("../config"); -// Get all devices +// Get all devices with brand info const getAllDevicesDb = async () => { const queryText = ` - SELECT * - FROM m_device - WHERE deleted_at IS NULL - ORDER BY device_id ASC + SELECT d.*, b.brand_name + FROM m_device d + LEFT JOIN m_brands b ON d.brand_id = b.brand_id + WHERE d.deleted_at IS NULL + ORDER BY d.device_id ASC `; const result = await pool.query(queryText); return result.recordset; @@ -15,17 +16,19 @@ const getAllDevicesDb = async () => { // Search devices by keyword const searchDevicesDb = async (keyword) => { const queryText = ` - SELECT * - FROM m_device - WHERE deleted_at IS NULL + SELECT d.*, b.brand_name + FROM m_device d + LEFT JOIN m_brands b ON d.brand_id = b.brand_id + WHERE d.deleted_at IS NULL AND ( - device_name LIKE '%' + $1 + '%' - OR device_code LIKE '%' + $1 + '%' - OR device_location LIKE '%' + $1 + '%' - OR ip_address LIKE '%' + $1 + '%' - OR device_description LIKE '%' + $1 + '%' + d.device_name LIKE '%' + $1 + '%' + OR d.device_code LIKE '%' + $1 + '%' + OR d.device_location LIKE '%' + $1 + '%' + OR d.ip_address LIKE '%' + $1 + '%' + OR d.device_description LIKE '%' + $1 + '%' + OR b.brand_name LIKE '%' + $1 + '%' ) - ORDER BY device_id ASC + ORDER BY d.device_id ASC `; const result = await pool.query(queryText, [keyword]); return result.recordset; @@ -34,10 +37,11 @@ const searchDevicesDb = async (keyword) => { // Get device by ID const getDeviceByIdDb = async (id) => { const queryText = ` - SELECT * - FROM m_device - WHERE device_id = $1 - AND deleted_at IS NULL + SELECT d.*, b.brand_name + FROM m_device d + LEFT JOIN m_brands b ON d.brand_id = b.brand_id + WHERE d.device_id = $1 + AND d.deleted_at IS NULL `; const result = await pool.query(queryText, [id]); return result.recordset[0]; @@ -46,10 +50,11 @@ const getDeviceByIdDb = async (id) => { // Get device by device_code const getDeviceByCodeDb = async (code) => { const queryText = ` - SELECT * - FROM m_device - WHERE device_code = $1 - AND deleted_at IS NULL + SELECT d.*, b.brand_name + FROM m_device d + LEFT JOIN m_brands b ON d.brand_id = b.brand_id + WHERE d.device_code = $1 + AND d.deleted_at IS NULL `; const result = await pool.query(queryText, [code]); return result.recordset[0];