update: device db

This commit is contained in:
2025-10-07 15:12:40 +07:00
parent ddf9784213
commit ba7f746433

View File

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