Skip to content

Commit

Permalink
feat: #52 created missing repositories
Browse files Browse the repository at this point in the history
- update also endpoints

- change api welcome message
  • Loading branch information
miguelcsx committed Sep 10, 2024
1 parent c54424b commit 9f12de8
Show file tree
Hide file tree
Showing 17 changed files with 484 additions and 77 deletions.
65 changes: 65 additions & 0 deletions app/api/endpoints/activity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# app/api/endpoints/activity.py

from fastapi import (
APIRouter,
Depends,
HTTPException,
)
from sqlalchemy.orm import Session
from app.database.session import get_db
from app.schemas.activity import (
ActivityCreate,
ActivityUpdate,
ActivityInDBBase,
)
from app.repositories.activity_repository import (
create_activity,
get_activity,
get_activities,
update_activity,
delete_activity,
)

router = APIRouter()


@router.post("/activities/", response_model=ActivityInDBBase)
def create_activity_endpoint(
activity: ActivityCreate, db: Session = Depends(get_db)
) -> ActivityInDBBase:
return create_activity(db, activity)


@router.get("/activities/{activity_id}", response_model=ActivityInDBBase)
def read_activity(activity_id: int, db: Session = Depends(get_db)) -> ActivityInDBBase:
activity = get_activity(db, activity_id)
if activity is None:
raise HTTPException(status_code=404, detail="Activity not found")
return activity


@router.get("/activities/", response_model=list[ActivityInDBBase])
def read_activities(
skip: int = 0, limit: int = 10, db: Session = Depends(get_db)
) -> list[ActivityInDBBase]:
return get_activities(db, skip, limit)


@router.put("/activities/{activity_id}", response_model=ActivityInDBBase)
def update_activity_endpoint(
activity_id: int, activity: ActivityUpdate, db: Session = Depends(get_db)
) -> ActivityInDBBase:
db_activity = get_activity(db, activity_id)
if db_activity is None:
raise HTTPException(status_code=404, detail="Activity not found")
return update_activity(db, activity_id, activity.model_dump())


@router.delete("/activities/{activity_id}", response_model=ActivityInDBBase)
def delete_activity_endpoint(
activity_id: int, db: Session = Depends(get_db)
) -> ActivityInDBBase:
activity = delete_activity(db, activity_id)
if activity is None:
raise HTTPException(status_code=404, detail="Activity not found")
return activity
65 changes: 65 additions & 0 deletions app/api/endpoints/answer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# app/api/endpoints/answer.py

from fastapi import (
APIRouter,
Depends,
HTTPException,
)
from sqlalchemy.orm import Session
from app.database.session import get_db
from app.schemas.answer import (
AnswerCreate,
AnswerUpdate,
AnswerInDBBase,
)
from app.repositories.answer_repository import (
create_answer,
get_answer,
get_answers,
update_answer,
delete_answer,
)

router = APIRouter()


@router.post("/answers/", response_model=AnswerInDBBase)
def create_answer_endpoint(
answer: AnswerCreate, db: Session = Depends(get_db)
) -> AnswerInDBBase:
return create_answer(db, answer)


@router.get("/answers/{answer_id}", response_model=AnswerInDBBase)
def read_answer(answer_id: int, db: Session = Depends(get_db)) -> AnswerInDBBase:
answer = get_answer(db, answer_id)
if answer is None:
raise HTTPException(status_code=404, detail="Answer not found")
return answer


@router.get("/answers/", response_model=list[AnswerInDBBase])
def read_answers(
skip: int = 0, limit: int = 10, db: Session = Depends(get_db)
) -> list[AnswerInDBBase]:
return get_answers(db, skip, limit)


@router.put("/answers/{answer_id}", response_model=AnswerInDBBase)
def update_answer_endpoint(
answer_id: int, answer: AnswerUpdate, db: Session = Depends(get_db)
) -> AnswerInDBBase:
db_answer = get_answer(db, answer_id)
if db_answer is None:
raise HTTPException(status_code=404, detail="Answer not found")
return update_answer(db, answer_id, answer.model_dump())


@router.delete("/answers/{answer_id}", response_model=AnswerInDBBase)
def delete_answer_endpoint(
answer_id: int, db: Session = Depends(get_db)
) -> AnswerInDBBase:
answer = delete_answer(db, answer_id)
if answer is None:
raise HTTPException(status_code=404, detail="Answer not found")
return answer
65 changes: 65 additions & 0 deletions app/api/endpoints/question.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# app/api/endpoints/question.py

from fastapi import (
APIRouter,
Depends,
HTTPException,
)
from sqlalchemy.orm import Session
from app.database.session import get_db
from app.schemas.question import (
QuestionCreate,
QuestionUpdate,
QuestionInDBBase,
)
from app.repositories.question_repository import (
create_question,
get_question,
get_questions,
update_question,
delete_question,
)

router = APIRouter()


@router.post("/questions/", response_model=QuestionInDBBase)
def create_question_endpoint(
question: QuestionCreate, db: Session = Depends(get_db)
) -> QuestionInDBBase:
return create_question(db, question)


@router.get("/questions/{question_id}", response_model=QuestionInDBBase)
def read_question(question_id: int, db: Session = Depends(get_db)) -> QuestionInDBBase:
question = get_question(db, question_id)
if question is None:
raise HTTPException(status_code=404, detail="Question not found")
return question


@router.get("/questions/", response_model=list[QuestionInDBBase])
def read_questions(
skip: int = 0, limit: int = 10, db: Session = Depends(get_db)
) -> list[QuestionInDBBase]:
return get_questions(db, skip, limit)


@router.put("/questions/{question_id}", response_model=QuestionInDBBase)
def update_question_endpoint(
question_id: int, question: QuestionUpdate, db: Session = Depends(get_db)
) -> QuestionInDBBase:
db_question = get_question(db, question_id)
if db_question is None:
raise HTTPException(status_code=404, detail="Question not found")
return update_question(db, question_id, question.model_dump())


@router.delete("/questions/{question_id}", response_model=QuestionInDBBase)
def delete_question_endpoint(
question_id: int, db: Session = Depends(get_db)
) -> QuestionInDBBase:
question = delete_question(db, question_id)
if question is None:
raise HTTPException(status_code=404, detail="Question not found")
return question
71 changes: 71 additions & 0 deletions app/api/endpoints/study_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# app/api/endpoints/study_session.py

from fastapi import (
APIRouter,
Depends,
HTTPException,
)
from sqlalchemy.orm import Session
from app.database.session import get_db
from app.schemas.study_session import (
StudySessionCreate,
StudySessionUpdate,
StudySessionInDBBase,
)
from app.repositories.study_session_repository import (
create_study_session,
get_study_session,
get_study_sessions,
update_study_session,
delete_study_session,
)

router = APIRouter()


@router.post("/study_sessions/", response_model=StudySessionInDBBase)
def create_study_session_endpoint(
study_session: StudySessionCreate, db: Session = Depends(get_db)
) -> StudySessionInDBBase:
return create_study_session(db, study_session)


@router.get("/study_sessions/{study_session_id}", response_model=StudySessionInDBBase)
def read_study_session(
study_session_id: int, db: Session = Depends(get_db)
) -> StudySessionInDBBase:
study_session = get_study_session(db, study_session_id)
if study_session is None:
raise HTTPException(status_code=404, detail="StudySession not found")
return study_session


@router.get("/study_sessions/", response_model=list[StudySessionInDBBase])
def read_study_sessions(
skip: int = 0, limit: int = 10, db: Session = Depends(get_db)
) -> list[StudySessionInDBBase]:
return get_study_sessions(db, skip, limit)


@router.put("/study_sessions/{study_session_id}", response_model=StudySessionInDBBase)
def update_study_session_endpoint(
study_session_id: int,
study_session: StudySessionUpdate,
db: Session = Depends(get_db),
) -> StudySessionInDBBase:
db_study_session = get_study_session(db, study_session_id)
if db_study_session is None:
raise HTTPException(status_code=404, detail="StudySession not found")
return update_study_session(db, study_session_id, study_session.model_dump())


@router.delete(
"/study_sessions/{study_session_id}", response_model=StudySessionInDBBase
)
def delete_study_session_endpoint(
study_session_id: int, db: Session = Depends(get_db)
) -> StudySessionInDBBase:
study_session = delete_study_session(db, study_session_id)
if study_session is None:
raise HTTPException(status_code=404, detail="StudySession not found")
return study_session
61 changes: 29 additions & 32 deletions app/database/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# app/database/models.py

from enum import Enum
from sqlalchemy import (
Column,
Integer,
String,
ForeignKey,
Enum as SQLEnum,
Text,
DateTime,
)
from sqlalchemy.orm import relationship
from app.database.session import Base
Expand All @@ -18,12 +19,12 @@ class ActivityType(Enum):
class User(Base):
__tablename__ = "users"

id: Column = Column(Integer, primary_key=True, index=True)
username: Column = Column(String, unique=True, index=True)
email: Column = Column(String, unique=True, index=True)
role: Column = Column(String)
created_at = Column(String)
updated_at = Column(String)
id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True, index=True)
email = Column(String, unique=True, index=True)
role = Column(String)
created_at = Column(DateTime)
updated_at = Column(DateTime)

study_sessions = relationship("StudySession", back_populates="user")
activities = relationship("Activity", back_populates="user")
Expand All @@ -34,10 +35,10 @@ class User(Base):
class StudySession(Base):
__tablename__ = "study_sessions"

id: Column = Column(Integer, primary_key=True, index=True)
user_id: Column = Column(Integer, ForeignKey("users.id"))
start_time: Column = Column(String)
end_time: Column = Column(String)
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id"))
start_time = Column(DateTime)
end_time = Column(DateTime)

user = relationship("User", back_populates="study_sessions")
activities = relationship("Activity", back_populates="study_session")
Expand All @@ -46,12 +47,12 @@ class StudySession(Base):
class Activity(Base):
__tablename__ = "activities"

id: Column = Column(Integer, primary_key=True, index=True)
user_id: Column = Column(Integer, ForeignKey("users.id"))
study_session_id: Column = Column(Integer, ForeignKey("study_sessions.id"))
type: Column = Column(Enum(ActivityType), nullable=False)
activity: Column = Column(String)
created_at = Column(String)
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id"))
study_session_id = Column(Integer, ForeignKey("study_sessions.id"))
type = Column(SQLEnum(ActivityType), nullable=False)
activity = Column(String)
created_at = Column(DateTime)

user = relationship("User", back_populates="activities")
study_session = relationship("StudySession", back_populates="activities")
Expand All @@ -62,10 +63,10 @@ class Activity(Base):
class Question(Base):
__tablename__ = "questions"

id: Column = Column(Integer, primary_key=True, index=True)
user_id: Column = Column(Integer, ForeignKey("users.id"))
activity_id: Column = Column(Integer, ForeignKey("activities.id"))
question: Column = Column(String)
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id"))
activity_id = Column(Integer, ForeignKey("activities.id"))
question = Column(String)

user = relationship("User", back_populates="questions")
answers = relationship("Answer", back_populates="question")
Expand All @@ -75,16 +76,12 @@ class Question(Base):
class Answer(Base):
__tablename__ = "answers"

id: Column = Column(Integer, primary_key=True, index=True)
user_id: Column = Column(Integer, ForeignKey("users.id"))
question_id: Column = Column(Integer, ForeignKey("questions.id"))
answer: Column = Column(String)
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id"))
question_id = Column(Integer, ForeignKey("questions.id"))
answer = Column(Text, nullable=False)
parent_answer_id = Column(Integer, ForeignKey("answers.id"))

user = relationship("User", back_populates="answers")
question = relationship("Question", back_populates="answers")
parent_answer = relationship(
"Answer", remote_side=[id], back_populates="child_answers"
)
child_answers = relationship(
"Answer", back_populates="parent_answer", cascade="all, delete-orphan"
)
parent_answer = relationship("Answer", remote_side=[id], backref="child_answers")
3 changes: 1 addition & 2 deletions app/database/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Generator
from sqlalchemy import (
create_engine,
Engine,
)
from sqlalchemy.orm import (
declarative_base,
Expand All @@ -14,7 +13,7 @@
BASE_DIR: str = os.path.dirname(os.path.abspath(__file__))
DB_PATH: str = os.path.join(BASE_DIR, "../../data/db/aura.db")

engine: Engine = create_engine(
engine = create_engine(
f"sqlite:///{DB_PATH}",
connect_args={"check_same_thread": False},
)
Expand Down
Loading

0 comments on commit 9f12de8

Please sign in to comment.