car/handler/car.py
2023-05-31 11:52:34 +08:00

39 lines
1.1 KiB
Python

from sqlalchemy.orm import Session
import logger
from fastapi import APIRouter, Depends, Query
from db.mysql import database
from orm.jiakaobaodian import JiaKaoBaoDian
from utils.common import response, strtobool
from utils.model import model_list
router = APIRouter(tags=["Car"], prefix="/car")
@router.get("/list")
def get_list(db: Session = Depends(database),
page: int = Query(default=1),
size: int = Query(default=10),
keyword: str = Query(default=""),
order_by_wrong_rate: bool = Query(default=True)):
logger.logger.info("page => %d, size => %d, keyword => %s", page, size, keyword)
query = db.query(JiaKaoBaoDian)
if len(keyword) > 0:
query = query.filter(JiaKaoBaoDian.question.like("%%%s%%" % keyword))
if order_by_wrong_rate:
query = query.order_by(JiaKaoBaoDian.wrong_rate.desc())
total = query.count()
offset = (page - 1) * size
data = query.offset(offset).limit(size).all()
logger.logger.info("data num %d", len(data))
return response({
"data": model_list(data),
"page": page,
"size": size,
"total": total
})