Spaces:
Running
Running
File size: 1,064 Bytes
48ec4db |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from sqlalchemy import Column, ForeignKey, String, Text, select
from app.backend.controllers.base_controller import engine
from sqlalchemy.ext.asyncio import AsyncSession
from app.backend.models.base_model import Base
from sqlalchemy.orm import relationship
class Message(Base):
__tablename__ = "messages"
id = Column("id", String, primary_key=True, unique=True)
content = Column("text", Text)
sender = Column("role", String)
chat_id = Column(String, ForeignKey("chats.id"))
chat = relationship("Chat", back_populates="messages")
async def add_new_message(id: str, chat_id: str, sender: str, content: str):
async with AsyncSession(engine) as db:
new_message = Message(id=id, content=content, sender=sender, chat_id=chat_id)
db.add(new_message)
await db.commit()
async def get_messages_by_chat_id(id: str) -> list[Message]:
async with AsyncSession(engine) as db:
result = await db.execute(
select(Message).where(Message.chat_id == id)
)
return result.scalars().all()
|