From de6b5a7bbe6844a4843e0f894c0da142a36b69ea Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 26 Aug 2024 12:27:00 +0200 Subject: [PATCH] refac --- backend/apps/webui/models/chats.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/backend/apps/webui/models/chats.py b/backend/apps/webui/models/chats.py index be77595ec..164be0646 100644 --- a/backend/apps/webui/models/chats.py +++ b/backend/apps/webui/models/chats.py @@ -249,22 +249,25 @@ class ChatTable: self, user_id: str, include_archived: bool = False, - skip: int = 0, - limit: int = -1, + skip: Optional[int] = None, + limit: Optional[int] = None, ) -> list[ChatTitleIdResponse]: with get_db() as db: query = db.query(Chat).filter_by(user_id=user_id) if not include_archived: query = query.filter_by(archived=False) - all_chats = ( - query.order_by(Chat.updated_at.desc()) - # limit cols - .with_entities(Chat.id, Chat.title, Chat.updated_at, Chat.created_at) - .limit(limit) - .offset(skip) - .all() + query = query.order_by(Chat.updated_at.desc()).with_entities( + Chat.id, Chat.title, Chat.updated_at, Chat.created_at ) + + if limit: + query = query.limit(limit) + if skip: + query = query.offset(skip) + + all_chats = query.all() + # result has to be destrctured from sqlalchemy `row` and mapped to a dict since the `ChatModel`is not the returned dataclass. return [ ChatTitleIdResponse.model_validate(