From b38cf61aafa4425d7352f91c612399704c9e542c Mon Sep 17 00:00:00 2001 From: mjkweon17 Date: Thu, 15 Feb 2024 17:25:15 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Chapter=20=ED=85=8C=EC=9D=B4=EB=B8=94?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/create_tables.sql | 10 +++++++++- src/models.py | 7 +++++++ src/routers/auth.py | 3 ++- src/routers/question.py | 13 ++++++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/sql/create_tables.sql b/sql/create_tables.sql index b0610ff..d4ddf53 100644 --- a/sql/create_tables.sql +++ b/sql/create_tables.sql @@ -8,6 +8,13 @@ CREATE TABLE `LUsers` ( PRIMARY KEY (`user_id`) ); +CREATE TABLE `LChapter` ( + `chapter_id` INT AUTO_INCREMENT NOT NULL, + `title` VARCHAR(255) NOT NULL, + PRIMARY KEY (`chapter_id`) +); +) + CREATE TABLE `LQuestions` ( `question_id` INT AUTO_INCREMENT NOT NULL, `is_fixed` BOOL NOT NULL DEFAULT FALSE, @@ -16,7 +23,8 @@ CREATE TABLE `LQuestions` ( `content` TEXT NOT NULL, `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), PRIMARY KEY (`question_id`), - FOREIGN KEY (`user_id`) REFERENCES `LUsers`(`user_id`) + FOREIGN KEY (`user_id`) REFERENCES `LUsers`(`user_id`), + FOREIGN kEY (`chapter_id`) REFERENCES `LChapter`(`chapter_id` ); CREATE TABLE `LAnswers` ( diff --git a/src/models.py b/src/models.py index 0dcc784..dd14045 100644 --- a/src/models.py +++ b/src/models.py @@ -15,16 +15,23 @@ class LUsers(Base): created_at = Column(DATETIME(fsp=3), nullable=False, server_default=text("CURRENT_TIMESTAMP(3)")) kakao_id = Column(BIGINT, nullable=True) +class LChapter(Base): + __tablename__ = 'LChapter' + chapter_id = Column(Integer, primary_key=True, autoincrement=True) + title = Column(String(255), nullable=False) + class LQuestions(Base): __tablename__ = 'LQuestions' question_id = Column(Integer, primary_key=True, autoincrement=True) is_fixed = Column(Boolean, nullable=False, default=False) + chatper_id = Column(Integer, ForeignKey('LChapter.chapter_id', onupdate='Cascade'), nullable=False) user_id = Column(Integer, ForeignKey('LUsers.user_id', onupdate='Cascade'), nullable=False) parents_id = Column(Integer, nullable=False) content = Column(Text, nullable=False) created_at = Column(DATETIME(fsp=3), nullable=False, server_default=text("CURRENT_TIMESTAMP(3)")) LUsers = relationship('LUsers') + LChapter = relationship('LChapter') class LAnswers(Base): __tablename__ = 'LAnswers' diff --git a/src/routers/auth.py b/src/routers/auth.py index 288e908..dba30e2 100644 --- a/src/routers/auth.py +++ b/src/routers/auth.py @@ -6,7 +6,6 @@ from models import LUsers from datetime import datetime, timedelta - from jose import jwt, JWTError SECRET_KEY = "secretkey" @@ -23,7 +22,9 @@ async def get_current_user(token: str = Header(None), db: Session = Depends(get_ if token is None: raise HTTPException(status_code=401, detail="토큰이 필요합니다.") try: + print("test1") payload = jwt.decode(token, SECRET_KEY, algorithms=ALGORITHM) + print("test2") print(user_id) print(user_id) print(user_id) diff --git a/src/routers/question.py b/src/routers/question.py index 6d22dcf..72f9c43 100644 --- a/src/routers/question.py +++ b/src/routers/question.py @@ -4,6 +4,8 @@ from database import get_db from models import LQuestions +from auth import get_current_user + router = APIRouter() @router.get("/") @@ -12,4 +14,13 @@ async def get_question(id: int, db: Session = Depends(get_db)): if question is None: raise HTTPException(status_code=404, detail="Question not found") - return {'id': question.question_id, 'content': question.content} \ No newline at end of file + return {'id': question.question_id, 'content': question.content} + +# 마지막으로 답변한 question_id 가져오기 +# request: user_id(access_token, ge +@router.get("/last") +async def get_last_question(user=Depends(get_current_user), db: Session = Depends(get_db)): + question = db.query(LQuestions).filter(LQuestions.user_id == user.user_id).order_by(LQuestions.question_id.desc()).first() + if question is None: + return {'question_id': 1} + return {'question_id': question.question_id, 'content': question.content} \ No newline at end of file