chats.py 2.86 KB
Newer Older
Anuraag Jain's avatar
Anuraag Jain committed
1
2

from fastapi import Depends, Request, HTTPException, status
Timothy J. Baek's avatar
Timothy J. Baek committed
3
4
5
6
7
from datetime import datetime, timedelta
from typing import List, Union, Optional

from fastapi import APIRouter
from pydantic import BaseModel
8
import json
Timothy J. Baek's avatar
Timothy J. Baek committed
9
10
11
12

from apps.web.models.users import Users
from apps.web.models.chats import (
    ChatModel,
13
    ChatResponse,
Timothy J. Baek's avatar
Timothy J. Baek committed
14
    ChatTitleForm,
Timothy J. Baek's avatar
Timothy J. Baek committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    ChatForm,
    ChatTitleIdResponse,
    Chats,
)

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

router = APIRouter()

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


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


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


@router.get("/all", response_model=List[ChatResponse])
Anuraag Jain's avatar
Anuraag Jain committed
43
44
async def get_all_user_chats(request:Request,):
    return [
Timothy J. Baek's avatar
Timothy J. Baek committed
45
            ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
Anuraag Jain's avatar
Anuraag Jain committed
46
            for chat in Chats.get_all_chats_by_user_id(request.user.id)
Timothy J. Baek's avatar
Timothy J. Baek committed
47
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])
Anuraag Jain's avatar
Anuraag Jain committed
56
57
58
async def create_new_chat(form_data: ChatForm,request:Request):
    chat = Chats.insert_new_chat(request.user.id, form_data)
    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])
Anuraag Jain's avatar
Anuraag Jain committed
67
68
async def get_chat_by_id(id: str, request:Request):
    chat = Chats.get_chat_by_id_and_user_id(id, request.user.id)
Timothy J. Baek's avatar
Timothy J. Baek committed
69

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


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


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

            chat = Chats.update_chat_by_id(id, updated_chat)
89
            return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
Anuraag Jain's avatar
Anuraag Jain committed
90
    else:
Timothy J. Baek's avatar
Timothy J. Baek committed
91
92
93
94
            raise HTTPException(
                status_code=status.HTTP_401_UNAUTHORIZED,
                detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
            )
95
96
97
98
99
100
101
102


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


@router.delete("/{id}", response_model=bool)
Anuraag Jain's avatar
Anuraag Jain committed
103
104
105
async def delete_chat_by_id(id: str, request: Request):
    result = Chats.delete_chat_by_id_and_user_id(id, request.user.id)
    return result