118 lines
2.9 KiB
JavaScript
118 lines
2.9 KiB
JavaScript
import { getDb } from '../db/client.js'
|
|
|
|
export function getAdminUserByUsername(username) {
|
|
return getDb().prepare('SELECT * FROM admin_users WHERE username = ? LIMIT 1').get(username) || null
|
|
}
|
|
|
|
export function getAdminUserById(userId) {
|
|
return getDb().prepare('SELECT * FROM admin_users WHERE id = ? LIMIT 1').get(userId) || 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,
|
|
)
|
|
|
|
return getAdminUserById(Number(result.lastInsertRowid))
|
|
}
|
|
|
|
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)
|
|
|
|
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,
|
|
)
|
|
|
|
return getAdminUserById(userId)
|
|
}
|
|
|
|
export function countActiveAdminUsers() {
|
|
const row = getDb().prepare(`
|
|
SELECT COUNT(*) AS total
|
|
FROM admin_users
|
|
WHERE role = 'admin' AND status = 'active'
|
|
`).get()
|
|
|
|
return Number(row?.total || 0)
|
|
}
|