forked from ttc/micro-service-api
--ระบบ ที่สมบูรของ search column และ condition
This commit is contained in:
@@ -22,6 +22,7 @@ export class budgetSearch {
|
|||||||
async onBudgetSearch(req, res, database) {
|
async onBudgetSearch(req, res, database) {
|
||||||
let idx = -1
|
let idx = -1
|
||||||
let aryResult = []
|
let aryResult = []
|
||||||
|
let condition = {}
|
||||||
try {
|
try {
|
||||||
// let username = req.body.request.username;
|
// let username = req.body.request.username;
|
||||||
// let password = req.body.request.password;
|
// let password = req.body.request.password;
|
||||||
@@ -35,16 +36,18 @@ export class budgetSearch {
|
|||||||
var column = ""
|
var column = ""
|
||||||
if(columnParams == 'edit'){
|
if(columnParams == 'edit'){
|
||||||
column = `bdgseq, bdgnam, bdgcod, bdgttl`
|
column = `bdgseq, bdgnam, bdgcod, bdgttl`
|
||||||
|
condition['bdgseq'] = req.body.request.bdgseq
|
||||||
} else if(columnParams == 'result' || columnParams == undefined || columnParams == ''){
|
} else if(columnParams == 'result' || columnParams == undefined || columnParams == ''){
|
||||||
column = `bdgnam, bdgttl`
|
column = `bdgnam, bdgttl`
|
||||||
}
|
}
|
||||||
aryResult = await this.budgetSearchService.getBudgetSearch(database, id, column);
|
|
||||||
|
aryResult = await this.budgetSearchService.getBudgetSearch(database, id, column, condition);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
idx = 1;
|
idx = 1;
|
||||||
} finally {
|
} finally {
|
||||||
if (idx === 1) return sendError('เกิดข้อผิดพลาดไม่คาดคิดเกิดขึ้น', 'Unexpected error');
|
if (idx === 1) return sendError('เกิดข้อผิดพลาดไม่คาดคิดเกิดขึ้น', 'Unexpected error');
|
||||||
if (!aryResult) return sendError('ไม่พบการมีอยู่ของข้อมูล', 'Cannot Find Any Data');
|
if (aryResult == 0) return sendError('ไม่พบการมีอยู่ของข้อมูล', 'Cannot Find Any Data');
|
||||||
return aryResult
|
return aryResult
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,14 +6,15 @@ export class BudgetSearchService {
|
|||||||
this.generalService = new GeneralService()
|
this.generalService = new GeneralService()
|
||||||
}
|
}
|
||||||
|
|
||||||
async getBudgetSearch(database, id, column) {
|
async getBudgetSearch(database, id, column, condition) {
|
||||||
const sql = `
|
const sql = `
|
||||||
SELECT
|
SELECT
|
||||||
${column}
|
${column}
|
||||||
FROM ${database}.bdgmst
|
FROM ${database}.bdgmst
|
||||||
|
WHERE 1=1
|
||||||
`
|
`
|
||||||
const params = []
|
const params = []
|
||||||
const result = await this.generalService.executeQueryParam(database, sql, params);
|
const result = await this.generalService.executeQueryConditions(database, sql, condition);
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,9 @@ export class GeneralService {
|
|||||||
|
|
||||||
for (const [key, value] of Object.entries(conditions)) {
|
for (const [key, value] of Object.entries(conditions)) {
|
||||||
if (value === undefined || value === null || value === '') continue
|
if (value === undefined || value === null || value === '') continue
|
||||||
|
|
||||||
const match = String(value).match(/^(ILIKE|LIKE)\s+(.+)$/i)
|
const match = String(value).match(/^(ILIKE|LIKE)\s+(.+)$/i)
|
||||||
|
|
||||||
if (match) {
|
if (match) {
|
||||||
const operator = match[1].toUpperCase()
|
const operator = match[1].toUpperCase()
|
||||||
const pattern = match[2].trim()
|
const pattern = match[2].trim()
|
||||||
@@ -69,6 +71,7 @@ export class GeneralService {
|
|||||||
|
|
||||||
let finalQuery = baseQuery
|
let finalQuery = baseQuery
|
||||||
if (whereClauses.length > 0) finalQuery += ' AND ' + whereClauses.join(' AND ')
|
if (whereClauses.length > 0) finalQuery += ' AND ' + whereClauses.join(' AND ')
|
||||||
|
|
||||||
const formattedSQL = finalQuery.replace(/\${database}/g, database)
|
const formattedSQL = finalQuery.replace(/\${database}/g, database)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -77,15 +80,20 @@ export class GeneralService {
|
|||||||
sql: formattedSQL,
|
sql: formattedSQL,
|
||||||
params
|
params
|
||||||
})
|
})
|
||||||
|
|
||||||
const result = await connection.query(formattedSQL, params)
|
const result = await connection.query(formattedSQL, params)
|
||||||
|
|
||||||
this.devhint(2, 'executeQueryConditions', `✅ Query Success (${result.rowCount} rows)`)
|
this.devhint(2, 'executeQueryConditions', `✅ Query Success (${result.rowCount} rows)`)
|
||||||
return result.rows
|
|
||||||
|
return result.rows
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.devhint(1, 'executeQueryConditions', `❌ SQL Error`, err.message)
|
this.devhint(1, 'executeQueryConditions', `❌ SQL Error`, err.message)
|
||||||
console.error('🧨 SQL Error:', err.message)
|
console.error('🧨 SQL Error:', err.message)
|
||||||
throw new Error(`SQL_EXECUTION_FAILED::${err.message}`) // ✅ “เจ๊งจริง” ส่งถึง controller แน่นอน
|
throw new Error(`SQL_EXECUTION_FAILED::${err.message}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
// ===================================================
|
// ===================================================
|
||||||
// Export สำหรับ controller หรืออื่นๆ เรียกใช้ได้ด้วย
|
// Export สำหรับ controller หรืออื่นๆ เรียกใช้ได้ด้วย
|
||||||
|
|||||||
Reference in New Issue
Block a user