This commit is contained in:
urieljarethbusiness-cpu
2026-05-19 14:21:56 -06:00
parent 4f3bc01dcb
commit 7361f7ced1
6 changed files with 133 additions and 11 deletions
+9 -1
View File
@@ -8,7 +8,7 @@ from fastapi.responses import FileResponse, PlainTextResponse
from backend.app.config import settings
from backend.app.managers.knowledge_manager import DuplicateSourceError, KnowledgeManager
from backend.app.models.schemas import MarkdownUpdate, SubjectCreate, WeekCreate, WeekUpdate
from backend.app.models.schemas import MarkdownUpdate, SubjectCreate, SubjectUpdate, WeekCreate, WeekUpdate
from backend.app.services.ingestion_service import IngestionService
app = FastAPI(title="Knowledge Station", version="0.1.0")
@@ -60,6 +60,14 @@ def get_subject(subject_id: int) -> dict:
raise HTTPException(status_code=404, detail=str(exc)) from exc
@app.put("/subjects/{subject_id}")
def update_subject(subject_id: int, payload: SubjectUpdate) -> dict:
try:
return manager.update_subject(subject_id, payload.name)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
@app.delete("/subjects/{subject_id}")
def delete_subject(subject_id: int) -> dict:
try:
+26
View File
@@ -154,6 +154,14 @@ class KnowledgeManager:
raise ValueError("Materia no encontrada")
return dict(row)
def update_subject(self, subject_id: int, name: str) -> dict[str, Any]:
if not name.strip():
raise ValueError("El nombre de la materia no puede estar vacio")
self.get_subject(subject_id)
with self._connect() as conn:
conn.execute("update subjects set name = ? where id = ?", (name.strip(), subject_id))
return self.get_subject(subject_id)
def delete_subject(self, subject_id: int) -> None:
subject = self.get_subject(subject_id)
with self._connect() as conn:
@@ -224,6 +232,8 @@ class KnowledgeManager:
def update_week(self, subject_id: int, current_number: int, new_number: int, title: str | None = None) -> dict[str, Any]:
subject = self.get_subject(subject_id)
week = self.get_week(subject_id, current_number)
old_dir: Path | None = None
new_dir: Path | None = None
if new_number != current_number:
with self._connect() as conn:
existing = conn.execute(
@@ -248,6 +258,22 @@ class KnowledgeManager:
"update weeks set number = ?, title = ? where id = ?",
(new_number, title, week["id"]),
)
if old_dir and new_dir:
old_prefix = str(old_dir)
new_prefix = str(new_dir)
conn.execute(
"update sources set stored_path = replace(stored_path, ?, ?) where week_id = ? and stored_path like ?",
(old_prefix, new_prefix, week["id"], f"{old_prefix}%"),
)
conn.execute(
"""
update documents
set markdown_path = replace(markdown_path, ?, ?)
where source_id in (select id from sources where week_id = ?)
and markdown_path like ?
""",
(old_prefix, new_prefix, week["id"], f"{old_prefix}%"),
)
return self.get_week(subject_id, new_number)
def week_dir(self, subject_slug: str, week_number: int) -> Path:
+4
View File
@@ -8,6 +8,10 @@ class SubjectCreate(BaseModel):
name: str = Field(min_length=1)
class SubjectUpdate(BaseModel):
name: str = Field(min_length=1)
class Subject(BaseModel):
id: int
name: str