Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separate schemas from model #24

Merged
merged 7 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Settings(BaseSettings):

class Config:
env_file = ".env.dev"
orm_mode = True
from_attributes = True


async def initiate_database():
Expand Down
Binary file added doc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 0 additions & 39 deletions models/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}
8 changes: 4 additions & 4 deletions routes/student.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions schemas/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class AdminSignIn(HTTPBasicCredentials):
class Config:
schema_extra = {
json_schema_extra = {
"example": {"username": "abdul@youngest.dev", "password": "3xt3m#"}
}

Expand All @@ -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",
}
}
}
39 changes: 39 additions & 0 deletions schemas/student.py
Original file line number Diff line number Diff line change
@@ -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",
}
}
Loading