diff --git a/app.py b/app.py index eb34a38..14850d6 100644 --- a/app.py +++ b/app.py @@ -9,15 +9,17 @@ from typing_extensions import Annotated from bson import ObjectId -import motor.motor_asyncio +import pymongo +from pymongo import AsyncMongoClient from pymongo import ReturnDocument +from pymongo.server_api import ServerApi app = FastAPI( title="Student Course API", summary="A sample application showing how to use FastAPI to add a ReST API to a MongoDB collection.", ) -client = motor.motor_asyncio.AsyncIOMotorClient(os.environ["MONGODB_URL"]) +client = AsyncMongoClient(os.environ["MONGODB_URL"],server_api=pymongo.server_api.ServerApi(version="1", strict=True,deprecation_errors=True)) db = client.college student_collection = db.get_collection("students") @@ -99,13 +101,11 @@ async def create_student(student: StudentModel = Body(...)): A unique `id` will be created and provided in the response. """ - new_student = await student_collection.insert_one( - student.model_dump(by_alias=True, exclude=["id"]) - ) - created_student = await student_collection.find_one( - {"_id": new_student.inserted_id} - ) - return created_student + new_student = student.model_dump(by_alias=True, exclude=["id"]) + result = await student_collection.insert_one(new_student) + new_student["_id"] = result.inserted_id + + return new_student @app.get( diff --git a/dev-requirements.in b/dev-requirements.in deleted file mode 100644 index b16298f..0000000 --- a/dev-requirements.in +++ /dev/null @@ -1,3 +0,0 @@ -requests -pytest -pip-tools \ No newline at end of file diff --git a/dev-requirements.txt b/dev-requirements.txt deleted file mode 100644 index 8b77d72..0000000 --- a/dev-requirements.txt +++ /dev/null @@ -1,42 +0,0 @@ -# -# This file is autogenerated by pip-compile with Python 3.12 -# by the following command: -# -# pip-compile --strip-extras dev-requirements.in -# -build==1.1.1 - # via pip-tools -certifi==2023.7.22 - # via requests -charset-normalizer==3.3.1 - # via requests -click==8.1.7 - # via pip-tools -idna==3.4 - # via requests -iniconfig==2.0.0 - # via pytest -packaging==23.2 - # via - # build - # pytest -pip-tools==7.4.1 - # via -r dev-requirements.in -pluggy==1.3.0 - # via pytest -pyproject-hooks==1.0.0 - # via - # build - # pip-tools -pytest==7.4.3 - # via -r dev-requirements.in -requests==2.31.0 - # via -r dev-requirements.in -urllib3==2.0.7 - # via requests -wheel==0.42.0 - # via pip-tools - -# The following packages are considered to be unsafe in a requirements file: -# pip -# setuptools diff --git a/requirements.in b/requirements.in index 113d9d3..72d2551 100644 --- a/requirements.in +++ b/requirements.in @@ -1,4 +1,4 @@ fastapi ~=0.110 -motor ~=3.3 +pymongo ~=4.13 uvicorn ~=0.28 -pydantic[email] \ No newline at end of file +pydantic[email] diff --git a/requirements.txt b/requirements.txt index 9bf9d19..56e7967 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,15 +24,13 @@ idna==3.4 # via # anyio # email-validator -motor==3.3.1 - # via -r requirements.in pydantic==2.6.3 # via # -r requirements.in # fastapi pydantic-core==2.16.3 # via pydantic -pymongo==4.5.0 +pymongo==4.13.1 # via motor sniffio==1.3.0 # via anyio