From 369ebd8fd0243d9aaac4154422b5c9f7dff4a2ed Mon Sep 17 00:00:00 2001 From: r4bb1t Date: Fri, 16 Feb 2024 04:17:46 +0900 Subject: [PATCH] fix: summary bug --- src/routers/question.py | 7 ++++++- src/routers/result.py | 23 ++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/routers/question.py b/src/routers/question.py index 1ab763b..b7bbb7f 100644 --- a/src/routers/question.py +++ b/src/routers/question.py @@ -107,10 +107,15 @@ async def skip_question(id: int, user=Depends(get_current_user), db: Session = D contexts = make_context(parent=question, db=db, user_id=user.user_id) print(contexts) + + root = question + while root.parents_id != -1: + root = db.query(LQuestions).filter(LQuestions.question_id == root.parents_id).first() + if (len(contexts) > 0): contents = generate_summary(contexts) print(contents) - db.add(LSummary(user_id=user.user_id, question_id=id, content=contents[1]['text'], chapter_id=question.chapter_id)) + db.add(LSummary(user_id=user.user_id, question_id=root.question_id, content=contents[1]['text'], chapter_id=question.chapter_id)) db.commit() return { diff --git a/src/routers/result.py b/src/routers/result.py index 47d424d..ddfed86 100644 --- a/src/routers/result.py +++ b/src/routers/result.py @@ -6,12 +6,29 @@ from routers.auth import get_current_user from database import get_db -from models import LSummary +from models import LSummary, LChapter, LQuestions router = APIRouter(responses={404: {"description": "Not found"}}) @router.get("", summary="모든 요약본 가져오기") async def get_chapters(user=Depends(get_current_user), db: Session = Depends(get_db)): - result = db.query(LSummary).all() - print(result) + summary_list = db.query(LSummary).filter(LSummary.user_id == user.user_id).all() + result = [] + chapters = db.query(LChapter).all() + for chapter in chapters: + summary = list(filter(lambda x: x.chapter_id == chapter.chapter_id, summary_list)) + if summary: + summaries = [] + for s in summary: + summaries.append({ + "summary_id": s.summary_id, + "question": db.query(LQuestions).filter(LQuestions.question_id == s.question_id).first().content, + "content": s.content, + }) + result.append({ + "chapter_id": chapter.chapter_id, + "chapter_title": chapter.title, + "summaries": summaries + }) + result = list(filter(lambda x: x["summaries"], result)) return result \ No newline at end of file