chats.py 3.27 KB
Newer Older
Anuraag Jain's avatar
Anuraag Jain committed
1
from fastapi import Depends, Request, HTTPException, status
Timothy J. Baek's avatar
Timothy J. Baek committed
2
3
from datetime import datetime, timedelta
from typing import List, Union, Optional
4
from utils.utils import get_current_user
Timothy J. Baek's avatar
Timothy J. Baek committed
5
6
from fastapi import APIRouter
from pydantic import BaseModel
7
import json
Timothy J. Baek's avatar
Timothy J. Baek committed
8
9
10
11

from apps.web.models.users import Users
from apps.web.models.chats import (
    ChatModel,
12
    ChatResponse,
Timothy J. Baek's avatar
Timothy J. Baek committed
13
    ChatTitleForm,
Timothy J. Baek's avatar
Timothy J. Baek committed
14
15
16
17
18
19
    ChatForm,
    ChatTitleIdResponse,
    Chats,
)

from utils.utils import (
20
    bearer_scheme, )
Timothy J. Baek's avatar
Timothy J. Baek committed
21
22
23
24
25
26
27
28
29
30
from constants import ERROR_MESSAGES

router = APIRouter()

############################
# GetChats
############################


@router.get("/", response_model=List[ChatTitleIdResponse])
31
async def get_user_chats(
32
        user=Depends(get_current_user), skip: int = 0, limit: int = 50):
33
    return Chats.get_chat_lists_by_user_id(user.id, skip, limit)
Timothy J. Baek's avatar
Timothy J. Baek committed
34
35


Timothy J. Baek's avatar
Timothy J. Baek committed
36
37
38
39
40
41
############################
# GetAllChats
############################


@router.get("/all", response_model=List[ChatResponse])
42
async def get_all_user_chats(user=Depends(get_current_user)):
Anuraag Jain's avatar
Anuraag Jain committed
43
    return [
44
45
46
        ChatResponse(**{
            **chat.model_dump(), "chat": json.loads(chat.chat)
        }) for chat in Chats.get_all_chats_by_user_id(user.id)
47
    ]
Timothy J. Baek's avatar
Timothy J. Baek committed
48
49


Timothy J. Baek's avatar
Timothy J. Baek committed
50
51
52
53
54
############################
# CreateNewChat
############################


55
@router.post("/new", response_model=Optional[ChatResponse])
56
57
async def create_new_chat(form_data: ChatForm, user=Depends(get_current_user)):
    chat = Chats.insert_new_chat(user.id, form_data)
Anuraag Jain's avatar
Anuraag Jain committed
58
    return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
Timothy J. Baek's avatar
Timothy J. Baek committed
59
60
61
62
63
64
65


############################
# GetChatById
############################


66
@router.get("/{id}", response_model=Optional[ChatResponse])
67
68
async def get_chat_by_id(id: str, user=Depends(get_current_user)):
    chat = Chats.get_chat_by_id_and_user_id(id, user.id)
Timothy J. Baek's avatar
Timothy J. Baek committed
69

Anuraag Jain's avatar
Anuraag Jain committed
70
    if chat:
71
72
73
        return ChatResponse(**{
            **chat.model_dump(), "chat": json.loads(chat.chat)
        })
Timothy J. Baek's avatar
Timothy J. Baek committed
74
    else:
75
76
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
                            detail=ERROR_MESSAGES.NOT_FOUND)
Timothy J. Baek's avatar
Timothy J. Baek committed
77
78
79
80
81
82
83


############################
# UpdateChatById
############################


84
@router.post("/{id}", response_model=Optional[ChatResponse])
85
86
87
async def update_chat_by_id(id: str,
                            form_data: ChatForm,
                            user=Depends(get_current_user)):
88
    chat = Chats.get_chat_by_id_and_user_id(id, user.id)
Anuraag Jain's avatar
Anuraag Jain committed
89
    if chat:
90
        updated_chat = {**json.loads(chat.chat), **form_data.chat}
Timothy J. Baek's avatar
Timothy J. Baek committed
91

92
        chat = Chats.update_chat_by_id(id, updated_chat)
93
94
95
        return ChatResponse(**{
            **chat.model_dump(), "chat": json.loads(chat.chat)
        })
Anuraag Jain's avatar
Anuraag Jain committed
96
    else:
97
98
99
100
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
        )
101
102
103
104
105
106
107
108


############################
# DeleteChatById
############################


@router.delete("/{id}", response_model=bool)
109
110
111
async def delete_chat_by_id(id: str, user=Depends(get_current_user)):
    result = Chats.delete_chat_by_id_and_user_id(id, user.id)
    return result
Timothy J. Baek's avatar
Timothy J. Baek committed
112

113

Timothy J. Baek's avatar
Timothy J. Baek committed
114
115
116
117
118
119
############################
# DeleteAllChats
############################


@router.delete("/", response_model=bool)
120
121
122
async def delete_all_user_chats(user=Depends(get_current_user)):
    result = Chats.delete_chats_by_user_id(user.id)
    return result