from typing import Optional from fastapi import Query from fastapi.params import Depends from trauma.api.account.model import AccountModel from trauma.api.chat import chat_router from trauma.api.chat.db_requests import (get_chat_obj, create_chat_obj, delete_chat_obj, update_chat_obj_title, get_all_chats_obj) from trauma.api.chat.schemas import ChatWrapper, AllChatWrapper, CreateChatRequest, AllChatResponse, ChatTitleRequest from trauma.api.common.dto import Paging from trauma.core.security import PermissionDependency from trauma.core.wrappers import TraumaResponseWrapper @chat_router.get('/all') async def get_all_chats( pageSize: Optional[int] = Query(10, description="Number of objects to return per page"), pageIndex: Optional[int] = Query(0, description="Page index to retrieve"), account: AccountModel = Depends(PermissionDependency()) ) -> AllChatWrapper: chats, total_count = await get_all_chats_obj(pageSize, pageIndex, account) response = AllChatResponse( paging=Paging(pageSize=pageSize, pageIndex=pageIndex, totalCount=total_count), data=chats ) return AllChatWrapper(data=response) @chat_router.get('/{chatId}') async def get_chat( chatId: str, account: AccountModel = Depends(PermissionDependency(is_public=True)) ) -> ChatWrapper: chat = await get_chat_obj(chatId, account) return ChatWrapper(data=chat) @chat_router.post('') async def create_chat( chat_data: CreateChatRequest, account: AccountModel = Depends(PermissionDependency(is_public=True)) ) -> ChatWrapper: chat = await create_chat_obj(chat_data, account) return ChatWrapper(data=chat) @chat_router.delete('/{chatId}') async def delete_chat(chatId: str, account: AccountModel = Depends(PermissionDependency())) -> TraumaResponseWrapper: await delete_chat_obj(chatId, account) return TraumaResponseWrapper() @chat_router.patch('/{chatId}/title') async def update_chat_title( chatId: str, chat: ChatTitleRequest, account: AccountModel = Depends(PermissionDependency()) ) -> ChatWrapper: chat = await update_chat_obj_title(chatId, chat, account) return ChatWrapper(data=chat)