彻底重构-1

This commit is contained in:
yml
2026-04-09 09:06:08 +08:00
parent b509394b09
commit df2eb34478
92 changed files with 2226 additions and 1261 deletions
+105 -98
View File
@@ -1,117 +1,124 @@
import { getDb } from '../db/client.js'
import { query } from '../db/client.js'
export function getAdminUserByUsername(username) {
return getDb().prepare('SELECT * FROM admin_users WHERE username = ? LIMIT 1').get(username) || null
export async function getAdminUserById(userId) {
const result = await query(
'SELECT * FROM admin_users WHERE id = $1 LIMIT 1',
[Number(userId)],
)
return result.rows[0] || null
}
export function getAdminUserById(userId) {
return getDb().prepare('SELECT * FROM admin_users WHERE id = ? LIMIT 1').get(userId) || null
export async function getAdminUserByUsername(username) {
const result = await query(
'SELECT * FROM admin_users WHERE username = $1 LIMIT 1',
[String(username || '').trim().toLowerCase()],
)
return result.rows[0] || null
}
export function createAdminUser(input) {
const result = getDb().prepare(`
INSERT INTO admin_users (
username,
password_hash,
role,
status,
created_at,
updated_at
) VALUES (?, ?, ?, ?, ?, ?)
`).run(
input.username,
input.passwordHash,
input.role,
input.status,
input.createdAt,
input.updatedAt,
export async function createAdminUser(input) {
const result = await query(
`
INSERT INTO admin_users (
username,
password_hash,
role,
status,
created_at,
updated_at
) VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *
`,
[
input.username,
input.passwordHash,
input.role,
input.status,
input.createdAt,
input.updatedAt,
],
)
return getAdminUserById(Number(result.lastInsertRowid))
return result.rows[0] || null
}
export function listAdminUsers(query = {}) {
const conditions = []
const values = []
if (query.username) {
conditions.push('username LIKE ?')
values.push(`%${query.username}%`)
}
if (query.role) {
conditions.push('role = ?')
values.push(query.role)
}
if (query.status) {
conditions.push('status = ?')
values.push(query.status)
}
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''
const page = Number(query.page) || 1
const pageSize = Number(query.pageSize) || 20
const offset = (page - 1) * pageSize
const db = getDb()
const items = db.prepare(`
SELECT *
FROM admin_users
${whereClause}
ORDER BY id DESC
LIMIT ? OFFSET ?
`).all(...values, pageSize, offset)
const totalRow = db.prepare(`
SELECT COUNT(*) AS total
FROM admin_users
${whereClause}
`).get(...values)
return {
items,
total: Number(totalRow?.total || 0),
}
}
export function updateAdminUser(userId, patch = {}) {
const current = getAdminUserById(userId)
export async function updateAdminUser(userId, patch) {
const current = await getAdminUserById(userId)
if (!current) {
return null
}
const next = {
username: Object.prototype.hasOwnProperty.call(patch, 'username') ? patch.username : current.username,
password_hash: Object.prototype.hasOwnProperty.call(patch, 'password_hash') ? patch.password_hash : current.password_hash,
role: Object.prototype.hasOwnProperty.call(patch, 'role') ? patch.role : current.role,
status: Object.prototype.hasOwnProperty.call(patch, 'status') ? patch.status : current.status,
updated_at: Object.prototype.hasOwnProperty.call(patch, 'updated_at') ? patch.updated_at : current.updated_at,
}
getDb().prepare(`
UPDATE admin_users
SET username = ?, password_hash = ?, role = ?, status = ?, updated_at = ?
WHERE id = ?
`).run(
next.username,
next.password_hash,
next.role,
next.status,
next.updated_at,
userId,
const next = { ...current, ...patch }
const result = await query(
`
UPDATE admin_users
SET
username = $1,
password_hash = $2,
role = $3,
status = $4,
updated_at = $5
WHERE id = $6
RETURNING *
`,
[
next.username,
next.password_hash,
next.role,
next.status,
next.updated_at,
Number(userId),
],
)
return getAdminUserById(userId)
return result.rows[0] || null
}
export function countActiveAdminUsers() {
const row = getDb().prepare(`
SELECT COUNT(*) AS total
FROM admin_users
WHERE role = 'admin' AND status = 'active'
`).get()
export async function listAdminUsers({ page = 1, pageSize = 20, username = '', role = '', status = '' } = {}) {
const offset = (page - 1) * pageSize
const filters = []
const params = []
return Number(row?.total || 0)
if (username) {
params.push(`%${username}%`)
filters.push(`username ILIKE $${params.length}`)
}
if (role) {
params.push(role)
filters.push(`role = $${params.length}`)
}
if (status) {
params.push(status)
filters.push(`status = $${params.length}`)
}
const whereClause = filters.length > 0 ? `WHERE ${filters.join(' AND ')}` : ''
const totalResult = await query(`SELECT COUNT(*)::int AS total FROM admin_users ${whereClause}`, params)
params.push(pageSize)
params.push(offset)
const itemsResult = await query(
`
SELECT *
FROM admin_users
${whereClause}
ORDER BY id DESC
LIMIT $${params.length - 1} OFFSET $${params.length}
`,
params,
)
return {
items: itemsResult.rows,
total: Number(totalResult.rows[0]?.total || 0),
}
}
export async function countActiveAdminUsers() {
const result = await query(
`SELECT COUNT(*)::int AS total FROM admin_users WHERE role = 'admin' AND status = 'active'`,
)
return Number(result.rows[0]?.total || 0)
}