-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
127 lines (104 loc) · 3.77 KB
/
Copy pathmain.py
File metadata and controls
127 lines (104 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from fastapi import FastAPI, HTTPException, Depends
from pydantic import BaseModel
from typing import List, Annotated
import models
from database import engine, SessionLocal
from sqlalchemy.orm import Session
from fastapi.responses import JSONResponse
app = FastAPI()
models.Base.metadata.create_all(bind=engine)
class ChoiceBase(BaseModel):
choice_text: str
is_correct: bool
class QuestionBase(BaseModel):
question_text: str
choices: List[ChoiceBase]
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
db_dependencies = Annotated[Session, Depends(get_db)]
@app.get("/question/{question_id}")
def get_question(question_id: int, db: db_dependencies):
res = db.query(models.Questions).filter(models.Questions.question_id == question_id).first()
if res is not None:
return JSONResponse(
status_code=200,
content={"message": f'Answer the following question {res.question_text}'}
)
raise HTTPException(
status_code=400,
detail="Kindly give correct question Id"
)
@app.get("/check/{question_id}")
def check(question_id:int, answer: str, db:db_dependencies):
choices = db.query(models.Choices).filter(models.Choices.question_id == question_id).all()
for choice in choices:
if choice.choice_text == answer:
return True
return False
@app.get("/choice/{choice_id}")
def get_choice(question_id: int, db: db_dependencies):
res = []
for choice in db.query(models.Choices).filter(models.Choices.question_id == question_id).all():
res.append(choice.choice_text)
if len(res) != 0:
return JSONResponse(
status_code=200,
content={"message": f'The choices of the question is {res}'}
)
raise HTTPException(
status_code=400,
detail="There is no choice for the question"
)
@app.post("/create_questions")
def create_questions(question: QuestionBase, db: db_dependencies):
db_response = db.query(models.Questions).filter(models.Questions.question_text == question.question_text).first()
if db_response is not None:
return JSONResponse(
status_code=400,
content={"message": "Question already exists"}
)
flag: bool = False
for choice in question.choices:
if choice.is_correct is True:
flag = True
if flag is False:
return JSONResponse(
status_code=400,
content={"message": "Please give least one choice with True value"}
)
db_question = models.Questions(question_text=question.question_text)
db.add(db_question)
db.commit()
db.refresh(db_question)
for choice in question.choices:
db_choice = models.Choices(choice_text=choice.choice_text, is_correct=choice.is_correct,
question_id=db_question.question_id)
db.add(db_choice)
db.commit()
return JSONResponse(
status_code=200,
content={"message": "successfully posted"}
)
@app.delete("/delete_question/{question_id}")
def delete_question(question_id: int, db: db_dependencies):
question = db.query(models.Questions).filter(models.Questions.question_id == question_id).first()
if question is None:
raise HTTPException(
detail="question_id incorrect"
)
db.query(models.Choices).filter(models.Choices.question_id == question_id).delete()
db.query(models.Questions).filter(models.Questions.question_id == question_id).delete()
db.commit()
@app.delete("/delete_all")
def delete(db: db_dependencies):
db.query(models.Choices).delete()
db.query(models.Questions).delete()
db.commit()
return JSONResponse(
status_code=200,
content={"message": "Successfully deleted"}
)