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 48c4f1b..babfba8 100644 --- a/src/routers/auth.py +++ b/src/routers/auth.py @@ -7,7 +7,6 @@ from datetime import datetime, timedelta from fastapi.security import HTTPBearer - from jose import jwt, JWTError SECRET_KEY = "secretkey" 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