Spaces:
Running
Running
Commit
·
f60a772
1
Parent(s):
5b13498
Fix: chat message list do not have created_at
Browse files
app/database/models/base.py
CHANGED
|
@@ -8,6 +8,7 @@ from sqlalchemy.orm import Mapped, mapped_column
|
|
| 8 |
from litestar.dto import dto_field
|
| 9 |
from pydantic import BaseModel as _BaseModel, ConfigDict
|
| 10 |
import uuid
|
|
|
|
| 11 |
class BaseModel(base.DefaultBase):
|
| 12 |
__abstract__ = True
|
| 13 |
id: Mapped[PG_UUID] = mapped_column(
|
|
|
|
| 8 |
from litestar.dto import dto_field
|
| 9 |
from pydantic import BaseModel as _BaseModel, ConfigDict
|
| 10 |
import uuid
|
| 11 |
+
|
| 12 |
class BaseModel(base.DefaultBase):
|
| 13 |
__abstract__ = True
|
| 14 |
id: Mapped[PG_UUID] = mapped_column(
|
app/database/models/chat_message.py
CHANGED
|
@@ -17,7 +17,6 @@ if TYPE_CHECKING:
|
|
| 17 |
@dataclass
|
| 18 |
class ChatMessage(BaseModel):
|
| 19 |
__tablename__ = "chat_messages"
|
| 20 |
-
|
| 21 |
id: Mapped[uuid.UUID] = mapped_column(
|
| 22 |
PG_UUID(as_uuid=True),
|
| 23 |
primary_key=True,
|
|
|
|
| 17 |
@dataclass
|
| 18 |
class ChatMessage(BaseModel):
|
| 19 |
__tablename__ = "chat_messages"
|
|
|
|
| 20 |
id: Mapped[uuid.UUID] = mapped_column(
|
| 21 |
PG_UUID(as_uuid=True),
|
| 22 |
primary_key=True,
|
app/database/models/user.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from __future__ import annotations
|
|
|
|
| 2 |
from datetime import datetime
|
| 3 |
from typing import Optional
|
| 4 |
import uuid
|
|
@@ -34,7 +35,7 @@ class UserTag(BaseModel):
|
|
| 34 |
PG_UUID(as_uuid=True), ForeignKey("tags.id"), primary_key=False
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
| 38 |
class User(BaseModel):
|
| 39 |
__tablename__ = "users"
|
| 40 |
name: Mapped[str] = mapped_column(String(255), unique=False, nullable=False)
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
from datetime import datetime
|
| 4 |
from typing import Optional
|
| 5 |
import uuid
|
|
|
|
| 35 |
PG_UUID(as_uuid=True), ForeignKey("tags.id"), primary_key=False
|
| 36 |
)
|
| 37 |
|
| 38 |
+
@dataclass
|
| 39 |
class User(BaseModel):
|
| 40 |
__tablename__ = "users"
|
| 41 |
name: Mapped[str] = mapped_column(String(255), unique=False, nullable=False)
|
app/domains/chat_message/service.py
CHANGED
|
@@ -2,14 +2,14 @@ from collections.abc import AsyncGenerator
|
|
| 2 |
from datetime import datetime
|
| 3 |
from typing import List
|
| 4 |
import uuid
|
| 5 |
-
|
| 6 |
from sqlalchemy import and_, desc, or_, select
|
| 7 |
from sqlalchemy.orm import noload
|
| 8 |
|
| 9 |
from database.models.property import Property
|
| 10 |
from domains.properties.service import PropertyService
|
| 11 |
from domains.chat_session.service import ChatSessionService
|
| 12 |
-
from database.models.chat_message import ChatMessage
|
| 13 |
from advanced_alchemy.repository import SQLAlchemyAsyncRepository
|
| 14 |
from advanced_alchemy.service import SQLAlchemyAsyncRepositoryService
|
| 15 |
from database.models.chat_session import ChatSession
|
|
@@ -114,33 +114,34 @@ class ChatMessageService(SQLAlchemyAsyncRepositoryService[ChatMessage]):
|
|
| 114 |
.limit(limit_offset.limit)
|
| 115 |
)
|
| 116 |
result = await self.repository.session.execute(paginated)
|
| 117 |
-
items = result.scalars().
|
| 118 |
total = await self.count()
|
| 119 |
return OffsetPagination(
|
| 120 |
-
items=
|
| 121 |
total=total,
|
| 122 |
limit=limit_offset.limit,
|
| 123 |
offset=limit_offset.offset,
|
| 124 |
)
|
| 125 |
|
|
|
|
| 126 |
async def chat_messages_by_session_id(
|
| 127 |
self, session_id: uuid.UUID, limit_offset: LimitOffset
|
| 128 |
) -> OffsetPagination[ChatMessage]:
|
| 129 |
query = select(ChatMessage).options(
|
| 130 |
noload(ChatMessage.sender)
|
| 131 |
)
|
| 132 |
-
query = query.
|
| 133 |
-
query = query.where(ChatSession.id == session_id)
|
| 134 |
paginated = (
|
| 135 |
-
query.order_by(desc(
|
| 136 |
.offset(limit_offset.offset)
|
| 137 |
.limit(limit_offset.limit)
|
| 138 |
)
|
|
|
|
| 139 |
result = await self.repository.session.execute(paginated)
|
| 140 |
-
items = result.scalars().
|
| 141 |
total = await self.count()
|
| 142 |
return OffsetPagination(
|
| 143 |
-
items=
|
| 144 |
total=total,
|
| 145 |
limit=limit_offset.limit,
|
| 146 |
offset=limit_offset.offset,
|
|
|
|
| 2 |
from datetime import datetime
|
| 3 |
from typing import List
|
| 4 |
import uuid
|
| 5 |
+
from sqlalchemy.dialects import postgresql # or mysql, sqlite depending on your DB
|
| 6 |
from sqlalchemy import and_, desc, or_, select
|
| 7 |
from sqlalchemy.orm import noload
|
| 8 |
|
| 9 |
from database.models.property import Property
|
| 10 |
from domains.properties.service import PropertyService
|
| 11 |
from domains.chat_session.service import ChatSessionService
|
| 12 |
+
from database.models.chat_message import ChatMessage, ChatMessageSchema
|
| 13 |
from advanced_alchemy.repository import SQLAlchemyAsyncRepository
|
| 14 |
from advanced_alchemy.service import SQLAlchemyAsyncRepositoryService
|
| 15 |
from database.models.chat_session import ChatSession
|
|
|
|
| 114 |
.limit(limit_offset.limit)
|
| 115 |
)
|
| 116 |
result = await self.repository.session.execute(paginated)
|
| 117 |
+
items = result.scalars().all()
|
| 118 |
total = await self.count()
|
| 119 |
return OffsetPagination(
|
| 120 |
+
items=self.to_schema(data=items, schema_type=ChatMessageSchema).items,
|
| 121 |
total=total,
|
| 122 |
limit=limit_offset.limit,
|
| 123 |
offset=limit_offset.offset,
|
| 124 |
)
|
| 125 |
|
| 126 |
+
|
| 127 |
async def chat_messages_by_session_id(
|
| 128 |
self, session_id: uuid.UUID, limit_offset: LimitOffset
|
| 129 |
) -> OffsetPagination[ChatMessage]:
|
| 130 |
query = select(ChatMessage).options(
|
| 131 |
noload(ChatMessage.sender)
|
| 132 |
)
|
| 133 |
+
query = query.where(ChatMessage.session_id == session_id)
|
|
|
|
| 134 |
paginated = (
|
| 135 |
+
query.order_by(desc(ChatMessage.created_at))
|
| 136 |
.offset(limit_offset.offset)
|
| 137 |
.limit(limit_offset.limit)
|
| 138 |
)
|
| 139 |
+
print(paginated.compile(dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True}))
|
| 140 |
result = await self.repository.session.execute(paginated)
|
| 141 |
+
items = result.scalars().all()
|
| 142 |
total = await self.count()
|
| 143 |
return OffsetPagination(
|
| 144 |
+
items=self.to_schema(data=items, schema_type=ChatMessageSchema).items,
|
| 145 |
total=total,
|
| 146 |
limit=limit_offset.limit,
|
| 147 |
offset=limit_offset.offset,
|