car/handler/car.py
2023-05-31 13:50:18 +08:00

40 lines
1.2 KiB
Python

from sqlalchemy.orm import Session
import logger
from fastapi import APIRouter, Depends, Query
from db.mysql import database
from model.car.dto.list import ListDto
from orm.jiakaobaodian import JiaKaoBaoDian
from utils.common import response, strtobool
from utils.model import model_list
router = APIRouter(tags=["Car"], prefix="/car")
@router.post("/list")
def get_list(dto: ListDto, db: Session = Depends(database)):
(page, size, keyword) = (dto.page, dto.size, dto.keyword)
order_by_wrong_rate = dto.order_by_wrong_rate
logger.logger.info("page => {}, size => {}, keyword => {}", page, size, keyword)
query = db.query(JiaKaoBaoDian)
if len(keyword) > 0:
query = query.filter(
JiaKaoBaoDian.question.like("%%%s%%" % keyword) |
JiaKaoBaoDian.illiteracy_explain.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 {}", len(data))
return response({
"data": model_list(data),
"page": page,
"size": size,
"total": total
})