Skip to content

Commit

Permalink
✨ feat(api): 增加筛选接口
Browse files Browse the repository at this point in the history
  • Loading branch information
kalicyh committed Aug 8, 2024
1 parent 05935b9 commit 6ac1060
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
21 changes: 20 additions & 1 deletion api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,23 @@ def update_numbers_info(db: Session, total_rows: int):
total_rows=total_rows
)
db.add(new_info)
db.commit()
db.commit()

def get_records_by_category(db: Session, category: str):
# 模糊匹配 category 字段
return db.query(Record).filter(Record.category.like(f'%{category}%')).all()

def get_records_by_category_and_month(db: Session, category: str, month: str):
# 模糊匹配 category 和 month 字段
return db.query(Record).filter(
Record.category.like(f'%{category}%'),
Record.month.like(f'%{month}%')
).all()

def get_records_by_full_filter(db: Session, category: str, month: str, name: str):
# 模糊匹配 category, month 和 name 字段
return db.query(Record).filter(
Record.category.like(f'%{category}%'),
Record.month.like(f'%{month}%'),
Record.name.like(f'%{name}%')
).all()
45 changes: 44 additions & 1 deletion api/routes/talking_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from io import BytesIO
import pandas as pd
import uuid
from typing import Optional, Dict, Any, List
from sqlalchemy.orm import Session
from ..database import SessionLocal, Record, Info
from ..crud import clear_records, add_records, get_records, get_info, update_info
from pydantic import BaseModel
from ..crud import clear_records, add_records, get_records, get_info, update_info, get_records_by_category, get_records_by_category_and_month, get_records_by_full_filter

router = APIRouter()

Expand Down Expand Up @@ -154,3 +156,44 @@ async def get_infos():
"total_rows": info_record.total_rows,
"name_categories": name_categories
})

class FilterRequest(BaseModel):
category: Optional[str] = None
month: Optional[str] = None
name: Optional[str] = None

@router.post("/filter")
async def get_filter_data(filter_request: FilterRequest) -> Dict[str, Any]:
db = SessionLocal() # 创建数据库会话
try:
category = filter_request.category
month = filter_request.month
name = filter_request.name

if category and not month and not name:
# 仅通过 category 返回 month
records = get_records_by_category(db, category)
months = set()
for record in records:
if record.month:
# 分割逗号分隔的月份并添加到集合中
months.update(record.month.split(','))
# 将集合转换为列表并返回
return {"months": sorted(list(months))}

if category and month and not name:
# 通过 category 和 month 返回 name
records = get_records_by_category_and_month(db, category, month)
names = list(set(record.name for record in records if record.name))
return {"names": names}

if category and month and name:
# 通过 category, month 和 name 返回 text
records = get_records_by_full_filter(db, category, month, name)
texts = [record.text for record in records if record.text]
return {"texts": texts}

# 如果没有匹配的条件,则返回空结果或适当的错误信息
raise HTTPException(status_code=400, detail="Invalid query parameters")
finally:
db.close() # 确保会话关闭

0 comments on commit 6ac1060

Please sign in to comment.