33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
# File: backend/app/api/v1/endpoints/messages.py (New)
|
|
# Description: API endpoint for fetching messages
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
|
from typing import List
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from app.db.database import get_db_session
|
|
from app.models.pydantic_models import MessageRead
|
|
from app.db.models import MessageModel # Import DB model
|
|
from sqlalchemy.future import select
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/session/{session_id}", response_model=List[MessageRead])
|
|
async def read_messages_for_session(
|
|
session_id: str,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
skip: int = Query(0, ge=0), # Offset for pagination
|
|
limit: int = Query(100, ge=1, le=500) # Limit number of messages
|
|
):
|
|
"""获取指定会话的消息列表 (按时间顺序)"""
|
|
# TODO: Add check if session exists
|
|
stmt = (
|
|
select(MessageModel)
|
|
.filter(MessageModel.session_id == session_id)
|
|
.order_by(MessageModel.order.asc()) # Fetch in chronological order
|
|
.offset(skip)
|
|
.limit(limit)
|
|
)
|
|
result = await db.execute(stmt)
|
|
messages = result.scalars().all()
|
|
# Validate using Pydantic model before returning
|
|
return [MessageRead.model_validate(msg) for msg in messages] |