chats.py 3.33 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 (
Timothy J. Baek's avatar
Timothy J. Baek committed
20
21
    bearer_scheme,
)
Timothy J. Baek's avatar
Timothy J. Baek committed
22
23
24
25
26
27
28
29
30
31
from constants import ERROR_MESSAGES

router = APIRouter()

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


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


Timothy J. Baek's avatar
Timothy J. Baek committed
38
39
40
41
42
43
############################
# GetAllChats
############################


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


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


56
@router.post("/new", response_model=Optional[ChatResponse])
57
async def create_new_chat(form_data: ChatForm, user=Depends(get_current_user)):
Timothy J. Baek's avatar
Timothy J. Baek committed
58
59
60
61
62
63
64
65
    try:
        chat = Chats.insert_new_chat(user.id, form_data)
        return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
    except Exception as e:
        print(e)
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
66
67
68
69
70
71
72


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


73
@router.get("/{id}", response_model=Optional[ChatResponse])
74
75
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
76

Anuraag Jain's avatar
Anuraag Jain committed
77
    if chat:
Timothy J. Baek's avatar
Timothy J. Baek committed
78
        return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
Timothy J. Baek's avatar
Timothy J. Baek committed
79
    else:
Timothy J. Baek's avatar
Timothy J. Baek committed
80
81
82
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
83
84
85
86
87
88
89


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


90
@router.post("/{id}", response_model=Optional[ChatResponse])
Timothy J. Baek's avatar
Timothy J. Baek committed
91
92
93
async def update_chat_by_id(
    id: str, form_data: ChatForm, user=Depends(get_current_user)
):
94
    chat = Chats.get_chat_by_id_and_user_id(id, user.id)
Anuraag Jain's avatar
Anuraag Jain committed
95
    if chat:
96
        updated_chat = {**json.loads(chat.chat), **form_data.chat}
Timothy J. Baek's avatar
Timothy J. Baek committed
97

98
        chat = Chats.update_chat_by_id(id, updated_chat)
Timothy J. Baek's avatar
Timothy J. Baek committed
99
        return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
Anuraag Jain's avatar
Anuraag Jain committed
100
    else:
101
102
103
104
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
        )
105
106
107
108
109
110
111
112


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


@router.delete("/{id}", response_model=bool)
113
114
115
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
116

117

Timothy J. Baek's avatar
Timothy J. Baek committed
118
119
120
121
122
123
############################
# DeleteAllChats
############################


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