chats.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. ChatForm,
  13. ChatTitleIdResponse,
  14. Chats,
  15. )
  16. from utils.utils import (
  17. bearer_scheme,
  18. )
  19. from constants import ERROR_MESSAGES
  20. router = APIRouter()
  21. ############################
  22. # GetChats
  23. ############################
  24. @router.get("/", response_model=List[ChatTitleIdResponse])
  25. async def get_user_chats(skip: int = 0, limit: int = 50, cred=Depends(bearer_scheme)):
  26. token = cred.credentials
  27. user = Users.get_user_by_token(token)
  28. if user:
  29. return Chats.get_chat_lists_by_user_id(user.id, skip, limit)
  30. else:
  31. raise HTTPException(
  32. status_code=status.HTTP_401_UNAUTHORIZED,
  33. detail=ERROR_MESSAGES.INVALID_TOKEN,
  34. )
  35. ############################
  36. # CreateNewChat
  37. ############################
  38. @router.post("/new", response_model=Optional[ChatResponse])
  39. async def create_new_chat(form_data: ChatForm, cred=Depends(bearer_scheme)):
  40. token = cred.credentials
  41. user = Users.get_user_by_token(token)
  42. if user:
  43. chat = Chats.insert_new_chat(user.id, form_data)
  44. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  45. else:
  46. raise HTTPException(
  47. status_code=status.HTTP_401_UNAUTHORIZED,
  48. detail=ERROR_MESSAGES.INVALID_TOKEN,
  49. )
  50. ############################
  51. # GetChatById
  52. ############################
  53. @router.get("/{id}", response_model=Optional[ChatResponse])
  54. async def get_chat_by_id(id: str, cred=Depends(bearer_scheme)):
  55. token = cred.credentials
  56. user = Users.get_user_by_token(token)
  57. if user:
  58. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  59. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  60. else:
  61. raise HTTPException(
  62. status_code=status.HTTP_401_UNAUTHORIZED,
  63. detail=ERROR_MESSAGES.INVALID_TOKEN,
  64. )
  65. ############################
  66. # UpdateChatById
  67. ############################
  68. @router.post("/{id}", response_model=Optional[ChatResponse])
  69. async def update_chat_by_id(id: str, form_data: ChatForm, cred=Depends(bearer_scheme)):
  70. token = cred.credentials
  71. user = Users.get_user_by_token(token)
  72. if user:
  73. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  74. if chat:
  75. chat = Chats.update_chat_by_id(id, form_data.chat)
  76. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  77. else:
  78. raise HTTPException(
  79. status_code=status.HTTP_401_UNAUTHORIZED,
  80. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  81. )
  82. else:
  83. raise HTTPException(
  84. status_code=status.HTTP_401_UNAUTHORIZED,
  85. detail=ERROR_MESSAGES.INVALID_TOKEN,
  86. )
  87. ############################
  88. # DeleteChatById
  89. ############################
  90. @router.delete("/{id}", response_model=bool)
  91. async def delete_chat_by_id(id: str, cred=Depends(bearer_scheme)):
  92. token = cred.credentials
  93. user = Users.get_user_by_token(token)
  94. if user:
  95. result = Chats.delete_chat_by_id_and_user_id(id, user.id)
  96. return result
  97. else:
  98. raise HTTPException(
  99. status_code=status.HTTP_401_UNAUTHORIZED,
  100. detail=ERROR_MESSAGES.INVALID_TOKEN,
  101. )