feat: 添加文章修改保存功能及首页入口
This commit is contained in:
78
server/db.js
78
server/db.js
@@ -233,6 +233,29 @@ const seedDefaultReferences = async () => {
|
||||
|
||||
await seedDefaultReferences();
|
||||
|
||||
/**
|
||||
* 同步现有的提纲素材到通用素材库
|
||||
*/
|
||||
const syncExistingOutlineMaterials = () => {
|
||||
try {
|
||||
const materials = db.prepare('SELECT * FROM outline_materials').all();
|
||||
if (materials.length === 0) return;
|
||||
|
||||
console.log(`🔍 正在同步 ${materials.length} 个存量提纲素材到通用素材库...`);
|
||||
db.transaction(() => {
|
||||
materials.forEach(mat => {
|
||||
syncToReferences(mat.id, mat.name, mat.content);
|
||||
});
|
||||
})();
|
||||
|
||||
console.log(`✅ 存量提纲素材同步完成`);
|
||||
} catch (err) {
|
||||
console.error('⚠️ 同步存量素材失败:', err.message);
|
||||
}
|
||||
};
|
||||
|
||||
syncExistingOutlineMaterials();
|
||||
|
||||
console.log('📦 SQLite 数据库初始化完成:', DB_PATH);
|
||||
|
||||
export function getAllReferences() {
|
||||
@@ -618,6 +641,32 @@ export function getAllOutlineMaterials() {
|
||||
return db.prepare('SELECT * FROM outline_materials ORDER BY created_at DESC').all();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将提纲素材同步到通用素材库 (materials/excerpts)
|
||||
* 提纲素材作为 'outline' 类型存储,便于在通用素材库中管理
|
||||
*/
|
||||
const syncToReferences = (id, name, content) => {
|
||||
const refId = `ref-${id}`; // 使用固定前缀关联
|
||||
|
||||
// 1. 更新或插入到 materials 表
|
||||
db.prepare(`
|
||||
INSERT INTO materials (id, type, title, source, is_default, created_at, updated_at)
|
||||
VALUES (?, 'outline', ?, '提纲写作', 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
title = EXCLUDED.title,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
`).run(refId, name);
|
||||
|
||||
// 2. 更新或插入到 reference_excerpts 表
|
||||
const excerptId = `${refId}-content`;
|
||||
db.prepare(`
|
||||
INSERT INTO reference_excerpts (id, reference_id, topic, content)
|
||||
VALUES (?, ?, '正文', ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
content = EXCLUDED.content
|
||||
`).run(excerptId, refId, content);
|
||||
};
|
||||
|
||||
export function addOutlineMaterial(material) {
|
||||
const id = material.id || `omat-${Date.now()}`;
|
||||
db.prepare(`
|
||||
@@ -629,6 +678,14 @@ export function addOutlineMaterial(material) {
|
||||
material.content,
|
||||
material.content?.length || 0
|
||||
);
|
||||
|
||||
// 同步到通用素材库
|
||||
try {
|
||||
syncToReferences(id, material.name, material.content);
|
||||
} catch (err) {
|
||||
console.error('同步素材到通用库失败:', err);
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -651,11 +708,32 @@ export function updateOutlineMaterial(id, updates) {
|
||||
params.push(id);
|
||||
|
||||
db.prepare(`UPDATE outline_materials SET ${setClauses.join(', ')} WHERE id = ?`).run(...params);
|
||||
|
||||
// 如果更新了名称或内容,需要同步
|
||||
try {
|
||||
const current = db.prepare('SELECT * FROM outline_materials WHERE id = ?').get(id);
|
||||
if (current) {
|
||||
syncToReferences(id, current.name, current.content);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('更新素材同步失败:', err);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export function deleteOutlineMaterial(id) {
|
||||
db.prepare('DELETE FROM outline_materials WHERE id = ?').run(id);
|
||||
|
||||
// 同时从通用库删除 (如果存在)
|
||||
try {
|
||||
const refId = `ref-${id}`;
|
||||
db.prepare('DELETE FROM reference_excerpts WHERE reference_id = ?').run(refId);
|
||||
db.prepare('DELETE FROM materials WHERE id = ?').run(refId);
|
||||
} catch (err) {
|
||||
console.error('删除素材同步失败:', err);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user