from fastapi import Query, APIRouter from service import MySQLService from request import RequestMySQL from response import ResponseMySQL as res from typing import Optional from request import RequestMySQL as req from function import support_function from fastapi import HTTPException router = APIRouter() @router.get("/render_chat_history", tags=["MySQL"]) async def render_chat(user_id: Optional[str] = Query(None)): check = support_function.check_value_user_id_controller(user_id) if check is not True: return check request = RequestMySQL.RequestRenderChatHistory(user_id=user_id) return MySQLService.render_chat_history(request) @router.get("/data_relevant", tags=["MySQL"]) async def render_chat_1(chat_detail_id: str): if chat_detail_id is None or chat_detail_id.strip() == "": return res.ReponseError(status=400, data=res.Message(message="Id field is required.")) chat_detail_id = chat_detail_id.strip("'").strip('"') try: chat_detail_id_int = int(chat_detail_id) except ValueError: return res.ReponseError(status=400, data=res.Message(message="Value must be an integer")) if not support_function.is_positive_integer(chat_detail_id_int): return res.ReponseError(status=400, data=res.Message(message="Value must be greater than 0")) request = req.RequestGetChatDetails(id=chat_detail_id) return MySQLService.get_detail_chat_by_chat_id(request) @router.get("/load_chat_history", tags=["MySQL"]) async def load_chat(chat_id: Optional[str] = Query(None), user_id: Optional[str] = Query(None)): check = support_function.check_value_user_id_controller(user_id) if check is not True: return check if chat_id is None or chat_id.strip() == "": return res.ReponseError(status=400, data=res.Message(message="Chat id field is required.")) chat_id = chat_id.strip("'").strip('"') try: chat_id_int = int(chat_id) except ValueError: return res.ReponseError(status=400, data=res.Message(message="Value must be an integer")) if not support_function.is_positive_integer(chat_id_int): return res.ReponseError(status=400, data=res.Message(message="Value must be greater than 0")) request = req.RequestLoadChatHistory(chat_id=chat_id,user_id = user_id) return MySQLService.load_chat_history(request) @router.put("/edit_chat/", tags=["MySQL"]) async def edit_chat(request: RequestMySQL.RequestEditNameChat): user_id = request.user_id check = support_function.check_value_user_id_controller(user_id) if check is not True: return check return MySQLService.edit_chat(request) @router.delete("/delete_chat/", tags=["MySQL"]) async def delete_chat(request: RequestMySQL.RequestDeleteChat): user_id = request.user_id check = support_function.check_value_user_id_controller(user_id) if check is not True: return check return MySQLService.delete_chat(request)