File size: 663 Bytes
4cd2145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio

from cbh.api.chat.model import ChatModel
from cbh.core.config import settings


async def create_chat_obj() -> ChatModel:
    chat = ChatModel()
    await settings.DB_CLIENT.chats.insert_one(chat.to_mongo())
    return chat


async def get_all_chats_obj(page_size: int, page_index: int) -> tuple[list[dict], int]:
    skip = page_size * page_index
    objects, total_count = await asyncio.gather(
        settings.DB_CLIENT.chats
        .find({})
        .sort("_id", -1)
        .skip(skip)
        .limit(page_size)
        .to_list(length=page_size),
        settings.DB_CLIENT.chats.count_documents({}),
    )
    return objects, total_count