diff --git a/README.md b/README.md index 0defcd4..49acec8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # FastAPI and MongoDB Boilerplate -A simple starter for building RESTful APIs with FastAPI and MongoDB. +A simple starter for building RESTful APIs with FastAPI and MongoDB. + +![image](./img.jpg) ## Features @@ -34,13 +36,13 @@ $ python3 -m venv venv 4. Start the application: ```console -python main.py +python3 main.py ``` The starter listens on port 8000 on address [0.0.0.0](0.0.0.0:8080). -![FastAPI-MongoDB starter](https://user-images.githubusercontent.com/31009679/165318867-4a0504d5-1fd0-4adc-8df9-db2ff3c0c3b9.png) +![FastAPI-MongoDB starter](doc.png) ## Testing diff --git a/config/config.py b/config/config.py index 70075ed..2f2ebd0 100644 --- a/config/config.py +++ b/config/config.py @@ -16,7 +16,7 @@ class Settings(BaseSettings): class Config: env_file = ".env.dev" - orm_mode = True + from_attributes = True async def initiate_database(): diff --git a/doc.png b/doc.png new file mode 100644 index 0000000..d7d3f8a Binary files /dev/null and b/doc.png differ diff --git a/img.jpg b/img.jpg new file mode 100644 index 0000000..442e589 Binary files /dev/null and b/img.jpg differ diff --git a/models/student.py b/models/student.py index 858647c..6503e4a 100644 --- a/models/student.py +++ b/models/student.py @@ -24,42 +24,3 @@ class Config: class Settings: name = "student" - - -class UpdateStudentModel(BaseModel): - fullname: Optional[str] - email: Optional[EmailStr] - course_of_study: Optional[str] - year: Optional[int] - gpa: Optional[float] - - class Collection: - name = "student" - - class Config: - schema_extra = { - "example": { - "fullname": "Abdulazeez Abdulazeez", - "email": "abdul@school.com", - "course_of_study": "Water resources and environmental engineering", - "year": 4, - "gpa": "5.0", - } - } - - -class Response(BaseModel): - status_code: int - response_type: str - description: str - data: Optional[Any] - - class Config: - schema_extra = { - "example": { - "status_code": 200, - "response_type": "success", - "description": "Operation successful", - "data": "Sample data", - } - } diff --git a/routes/student.py b/routes/student.py index 6cbfc14..658f0ec 100644 --- a/routes/student.py +++ b/routes/student.py @@ -1,7 +1,9 @@ from fastapi import APIRouter, Body from database.database import * -from models.student import * +from models.student import Student +from schemas.student import Response, UpdateStudentModel + router = APIRouter() @@ -17,9 +19,7 @@ async def get_students(): } -@router.get( - "/{id}", response_description="Student data retrieved", response_model=Response -) +@router.get("/{id}", response_description="Student data retrieved", response_model=Response) async def get_student_data(id: PydanticObjectId): student = await retrieve_student(id) if student: diff --git a/schemas/admin.py b/schemas/admin.py index a459d03..4b3fb6a 100644 --- a/schemas/admin.py +++ b/schemas/admin.py @@ -4,7 +4,7 @@ class AdminSignIn(HTTPBasicCredentials): class Config: - schema_extra = { + json_schema_extra = { "example": {"username": "abdul@youngest.dev", "password": "3xt3m#"} } @@ -14,9 +14,9 @@ class AdminData(BaseModel): email: EmailStr class Config: - schema_extra = { + json_schema_extra = { "example": { "fullname": "Abdulazeez Abdulazeez Adeshina", "email": "abdul@youngest.dev", } - } \ No newline at end of file + } diff --git a/schemas/student.py b/schemas/student.py new file mode 100644 index 0000000..3fc03f9 --- /dev/null +++ b/schemas/student.py @@ -0,0 +1,39 @@ +from pydantic import BaseModel, EmailStr +from typing import Optional, Any + +class UpdateStudentModel(BaseModel): + fullname: Optional[str] + email: Optional[EmailStr] + course_of_study: Optional[str] + year: Optional[int] + gpa: Optional[float] + + class Collection: + name = "student" + + class Config: + json_schema_extra = { + "example": { + "fullname": "Abdulazeez Abdulazeez", + "email": "abdul@school.com", + "course_of_study": "Water resources and environmental engineering", + "year": 4, + "gpa": "5.0", + } + } + +class Response(BaseModel): + status_code: int + response_type: str + description: str + data: Optional[Any] + + class Config: + json_schema_extra = { + "example": { + "status_code": 200, + "response_type": "success", + "description": "Operation successful", + "data": "Sample data", + } + }