chats.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from fastapi import Depends, Request, HTTPException, status
  2. from datetime import datetime, timedelta
  3. from typing import List, Union, Optional
  4. from utils.utils import get_current_user
  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(
  27. user=Depends(get_current_user), skip: int = 0, limit: int = 50
  28. ):
  29. return Chats.get_chat_lists_by_user_id(user.id, skip, limit)
  30. ############################
  31. # GetAllChats
  32. ############################
  33. @router.get("/all", response_model=List[ChatResponse])
  34. async def get_all_user_chats(user=Depends(get_current_user)):
  35. return [
  36. ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  37. for chat in Chats.get_all_chats_by_user_id(user.id)
  38. ]
  39. ############################
  40. # CreateNewChat
  41. ############################
  42. @router.post("/new", response_model=Optional[ChatResponse])
  43. async def create_new_chat(form_data: ChatForm, user=Depends(get_current_user)):
  44. try:
  45. chat = Chats.insert_new_chat(user.id, form_data)
  46. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  47. except Exception as e:
  48. print(e)
  49. raise HTTPException(
  50. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  51. )
  52. ############################
  53. # GetChatById
  54. ############################
  55. @router.get("/{id}", response_model=Optional[ChatResponse])
  56. async def get_chat_by_id(id: str, user=Depends(get_current_user)):
  57. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  58. if chat:
  59. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  60. else:
  61. raise HTTPException(
  62. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  63. )
  64. ############################
  65. # UpdateChatById
  66. ############################
  67. @router.post("/{id}", response_model=Optional[ChatResponse])
  68. async def update_chat_by_id(
  69. id: str, form_data: ChatForm, user=Depends(get_current_user)
  70. ):
  71. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  72. if chat:
  73. updated_chat = {**json.loads(chat.chat), **form_data.chat}
  74. chat = Chats.update_chat_by_id(id, updated_chat)
  75. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  76. else:
  77. raise HTTPException(
  78. status_code=status.HTTP_401_UNAUTHORIZED,
  79. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  80. )
  81. ############################
  82. # DeleteChatById
  83. ############################
  84. @router.delete("/{id}", response_model=bool)
  85. async def delete_chat_by_id(id: str, user=Depends(get_current_user)):
  86. result = Chats.delete_chat_by_id_and_user_id(id, user.id)
  87. return result
  88. ############################
  89. # DeleteAllChats
  90. ############################
  91. @router.delete("/", response_model=bool)
  92. async def delete_all_user_chats(user=Depends(get_current_user)):
  93. result = Chats.delete_chats_by_user_id(user.id)
  94. return result