from uuid import uuid4 from abc import ABC, abstractmethod from src.config import TBL_BOT_SET, TBL_USER_BOT, VW_USER_BOT_LATEST from src.gcp.bigquery import executor, client from src.bot.schemas import BotCreateModel, BotUpdateModel # Define the interface (abstract base class) for the Bot_SettingsRepository class BotsRepositoryInterface(ABC): @abstractmethod def get_bot_by_user_id(self, user_id): pass @abstractmethod def save_bot_settings(self, bot_settings = BotCreateModel): pass @abstractmethod def update_bot_settings(self, bot_settings = BotUpdateModel): pass # @abstractmethod # def delete_bot_settings(self, user_id): # pass # Concrete implementation of the bot_settingsRepository using BigQuery class BotRepositoryBigQuery(BotsRepositoryInterface): def __init__(self, executor): self._executor = executor def get_bot_by_user_id(self, user_id): query = f"SELECT * FROM `{VW_USER_BOT_LATEST}` WHERE user_id = '{user_id}'" return self._executor.execute_query(query) def save_user_bot(self,user_id): id = str(uuid4()) row_to_insert = { "id": id, "user_id" : user_id } errors = client.insert_rows_json(TBL_USER_BOT, [row_to_insert]) if not errors: return id else: msg = f"Errors occurred while saving bot for user {user_id}: {errors}" print(msg) return {"message":msg} def save_bot_settings(self, bot_settings = BotCreateModel): bot_id = self.save_user_bot(bot_settings['user_id']) row_to_insert = { "id": str(uuid4()), "bot_id": bot_id, "title": bot_settings['title'], "description": bot_settings["description"], "chat_styles": bot_settings['chat_styles'], "initial_message": bot_settings['initial_message'], "message_suggestion": bot_settings['message_suggestion'], "allow_feedback_response": bot_settings["allow_feedback_response"], "show_source_in_response": bot_settings["show_source_in_response"], "advance_settings": bot_settings["advance_settings"] } errors = client.insert_rows_json(TBL_BOT_SET, [row_to_insert]) if not errors: msg = f"Bot settings for bot {bot_id} saved successfully." print(msg) return {"message":msg} else: msg = f"Errors occurred while saving bot settings for bot_id: {bot_id}: {errors}" print(msg) return {"message":msg} def get_bot_settings(self,bot_id): query = f"SELECT * FROM `{VW_USER_BOT_LATEST}` WHERE bot_id = '{bot_id}'" return self._executor.execute_query(query) def update_bot_settings(self, bot_settings = BotUpdateModel): row_to_insert = { "id": str(uuid4()), "bot_id": bot_settings['bot_id'], "title": bot_settings['title'], "description": bot_settings["description"], "chat_styles": bot_settings['chat_styles'], "initial_message": bot_settings['initial_message'], "message_suggestion": bot_settings['message_suggestion'], "allow_feedback_response": bot_settings["allow_feedback_response"], "show_source_in_response": bot_settings["show_source_in_response"], "advance_settings": bot_settings["advance_settings"] } errors = client.insert_rows_json(TBL_BOT_SET, [row_to_insert]) if not errors: msg = f"Bot settings for bot {bot_settings['bot_id']} saved successfully." print(msg) return {"message":msg} else: msg = f"Errors occurred while saving bot settings for bot_id: {bot_settings['bot_id']}: {errors}" print(msg) return {"message":msg} # def delete_bot_settings(self, user_id): # # Delete the row from the BigQuery table # delete_condition = f"user_id = '{user_id}'" # errors = client.delete_rows(f"{BIGQUERY_SCHEMA}.bot_settings", delete_condition) # if not errors: # msg = f"UI settings for user {user_id} deleted successfully." # print(msg) # else: # msg = f"Errors occurred while deleting UI settings for user {user_id}: {errors}" # print(msg) # return msg repository = BotRepositoryBigQuery(executor)