File size: 2,586 Bytes
50553ea
 
c6cc0f2
50553ea
c6cc0f2
 
50553ea
 
 
 
 
3ec35ef
50553ea
 
c6cc0f2
50553ea
 
 
 
 
 
 
c6cc0f2
50553ea
9150f8e
50553ea
 
 
 
 
 
 
 
 
c6cc0f2
 
50553ea
c6cc0f2
50553ea
 
 
 
c6cc0f2
 
 
 
 
50553ea
 
 
 
 
c6cc0f2
 
50553ea
c6cc0f2
50553ea
3ec35ef
 
 
 
c6cc0f2
 
3ec35ef
c6cc0f2
3ec35ef
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from typing import Optional

from fastapi import Query, Depends

from trauma.api.account.dto import AccountType
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, save_intro_message)
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"),
        _: AccountModel = Depends(PermissionDependency([AccountType.Admin]))
) -> AllChatWrapper:
    chats, total_count = await get_all_chats_obj(pageSize, pageIndex)
    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([AccountType.Admin, AccountType.User]))
) -> ChatWrapper:
    chat = await get_chat_obj(chatId, account)
    return ChatWrapper(data=chat)


@chat_router.delete('/{chatId}')
async def delete_chat(
        chatId: str,
        account: AccountModel = Depends(PermissionDependency([AccountType.Admin, AccountType.User]))
) -> 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([AccountType.Admin, AccountType.User]))
) -> ChatWrapper:
    chat = await update_chat_obj_title(chatId, chat, account)
    return ChatWrapper(data=chat)


@chat_router.post('')
async def create_chat(
        chat_data: CreateChatRequest,
        account: AccountModel = Depends(PermissionDependency([AccountType.Admin, AccountType.User]))
) -> ChatWrapper:
    chat = await create_chat_obj(chat_data, account)
    await save_intro_message(chat.id)
    return ChatWrapper(data=chat)