File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1212else :
1313 engine = create_engine (DATABASE_URL )
1414SessionLocal = 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 ()
Original file line number Diff line number Diff line change 11from fastapi import FastAPI
22from app .database import Base , engine
33from app .routers import auth , quiz
4+ from app .routers import auth , quiz , syllabus
45from fastapi .middleware .cors import CORSMiddleware
56
67app = FastAPI ()
1516
1617app .include_router (auth .router )
1718app .include_router (quiz .router )
19+ app .include_router (syllabus .router )
1820
1921@app .get ("/" )
2022def root ():
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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 ]
You can’t perform that action at this time.
0 commit comments