chats.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. from fastapi import Response
  2. from fastapi import Depends, FastAPI, HTTPException, status
  3. from datetime import datetime, timedelta
  4. from typing import List, Union, Optional
  5. from fastapi import APIRouter
  6. from pydantic import BaseModel
  7. import json
  8. from apps.web.models.users import Users
  9. from apps.web.models.chats import (
  10. ChatModel,
  11. ChatResponse,
  12. ChatTitleForm,
  13. ChatForm,
  14. ChatTitleIdResponse,
  15. Chats,
  16. )
  17. from utils.utils import (
  18. bearer_scheme,
  19. )
  20. from constants import ERROR_MESSAGES
  21. router = APIRouter()
  22. ############################
  23. # GetChats
  24. ############################
  25. @router.get("/", response_model=List[ChatTitleIdResponse])
  26. async def get_user_chats(skip: int = 0, limit: int = 50, cred=Depends(bearer_scheme)):
  27. token = cred.credentials
  28. user = Users.get_user_by_token(token)
  29. if user:
  30. return Chats.get_chat_lists_by_user_id(user.id, skip, limit)
  31. else:
  32. raise HTTPException(
  33. status_code=status.HTTP_401_UNAUTHORIZED,
  34. detail=ERROR_MESSAGES.INVALID_TOKEN,
  35. )
  36. ############################
  37. # GetAllChats
  38. ############################
  39. @router.get("/all", response_model=List[ChatResponse])
  40. async def get_all_user_chats(cred=Depends(bearer_scheme)):
  41. token = cred.credentials
  42. user = Users.get_user_by_token(token)
  43. if user:
  44. return [
  45. ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  46. for chat in Chats.get_all_chats_by_user_id(user.id)
  47. ]
  48. else:
  49. raise HTTPException(
  50. status_code=status.HTTP_401_UNAUTHORIZED,
  51. detail=ERROR_MESSAGES.INVALID_TOKEN,
  52. )
  53. ############################
  54. # CreateNewChat
  55. ############################
  56. @router.post("/new", response_model=Optional[ChatResponse])
  57. async def create_new_chat(form_data: ChatForm, cred=Depends(bearer_scheme)):
  58. token = cred.credentials
  59. user = Users.get_user_by_token(token)
  60. if user:
  61. chat = Chats.insert_new_chat(user.id, form_data)
  62. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  63. else:
  64. raise HTTPException(
  65. status_code=status.HTTP_401_UNAUTHORIZED,
  66. detail=ERROR_MESSAGES.INVALID_TOKEN,
  67. )
  68. ############################
  69. # GetChatById
  70. ############################
  71. @router.get("/{id}", response_model=Optional[ChatResponse])
  72. async def get_chat_by_id(id: str, cred=Depends(bearer_scheme)):
  73. token = cred.credentials
  74. user = Users.get_user_by_token(token)
  75. if user:
  76. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  77. if chat:
  78. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  79. else:
  80. raise HTTPException(
  81. status_code=status.HTTP_401_UNAUTHORIZED,
  82. detail=ERROR_MESSAGES.NOT_FOUND,
  83. )
  84. else:
  85. raise HTTPException(
  86. status_code=status.HTTP_401_UNAUTHORIZED,
  87. detail=ERROR_MESSAGES.INVALID_TOKEN,
  88. )
  89. ############################
  90. # UpdateChatById
  91. ############################
  92. @router.post("/{id}", response_model=Optional[ChatResponse])
  93. async def update_chat_by_id(id: str, form_data: ChatForm, cred=Depends(bearer_scheme)):
  94. token = cred.credentials
  95. user = Users.get_user_by_token(token)
  96. if user:
  97. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  98. if chat:
  99. updated_chat = {**json.loads(chat.chat), **form_data.chat}
  100. chat = Chats.update_chat_by_id(id, updated_chat)
  101. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  102. else:
  103. raise HTTPException(
  104. status_code=status.HTTP_401_UNAUTHORIZED,
  105. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  106. )
  107. else:
  108. raise HTTPException(
  109. status_code=status.HTTP_401_UNAUTHORIZED,
  110. detail=ERROR_MESSAGES.INVALID_TOKEN,
  111. )
  112. ############################
  113. # DeleteChatById
  114. ############################
  115. @router.delete("/{id}", response_model=bool)
  116. async def delete_chat_by_id(id: str, cred=Depends(bearer_scheme)):
  117. token = cred.credentials
  118. user = Users.get_user_by_token(token)
  119. if user:
  120. result = Chats.delete_chat_by_id_and_user_id(id, user.id)
  121. return result
  122. else:
  123. raise HTTPException(
  124. status_code=status.HTTP_401_UNAUTHORIZED,
  125. detail=ERROR_MESSAGES.INVALID_TOKEN,
  126. )
  127. ############################
  128. # DeleteAllChats
  129. ############################
  130. @router.delete("/", response_model=bool)
  131. async def delete_all_user_chats(cred=Depends(bearer_scheme)):
  132. token = cred.credentials
  133. user = Users.get_user_by_token(token)
  134. if user:
  135. result = Chats.delete_chats_by_user_id(user.id)
  136. return result
  137. else:
  138. raise HTTPException(
  139. status_code=status.HTTP_401_UNAUTHORIZED,
  140. detail=ERROR_MESSAGES.INVALID_TOKEN,
  141. )