update template name -> command name

This commit is contained in:
duanfuxiang
2025-04-15 23:24:35 +08:00
parent 43599fca47
commit bde7df8b77
20 changed files with 622 additions and 225 deletions

View File

@@ -0,0 +1,67 @@
import fuzzysort from 'fuzzysort'
import { App } from 'obsidian'
import { DBManager } from '../../database-manager'
import { DuplicateTemplateException } from '../../exception'
import { InsertTemplate, SelectTemplate, UpdateTemplate } from '../../schema'
import { CommandRepository } from './command-repository'
export class CommandManager {
private app: App
private repository: CommandRepository
private dbManager: DBManager
constructor(app: App, dbManager: DBManager) {
this.app = app
this.dbManager = dbManager
this.repository = new CommandRepository(app, dbManager.getPgClient())
}
async createCommand(command: InsertTemplate): Promise<SelectTemplate> {
const existingTemplate = await this.repository.findByName(command.name)
if (existingTemplate) {
throw new DuplicateTemplateException(command.name)
}
const created = await this.repository.create(command)
return created
}
async updateCommand(id: string, template: UpdateTemplate): Promise<SelectTemplate> {
const updated = await this.repository.update(id, template)
return updated
}
async findAllCommands(): Promise<SelectTemplate[]> {
return await this.repository.findAll()
}
getAllCommands(callback: (templates: SelectTemplate[]) => void): void {
const db = this.dbManager.getPgClient()
db?.live.query('SELECT * FROM template ORDER BY updated_at DESC', [], (results: { rows: Array<SelectTemplate> }) => {
callback(results.rows.map(row => ({
id: row.id,
name: row.name,
content: row.content,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
})))
})
}
async searchCommands(query: string): Promise<SelectTemplate[]> {
const templates = await this.findAllCommands()
const results = fuzzysort.go(query, templates, {
keys: ['name'],
threshold: 0.2,
limit: 20,
all: true,
})
return results.map((result) => result.obj)
}
async deleteCommand(id: string): Promise<boolean> {
const deleted = await this.repository.delete(id)
return deleted
}
}

View File

@@ -2,9 +2,9 @@ import { PGliteInterface } from '@electric-sql/pglite'
import { App } from 'obsidian'
import { DatabaseNotInitializedException } from '../../exception'
import { type InsertTemplate, type SelectTemplate } from '../../schema'
import { type InsertTemplate, type SelectTemplate, type UpdateTemplate } from '../../schema'
export class TemplateRepository {
export class CommandRepository {
private app: App
private db: PGliteInterface | null
@@ -13,7 +13,7 @@ export class TemplateRepository {
this.db = pgClient
}
async create(template: InsertTemplate): Promise<SelectTemplate> {
async create(command: InsertTemplate): Promise<SelectTemplate> {
if (!this.db) {
throw new DatabaseNotInitializedException()
}
@@ -22,7 +22,7 @@ export class TemplateRepository {
`INSERT INTO "template" (name, content)
VALUES ($1, $2)
RETURNING *`,
[template.name, template.content]
[command.name, command.content]
)
return result.rows[0]
}
@@ -31,7 +31,7 @@ export class TemplateRepository {
if (!this.db) {
throw new DatabaseNotInitializedException()
}
const result = await this.db.query<SelectTemplate>(
const result = await this.db.liveQuery<SelectTemplate>(
`SELECT * FROM "template"`
)
return result.rows
@@ -50,7 +50,7 @@ export class TemplateRepository {
async update(
id: string,
template: Partial<InsertTemplate>,
command: UpdateTemplate,
): Promise<SelectTemplate | null> {
if (!this.db) {
throw new DatabaseNotInitializedException()
@@ -60,15 +60,15 @@ export class TemplateRepository {
const params: any[] = []
let paramIndex = 1
if (template.name !== undefined) {
if (command.name !== undefined) {
setClauses.push(`name = $${paramIndex}`)
params.push(template.name)
params.push(command.name)
paramIndex++
}
if (template.content !== undefined) {
if (command.content !== undefined) {
setClauses.push(`content = $${paramIndex}`)
params.push(template.content)
params.push(command.content)
paramIndex++
}

View File

@@ -1,49 +0,0 @@
import fuzzysort from 'fuzzysort'
import { App } from 'obsidian'
import { DBManager } from '../../database-manager'
import { DuplicateTemplateException } from '../../exception'
import { InsertTemplate, SelectTemplate } from '../../schema'
import { TemplateRepository } from './template-repository'
export class TemplateManager {
private app: App
private repository: TemplateRepository
private dbManager: DBManager
constructor(app: App, dbManager: DBManager) {
this.app = app
this.dbManager = dbManager
this.repository = new TemplateRepository(app, dbManager.getPgClient())
}
async createTemplate(template: InsertTemplate): Promise<SelectTemplate> {
const existingTemplate = await this.repository.findByName(template.name)
if (existingTemplate) {
throw new DuplicateTemplateException(template.name)
}
const created = await this.repository.create(template)
return created
}
async findAllTemplates(): Promise<SelectTemplate[]> {
return await this.repository.findAll()
}
async searchTemplates(query: string): Promise<SelectTemplate[]> {
const templates = await this.findAllTemplates()
const results = fuzzysort.go(query, templates, {
keys: ['name'],
threshold: 0.2,
limit: 20,
all: true,
})
return results.map((result) => result.obj)
}
async deleteTemplate(id: string): Promise<boolean> {
const deleted = await this.repository.delete(id)
return deleted
}
}