feat: export all chats from db

admin only
This commit is contained in:
Timothy J. Baek 2024-02-04 01:07:18 -08:00
parent c4ca46637e
commit 7c2f297c84
4 changed files with 103 additions and 9 deletions

View file

@ -70,9 +70,9 @@ class ChatTable:
**{
"id": id,
"user_id": user_id,
"title": form_data.chat["title"]
if "title" in form_data.chat
else "New Chat",
"title": (
form_data.chat["title"] if "title" in form_data.chat else "New Chat"
),
"chat": json.dumps(form_data.chat),
"timestamp": int(time.time()),
}
@ -131,6 +131,12 @@ class ChatTable:
.order_by(Chat.timestamp.desc())
]
def get_all_chats(self) -> List[ChatModel]:
return [
ChatModel(**model_to_dict(chat))
for chat in Chat.select().order_by(Chat.timestamp.desc())
]
def get_all_chats_by_user_id(self, user_id: str) -> List[ChatModel]:
return [
ChatModel(**model_to_dict(chat))

View file

@ -54,6 +54,25 @@ async def get_all_user_chats(user=Depends(get_current_user)):
]
############################
# GetAllChatsInDB
############################
@router.get("/all/db", response_model=List[ChatResponse])
async def get_all_user_chats_in_db(user=Depends(get_current_user)):
if user.role == "admin":
return [
ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
for chat in Chats.get_all_chats()
]
else:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
############################
# CreateNewChat
############################