feat: /search
This commit is contained in:
parent
980cc4740f
commit
82ecb49292
3 changed files with 281 additions and 0 deletions
70
server/api/admin/users/search.get.ts
Normal file
70
server/api/admin/users/search.get.ts
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import { queryNnas } from '../../../utils/nnasDb'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const queryParams = getQuery(event)
|
||||
const searchTerm = String(queryParams.q || '').trim()
|
||||
const page = Math.max(1, parseInt(String(queryParams.page || '1'), 10))
|
||||
const limit = Math.max(1, Math.min(50, parseInt(String(queryParams.limit || '10'), 10)))
|
||||
const offset = (page - 1) * limit
|
||||
|
||||
if (!searchTerm) {
|
||||
return {
|
||||
data: [],
|
||||
pagination: { total: 0, page, limit, totalPages: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const wildPattern = `%${searchTerm}%`
|
||||
const startsWithPattern = `${searchTerm}%`
|
||||
|
||||
const countResult = await queryNnas(
|
||||
`SELECT COUNT(*) AS total
|
||||
FROM users
|
||||
WHERE username ILIKE $1
|
||||
OR CAST(pid AS TEXT) ILIKE $1
|
||||
OR email ILIKE $1`,
|
||||
[wildPattern]
|
||||
)
|
||||
|
||||
const total = parseInt(countResult.rows[0]?.total || '0', 10)
|
||||
const totalPages = Math.ceil(total / limit)
|
||||
|
||||
const dataResult = await queryNnas(
|
||||
`SELECT
|
||||
pid AS id,
|
||||
username AS sfid,
|
||||
pid,
|
||||
email
|
||||
FROM users
|
||||
WHERE username ILIKE $1
|
||||
OR CAST(pid AS TEXT) ILIKE $1
|
||||
OR email ILIKE $1
|
||||
ORDER BY
|
||||
CASE
|
||||
WHEN LOWER(username) = LOWER($2) OR CAST(pid AS TEXT) = $2 THEN 0
|
||||
WHEN username ILIKE $3 OR email ILIKE $3 OR CAST(pid AS TEXT) ILIKE $3 THEN 1
|
||||
ELSE 2
|
||||
END,
|
||||
pid DESC
|
||||
LIMIT $4 OFFSET $5`,
|
||||
[wildPattern, searchTerm, startsWithPattern, limit, offset]
|
||||
)
|
||||
|
||||
return {
|
||||
data: dataResult.rows,
|
||||
pagination: {
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('[NNAS DB SEARCH ERROR]:', error)
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: `NNAS Database query failed`
|
||||
})
|
||||
}
|
||||
})
|
||||
Loading…
Reference in a new issue