car/handler/car.py

41 lines
1.2 KiB
Python
Raw Normal View History

2023-05-31 11:52:34 +08:00
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)):
2023-05-31 13:20:04 +08:00
logger.logger.info("page => {}, size => {}, keyword => {}", page, size, keyword)
2023-05-31 11:52:34 +08:00
query = db.query(JiaKaoBaoDian)
if len(keyword) > 0:
2023-05-31 13:20:04 +08:00
query = query.filter(
JiaKaoBaoDian.question.like("%%%s%%" % keyword),
JiaKaoBaoDian.illiteracy_explain.like("%%%s%%" % keyword),)
2023-05-31 11:52:34 +08:00
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()
2023-05-31 13:20:04 +08:00
logger.logger.info("data num {}", len(data))
2023-05-31 11:52:34 +08:00
return response({
"data": model_list(data),
"page": page,
"size": size,
"total": total
})