This commit is contained in:
Baobhan Sith
2025-04-20 15:23:58 +08:00
parent 1160f8a514
commit 77cd9272ba
31 changed files with 2781 additions and 2113 deletions
@@ -1,4 +1,5 @@
import { getDb } from '../database';
// packages/backend/src/repositories/quick-commands.repository.ts
import { getDbInstance, runDb, getDb as getDbRow, allDb } from '../database/connection'; // Import new async helpers
// 定义快捷指令的接口
export interface QuickCommand {
@@ -10,124 +11,118 @@ export interface QuickCommand {
updated_at: number; // Unix 时间戳 (秒)
}
// Define the expected row structure from the database if it matches QuickCommand
type DbQuickCommandRow = QuickCommand;
/**
* 添加一条新的快捷指令
* @param name - 指令名称 (可选)
* @param command - 指令内容
* @returns 返回插入记录的 ID
*/
export const addQuickCommand = (name: string | null, command: string): Promise<number> => {
const db = getDb();
export const addQuickCommand = async (name: string | null, command: string): Promise<number> => {
const sql = `INSERT INTO quick_commands (name, command, created_at, updated_at) VALUES (?, ?, strftime('%s', 'now'), strftime('%s', 'now'))`;
return new Promise((resolve, reject) => {
db.run(sql, [name, command], function (err) {
if (err) {
console.error('添加快捷指令时出错:', err);
return reject(new Error('无法添加快捷指令'));
}
resolve(this.lastID);
});
});
};
try {
const db = await getDbInstance();
const result = await runDb(db, sql, [name, command]);
// Ensure lastID is valid before returning
if (typeof result.lastID !== 'number' || result.lastID <= 0) {
throw new Error('添加快捷指令后未能获取有效的 lastID');
}
return result.lastID;
} catch (err: any) {
console.error('添加快捷指令时出错:', err.message);
throw new Error('无法添加快捷指令');
}
}; // End of addQuickCommand
/**
* 更新指定的快捷指令
* @param id - 要更新的记录 ID
* @param name - 新的指令名称 (可选)
* @param command - 新的指令内容
* @returns 返回更新的行数 (通常是 1 或 0)
* @returns 返回是否成功更新 (true/false)
*/
export const updateQuickCommand = (id: number, name: string | null, command: string): Promise<number> => {
const db = getDb();
export const updateQuickCommand = async (id: number, name: string | null, command: string): Promise<boolean> => {
const sql = `UPDATE quick_commands SET name = ?, command = ?, updated_at = strftime('%s', 'now') WHERE id = ?`;
return new Promise((resolve, reject) => {
db.run(sql, [name, command, id], function (err) {
if (err) {
console.error('更新快捷指令时出错:', err);
return reject(new Error('无法更新快捷指令'));
}
resolve(this.changes);
});
});
};
try {
const db = await getDbInstance();
const result = await runDb(db, sql, [name, command, id]);
return result.changes > 0;
} catch (err: any) {
console.error('更新快捷指令时出错:', err.message);
throw new Error('无法更新快捷指令');
}
}; // End of updateQuickCommand
/**
* 根据 ID 删除指定的快捷指令
* @param id - 要删除的记录 ID
* @returns 返回删除的行数 (通常是 1 或 0)
* @returns 返回是否成功删除 (true/false)
*/
export const deleteQuickCommand = (id: number): Promise<number> => {
const db = getDb();
export const deleteQuickCommand = async (id: number): Promise<boolean> => {
const sql = `DELETE FROM quick_commands WHERE id = ?`;
return new Promise((resolve, reject) => {
db.run(sql, [id], function (err) {
if (err) {
console.error('删除快捷指令时出错:', err);
return reject(new Error('无法删除快捷指令'));
}
resolve(this.changes);
});
});
};
try {
const db = await getDbInstance();
const result = await runDb(db, sql, [id]);
return result.changes > 0;
} catch (err: any) {
console.error('删除快捷指令时出错:', err.message);
throw new Error('无法删除快捷指令');
}
}; // End of deleteQuickCommand
/**
* 获取所有快捷指令
* @param sortBy - 排序字段 ('name' 或 'usage_count')
* @returns 返回包含所有快捷指令条目的数组
*/
export const getAllQuickCommands = (sortBy: 'name' | 'usage_count' = 'name'): Promise<QuickCommand[]> => {
const db = getDb();
export const getAllQuickCommands = async (sortBy: 'name' | 'usage_count' = 'name'): Promise<QuickCommand[]> => {
let orderByClause = 'ORDER BY name ASC'; // 默认按名称升序
if (sortBy === 'usage_count') {
orderByClause = 'ORDER BY usage_count DESC, name ASC'; // 按使用频率降序,同频率按名称升序
}
// SQLite 中 NULLS LAST/FIRST 的支持可能不一致,这里简单处理 NULL 名称排在前面
const sql = `SELECT id, name, command, usage_count, created_at, updated_at FROM quick_commands ${orderByClause}`;
return new Promise((resolve, reject) => {
db.all(sql, [], (err, rows: QuickCommand[]) => {
if (err) {
console.error('获取快捷指令时出错:', err);
return reject(new Error('无法获取快捷指令'));
}
resolve(rows);
});
});
};
try {
const db = await getDbInstance();
const rows = await allDb<DbQuickCommandRow>(db, sql);
return rows;
} catch (err: any) {
console.error('获取快捷指令时出错:', err.message);
throw new Error('无法获取快捷指令');
}
}; // End of getAllQuickCommands
/**
* 增加指定快捷指令的使用次数
* @param id - 要增加次数的记录 ID
* @returns 返回更新的行数 (通常是 1 或 0)
* @returns 返回是否成功更新 (true/false)
*/
export const incrementUsageCount = (id: number): Promise<number> => {
const db = getDb();
export const incrementUsageCount = async (id: number): Promise<boolean> => {
const sql = `UPDATE quick_commands SET usage_count = usage_count + 1, updated_at = strftime('%s', 'now') WHERE id = ?`;
return new Promise((resolve, reject) => {
db.run(sql, [id], function (err) {
if (err) {
console.error('增加快捷指令使用次数时出错:', err);
return reject(new Error('无法增加快捷指令使用次数'));
}
resolve(this.changes);
});
});
};
try {
const db = await getDbInstance();
const result = await runDb(db, sql, [id]);
return result.changes > 0;
} catch (err: any) {
console.error('增加快捷指令使用次数时出错:', err.message);
throw new Error('无法增加快捷指令使用次数');
}
}; // End of incrementUsageCount
/**
* 根据 ID 查找快捷指令 (用于编辑前获取数据)
* @param id - 要查找的记录 ID
* @returns 返回找到的快捷指令条目,如果未找到则返回 undefined
*/
export const findQuickCommandById = (id: number): Promise<QuickCommand | undefined> => {
const db = getDb();
export const findQuickCommandById = async (id: number): Promise<QuickCommand | undefined> => {
const sql = `SELECT id, name, command, usage_count, created_at, updated_at FROM quick_commands WHERE id = ?`;
return new Promise((resolve, reject) => {
db.get(sql, [id], (err, row: QuickCommand | undefined) => {
if (err) {
console.error('查找快捷指令时出错:', err);
return reject(new Error('无法查找快捷指令'));
}
resolve(row);
});
});
};
try {
const db = await getDbInstance();
const row = await getDbRow<DbQuickCommandRow>(db, sql, [id]);
return row; // Returns undefined if not found
} catch (err: any) {
console.error('查找快捷指令时出错:', err.message);
throw new Error('无法查找快捷指令');
}
}; // End of findQuickCommandById