Skip to content

Commit d8fb9d5

Browse files
committed
feat:create endpoints for take_test screen
1 parent a4a63f6 commit d8fb9d5

5 files changed

Lines changed: 90 additions & 1 deletion

File tree

app/crud/syllabus.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from sqlalchemy.orm import Session
2+
from app import models
3+
4+
5+
def get_syllabus(db: Session):
6+
subjects = db.query(models.Subject).all()
7+
8+
result = []
9+
10+
for subject in subjects:
11+
subject_data = {
12+
"name": subject.name,
13+
"topics": []
14+
}
15+
16+
topics = db.query(models.Topic).filter_by(subject_id=subject.id).all()
17+
18+
for topic in topics:
19+
topic_data = {
20+
"name": topic.name,
21+
"chapters": []
22+
}
23+
24+
chapters = db.query(models.Chapter).filter_by(topic_id=topic.id).all()
25+
26+
for chapter in chapters:
27+
chapter_data = {
28+
"name": chapter.name,
29+
"concepts": []
30+
}
31+
32+
concepts = db.query(models.Concept).filter_by(chapter_id=chapter.id).all()
33+
34+
for concept in concepts:
35+
chapter_data["concepts"].append({
36+
"name": concept.name
37+
})
38+
39+
topic_data["chapters"].append(chapter_data)
40+
41+
subject_data["topics"].append(topic_data)
42+
43+
result.append(subject_data)
44+
45+
return result

app/database.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@
1212
else:
1313
engine = create_engine(DATABASE_URL)
1414
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
15-
Base = declarative_base()
15+
Base = declarative_base()
16+
17+
def get_db():
18+
db = SessionLocal()
19+
try:
20+
yield db
21+
finally:
22+
db.close()

app/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from fastapi import FastAPI
22
from app.database import Base, engine
33
from app.routers import auth, quiz
4+
from app.routers import auth, quiz, syllabus
45
from fastapi.middleware.cors import CORSMiddleware
56

67
app = FastAPI()
@@ -15,6 +16,7 @@
1516

1617
app.include_router(auth.router)
1718
app.include_router(quiz.router)
19+
app.include_router(syllabus.router)
1820

1921
@app.get("/")
2022
def root():

app/routers/syllabus.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from fastapi import APIRouter, Depends
2+
from sqlalchemy.orm import Session
3+
from typing import List
4+
5+
from app.database import get_db
6+
from app.crud import syllabus as syllabus_crud
7+
from app.schemas.syllabus import SubjectOut
8+
9+
router = APIRouter(prefix="/syllabus", tags=["Syllabus"])
10+
11+
12+
@router.get("/", response_model=List[SubjectOut])
13+
def get_syllabus(db: Session = Depends(get_db)):
14+
return syllabus_crud.get_syllabus(db)

app/schemas/syllabus.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pydantic import BaseModel
2+
from typing import List
3+
4+
5+
class ConceptOut(BaseModel):
6+
name: str
7+
8+
9+
class ChapterOut(BaseModel):
10+
name: str
11+
concepts: List[ConceptOut]
12+
13+
14+
class TopicOut(BaseModel):
15+
name: str
16+
chapters: List[ChapterOut]
17+
18+
19+
class SubjectOut(BaseModel):
20+
name: str
21+
topics: List[TopicOut]

0 commit comments

Comments
 (0)