matsuap commited on
Commit
b582880
·
verified ·
1 Parent(s): 633c3f1

Upload workbook_server.py

Browse files
Files changed (1) hide show
  1. workbook_server.py +16 -1
workbook_server.py CHANGED
@@ -1,8 +1,9 @@
1
  import uvicorn
2
  from contextlib import asynccontextmanager
3
  import sqlite3
4
- from fastapi import FastAPI
5
  from fastapi.responses import JSONResponse
 
6
 
7
  @asynccontextmanager
8
  async def lifespan(app: FastAPI):
@@ -29,5 +30,19 @@ def get_chapters():
29
  chapter_list = [{"chapter_number": row[0], "chapter_name": row[1]} for row in chapters]
30
  return JSONResponse(content=chapter_list)
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  if __name__ == '__main__':
33
  uvicorn.run(app, host="127.0.0.1", port=8000, log_level="debug")
 
1
  import uvicorn
2
  from contextlib import asynccontextmanager
3
  import sqlite3
4
+ from fastapi import FastAPI, HTTPException
5
  from fastapi.responses import JSONResponse
6
+ from pydantic import BaseModel
7
 
8
  @asynccontextmanager
9
  async def lifespan(app: FastAPI):
 
30
  chapter_list = [{"chapter_number": row[0], "chapter_name": row[1]} for row in chapters]
31
  return JSONResponse(content=chapter_list)
32
 
33
+ class ChapterRequest(BaseModel):
34
+ chapter_number: int
35
+
36
+ @app.post('/api/chapter/questions')
37
+ def get_questions_by_chapter(request: ChapterRequest):
38
+ db = get_db()
39
+ cursor = db.cursor()
40
+ cursor.execute("SELECT question_id, question_text FROM questions WHERE chapter_number = ?", (request.chapter_number,))
41
+ questions = cursor.fetchall()
42
+ if not questions:
43
+ raise HTTPException(status_code=404, detail="Questions not found for the given chapter number")
44
+ question_list = [{"question_id": row[0], "question_text": row[1]} for row in questions]
45
+ return JSONResponse(content=question_list)
46
+
47
  if __name__ == '__main__':
48
  uvicorn.run(app, host="127.0.0.1", port=8000, log_level="debug")