Skip to content

Commit 024dc73

Browse files
committed
fix: resolve multiple 500 errors (#104, #105, #106)
- #104: Add update_chapter_ranges() to StoryNodeRepository - #105: Fix create_template() database operation in PromptManager - #106: Fix update_config() database operation in EmbeddingConfigService All fixes ensure proper connection handling and parameter types.
1 parent 593678c commit 024dc73

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

application/ai/embedding_config_service.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ def update_config(self, **kwargs) -> EmbeddingConfigModel:
157157
params.append("default") # WHERE id = ?
158158

159159
sql = f"UPDATE embedding_config SET {', '.join(set_clauses)} WHERE id = ?"
160-
db.execute(sql, params)
161-
db.get_connection().commit()
160+
conn = db.get_connection()
161+
conn.execute(sql, tuple(params))
162+
conn.commit()
162163

163164
logger.info("EmbeddingConfigService: 配置已更新,字段: %s", list(kwargs.keys()))
164165
return self.get_config()

infrastructure/ai/prompt_manager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,14 @@ def create_template(self, name: str, description: str = "",
425425
db = self._get_db()
426426
tid = _uid()
427427
now = datetime.now().isoformat()
428-
db.execute("""
428+
conn = db.get_connection()
429+
conn.execute("""
429430
INSERT INTO prompt_templates
430431
(id, name, description, category, version, author, icon, color,
431432
is_builtin, metadata, created_at, updated_at)
432433
VALUES (?, ?, ?, ?, '1.0.0', '', '📦', '#6b7280', 0, '{}', ?, ?)
433434
""", (tid, name, description, category, now, now))
434-
db.commit()
435+
conn.commit()
435436
return TemplateInfo({"id": tid, "name": name, "description": description,
436437
"category": category, "node_count": 0})
437438

infrastructure/persistence/database/story_node_repository.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,43 @@ def _row_to_entity(self, row: sqlite3.Row) -> StoryNode:
417417
created_at=datetime.fromisoformat(row_dict["created_at"]),
418418
updated_at=datetime.fromisoformat(row_dict["updated_at"]),
419419
)
420+
421+
async def update_chapter_ranges(self, novel_id: str) -> None:
422+
"""根据子节点的 chapter_start/chapter_end 更新父节点的章节范围"""
423+
conn = self._get_connection()
424+
try:
425+
cursor = conn.cursor()
426+
cursor.execute("""
427+
SELECT id, parent_id, chapter_start, chapter_end, node_type
428+
FROM story_nodes WHERE novel_id = ?
429+
ORDER BY order_index
430+
""", (novel_id,))
431+
rows = cursor.fetchall()
432+
433+
nodes_by_parent = {}
434+
for row in rows:
435+
pid = row[1]
436+
if pid not in nodes_by_parent:
437+
nodes_by_parent[pid] = []
438+
nodes_by_parent[pid].append(row)
439+
440+
for parent_id, children in nodes_by_parent.items():
441+
if not children:
442+
continue
443+
starts = [r[2] for r in children if r[2] is not None]
444+
ends = [r[3] for r in children if r[3] is not None]
445+
if starts and ends:
446+
new_start = min(starts)
447+
new_end = max(ends)
448+
cursor.execute("""
449+
UPDATE story_nodes
450+
SET chapter_start = ?, chapter_end = ?, updated_at = ?
451+
WHERE id = ?
452+
""", (new_start, new_end, datetime.now().isoformat(), parent_id))
453+
454+
conn.commit()
455+
except Exception as e:
456+
conn.rollback()
457+
raise e
458+
finally:
459+
conn.close()

0 commit comments

Comments
 (0)