async def add_new_user(user: UserSerialization): """ create new user :param user: :return: """ user_inst = await User.create(**user.dict()) return GeneralJSONResponse(code=1000, data=dict(id=user_inst.id, username=user_inst.username))
async def get_user_list( page_size: int = Query(10, title="page size", description="how many user in a page", gt=1), page_number: int = Query(0, title="page number", description="current page of user list", ge=0) ): total = await User.all().count() total_pages = total % page_size if page_number >= total_pages: page_number = total_pages offset = (page_number - 1) * page_size if page_number > 0 else 0 limit = page_size users = await User.all().offset(offset).limit(limit).values() return GeneralJSONResponse(code=1000, data=dict(total_page=total_pages, page_size=page_size, page_number=page_number, objects=users))
async def get_user_detail(user_id: int): user = await User.filter(id=user_id).values(flat=True) return GeneralJSONResponse(code=1000, data=user)
async def counter(): rc = RedisDriver() await rc.set("a", 111) return GeneralJSONResponse(code=1000, data="hi, write something to redis")
async def greeting(): return GeneralJSONResponse(code=1000, data="hi, welcome to fastapi boilerplate")
def handler_params_exception(request: Request, exc: HTTPException): # print(exc.detail) print(traceback.print_exc()) return GeneralJSONResponse(code=3000, data={}, detail=str(exc))
def handle_general_exception(request: Request, exc: HTTPException): # HTTPException is an based on Exception # for general debug , or batter you can write this to log file # print(exc.detail) print(traceback.print_exc()) return GeneralJSONResponse(code=4000, data={}, detail=str(exc))