diff --git a/app/main.py b/app/main.py index e7744eb..588de4e 100644 --- a/app/main.py +++ b/app/main.py @@ -1,4 +1,4 @@ -from router import auth, session, group +from router import auth, session, group, student from fastapi import FastAPI, Request from fastapi.responses import JSONResponse from fastapi_jwt_auth.exceptions import AuthJWTException @@ -16,6 +16,7 @@ def authjwt_exception_handler(request: Request, exc: AuthJWTException): app.include_router(auth.router) app.include_router(session.router) app.include_router(group.router) +app.include_router(student.router) @app.get("/") diff --git a/app/router/student.py b/app/router/student.py new file mode 100644 index 0000000..5359743 --- /dev/null +++ b/app/router/student.py @@ -0,0 +1,127 @@ +from fastapi import APIRouter, HTTPException, Request, Query +import requests +from settings import settings + +router = APIRouter(prefix="/student", tags=["Student"]) +student_db_url = settings.db_url + "/student" + +QUERY_PARAMS = [ + "student_id", + "father_name", + "father_phone_number", + "mother_name", + "mother_phone_number", + "category", + "stream", + "physically_handicapped", + "family_income", + "father_profession", + "father_educational_level", + "mother_profession", + "mother_educational_level", + "time_of_device_availability", + "has_internet_access", + "contact_hours_per_week", + "is_dropper", +] + + +@router.get("/") +def get_students(request: Request): + """ + This API returns a student or a list of students who match the criteria(s) given in the request. + + Optional Parameters: + student_id (str), birth_date (str), category (str), stream (str). + For extensive list of optional parameters, refer to the DB schema note on Notion. + + Returns: + list: student data if student(s) whose details match, otherwise 404 + + Example: + > $BASE_URL/student/ + returns [data_of_all_students] + + > $BASE_URL/student/?student_id=1234 + returns [{student_data}] + + > $BASE_URL/student/?stream=PCB + returns [data_of_all_students_with_stream_PCB] + + > $BASE_URL/student/?student_id=student_id_with_stream_PCM&stream=PCB + + returns { + "status_code": 404, + "detail": "No student found!", + "headers": null + } + + """ + query_params = {} + for key in request.query_params.keys(): + if key not in QUERY_PARAMS: + return HTTPException( + status_code=400, detail="Query Parameter {} is not allowed!".format(key) + ) + query_params[key] = request.query_params[key] + + response = requests.get(student_db_url, params=query_params) + if response.status_code == 200: + if len(response.json()) == 0: + return HTTPException(status_code=404, detail="No student found!") + return response.json() + return HTTPException(status_code=response.status_code, detail=response.errors) + + +@router.get("/verify") +async def verify_student(request: Request, student_id: str): + """ + This API checks if the provided student ID and the additional details match in the database. + + Parameters: + student_id (str): The student ID to be checked. + + Other parameters: + Example: birth_date (str), category (str), stream (str). + For extensive list of optional parameters, refer to the DB schema note on Notion. + + Returns: + bool: True if the details match against the student ID, otherwise False + + Example: + > $BASE_URL/student/verify/?student_id=1234 + returns True + + > $BASE_URL/student/verify/?student_id=1234&stream=PCB + returns True + + > $BASE_URL/student/verify/?student_id={invalid_id} + returns { + "status_code": 404, + "detail": "Student ID does not exist!", + "headers": null + } + + > $BASE_URL/student/1234/?student_id=1234&stream=PCM + returns { + "status_code": 404, + "detail": "Student ID does not exist!", + "headers": null + } + + """ + query_params = {} + for key in request.query_params.keys(): + if key not in QUERY_PARAMS: + return HTTPException( + status_code=400, detail="Query Parameter {} is not allowed!".format(key) + ) + query_params[key] = request.query_params[key] + query_params["student_id"] = student_id + + response = requests.get(student_db_url, params=query_params) + if response.status_code == 200: + if len(response.json()) == 0: + return HTTPException(status_code=404, detail="Student ID does not exist!") + return True + return HTTPException(status_code=response.status_code, detail=response.errors)