from fastapi.responses import JSONResponse, Response
from fastapi import status
from app.db.page_util import Pager


def response_code_view(code: int, msg: str) -> Response:
    return JSONResponse(
        status_code=code,
        content={
            'code': code,
            'msg': msg
        }
    )


def response_success(msg: str = "查询成功", data: object = None):
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content={
            'code': 200,
            'msg': msg,
            'data': data,
        }
    )


def response_success_pager(pager: Pager):
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content={
            'code': 200,
            'msg': "查询成功",
            'data': pager.data,
            'total': pager.total
        }
    )


def response_error(msg: str):
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content={
            'code': 500,
            'msg': msg,
            'data': None,
        }
    )