List and Read on frontend, fullecrud on back
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
from beanie import PydanticObjectId
|
||||
from beanie.odm.enums import SortDirection
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from typing import TypeVar, List, Generic
|
||||
from fastapi_paginate import Page, Params, add_pagination
|
||||
from fastapi_paginate.ext.motor import paginate
|
||||
|
||||
from typing import TypeVar, List, Generic, Any, Dict
|
||||
|
||||
|
||||
T = TypeVar('T')
|
||||
@@ -10,7 +14,21 @@ V = TypeVar('V')
|
||||
W = TypeVar('W')
|
||||
|
||||
|
||||
def parse_sort(sort_by):
|
||||
fields = []
|
||||
for field in sort_by.split(','):
|
||||
dir, col = field.split('(')
|
||||
fields.append((col[:-1], 1 if dir == 'asc' else -1))
|
||||
|
||||
return fields
|
||||
|
||||
|
||||
def parse_query(query) -> Dict[Any, Any]:
|
||||
return {}
|
||||
|
||||
|
||||
def get_crud_router(model, model_create, model_read, model_update):
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/", response_description="{} added to the database".format(model.__name__))
|
||||
@@ -22,12 +40,17 @@ def get_crud_router(model, model_create, model_read, model_update):
|
||||
@router.get("/{id}", response_description="{} record retrieved".format(model.__name__))
|
||||
async def read_id(id: PydanticObjectId) -> model_read:
|
||||
item = await model.get(id)
|
||||
return item
|
||||
return model_read(**item.dict())
|
||||
|
||||
@router.get("/", response_description="{} records retrieved".format(model.__name__))
|
||||
async def read_list() -> List[model_read]:
|
||||
item = await model.find_all().to_list()
|
||||
return item
|
||||
@router.get("/", response_model=Page[model_read], response_description="{} records retrieved".format(model.__name__))
|
||||
async def read_list(size: int = 50, page: int = 1, sort_by: str = None, query: str = None) -> Page[model_read]:
|
||||
sort = parse_sort(sort_by)
|
||||
query = parse_query(query)
|
||||
# limit=limit, skip=offset,
|
||||
|
||||
collection = model.get_motor_collection()
|
||||
items = paginate(collection, query, Params(**{'size': size, 'page': page}), sort=sort)
|
||||
return await items
|
||||
|
||||
@router.put("/{id}", response_description="{} record updated".format(model.__name__))
|
||||
async def update(id: PydanticObjectId, req: model_update) -> model_read:
|
||||
@@ -44,7 +67,7 @@ def get_crud_router(model, model_create, model_read, model_update):
|
||||
)
|
||||
|
||||
await item.update(update_query)
|
||||
return item
|
||||
return model_read(**item.dict())
|
||||
|
||||
@router.delete("/{id}", response_description="{} record deleted from the database".format(model.__name__))
|
||||
async def delete(id: PydanticObjectId) -> dict:
|
||||
@@ -61,4 +84,7 @@ def get_crud_router(model, model_create, model_read, model_update):
|
||||
"message": "{} deleted successfully".format(model.__name__)
|
||||
}
|
||||
|
||||
add_pagination(router)
|
||||
return router
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user