"src/vscode:/vscode.git/clone" did not exist on "e2eb6036e2d360d2a3af1a148841f10b9269ad05"
chats.py 3.15 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
20
21
22
23
24
25
26
27
28
29
30
31
    ChatForm,
    ChatTitleIdResponse,
    Chats,
)

from utils.utils import (
    bearer_scheme,
)
from constants import ERROR_MESSAGES

router = APIRouter()

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


@router.get("/", response_model=List[ChatTitleIdResponse])
32
33
34
35
async def get_user_chats(
    user=Depends(get_current_user), skip: int = 0, limit: int = 50
):
    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 [
46
47
48
        ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
        for chat in Chats.get_all_chats_by_user_id(user.id)
    ]
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
58
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
59
    return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
Timothy J. Baek's avatar
Timothy J. Baek committed
60
61
62
63
64
65
66


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


67
@router.get("/{id}", response_model=Optional[ChatResponse])
68
69
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
70

Anuraag Jain's avatar
Anuraag Jain committed
71
72
    if chat:
        return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
Timothy J. Baek's avatar
Timothy J. Baek committed
73
    else:
74
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
88
async def update_chat_by_id(
    id: str, form_data: ChatForm, user=Depends(get_current_user)
):
    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
93
        chat = Chats.update_chat_by_id(id, updated_chat)
        return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
Anuraag Jain's avatar
Anuraag Jain committed
94
    else:
95
96
97
98
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
        )
99
100
101
102
103
104
105
106


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


@router.delete("/{id}", response_model=bool)
107
108
109
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
110

111

Timothy J. Baek's avatar
Timothy J. Baek committed
112
113
114
115
116
117
############################
# DeleteAllChats
############################


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