common update

This commit is contained in:
2025-06-18 11:48:41 +07:00
parent 88c4ee362e
commit 047290e2ea
27 changed files with 1533 additions and 87 deletions
+35
View File
@@ -0,0 +1,35 @@
from supabase import create_client, Client
import os
from dotenv import load_dotenv
from infra.db import PostgresConnection
load_dotenv()
class Database:
def __init__(self):
uri: str = os.getenv('SUPABASE_URI')
if not uri:
raise ValueError("SUPABASE_URI environment variable is not set")
# Parse URI to get URL and key
# Format: postgresql://postgres:[YOUR-PASSWORD]@db.[YOUR-PROJECT-REF].supabase.co:5432/postgres
parts = uri.split('@')
if len(parts) != 2:
raise ValueError("Invalid SUPABASE_URI format")
# Extract project reference from the host
host = parts[1].split(':')[0]
project_ref = host.split('.')[1]
# Construct Supabase URL and key
url = f"https://{project_ref}.supabase.co"
key = os.getenv('SUPABASE_KEY') # Still need the anon key for API access
if not key:
raise ValueError("SUPABASE_KEY environment variable is not set")
self.supabase: Client = create_client(url, key)
class BaseRepository:
def __init__(self):
self.db = PostgresConnection()
+45
View File
@@ -0,0 +1,45 @@
from typing import Optional, List
from .base import BaseRepository
from models.channel import DiscordChannel
class ChannelRepository(BaseRepository):
def __init__(self):
super().__init__()
self.table = self.db.get_table('discord_channel')
async def get_channel(self, channel_id: int) -> Optional[DiscordChannel]:
try:
response = self.table.select('*').eq('channel_id', channel_id).execute()
if response.data:
return DiscordChannel(**response.data[0])
return None
except Exception as e:
print(f"Error getting channel: {e}")
return None
async def get_channels_by_server(self, server_id: int) -> List[DiscordChannel]:
try:
response = self.table.select('*').eq('server_id', server_id).execute()
return [DiscordChannel(**channel) for channel in response.data]
except Exception as e:
print(f"Error getting channels: {e}")
return []
async def create_channel(self, channel_id: int, server_id: int, channel_name: str) -> Optional[DiscordChannel]:
try:
channel = DiscordChannel(channel_id=channel_id, server_id=server_id, channel_name=channel_name)
response = self.table.insert(channel.dict()).execute()
return DiscordChannel(**response.data[0])
except Exception as e:
print(f"Error creating channel: {e}")
return None
async def update_channel(self, channel_id: int, channel_name: str) -> Optional[DiscordChannel]:
try:
response = self.table.update({'channel_name': channel_name}).eq('channel_id', channel_id).execute()
if response.data:
return DiscordChannel(**response.data[0])
return None
except Exception as e:
print(f"Error updating channel: {e}")
return None
+43
View File
@@ -0,0 +1,43 @@
from typing import Optional, List
from .base import BaseRepository
from models.channel_app import DiscordChannelApp
class ChannelAppRepository(BaseRepository):
def __init__(self):
super().__init__()
self.table = self.db.get_table('discord_channel_app')
async def get_channel_app(self, channel_id: int, app_name: str) -> Optional[DiscordChannelApp]:
try:
response = self.table.select('*').eq('channel_id', channel_id).eq('app_name', app_name).execute()
if response.data:
return DiscordChannelApp(**response.data[0])
return None
except Exception as e:
print(f"Error getting channel app: {e}")
return None
async def get_channel_apps(self, channel_id: int) -> List[DiscordChannelApp]:
try:
response = self.table.select('*').eq('channel_id', channel_id).execute()
return [DiscordChannelApp(**app) for app in response.data]
except Exception as e:
print(f"Error getting channel apps: {e}")
return []
async def create_channel_app(self, channel_id: int, app_name: str) -> Optional[DiscordChannelApp]:
try:
app = DiscordChannelApp(channel_id=channel_id, app_name=app_name)
response = self.table.insert(app.dict()).execute()
return DiscordChannelApp(**response.data[0])
except Exception as e:
print(f"Error creating channel app: {e}")
return None
async def delete_channel_app(self, channel_id: int, app_name: str) -> bool:
try:
response = self.table.delete().eq('channel_id', channel_id).eq('app_name', app_name).execute()
return len(response.data) > 0
except Exception as e:
print(f"Error deleting channel app: {e}")
return False
+40
View File
@@ -0,0 +1,40 @@
from typing import Optional
from .base import BaseRepository
from models.server import DiscordServer
class ServerRepository(BaseRepository):
def __init__(self):
super().__init__()
self.table = self.db.get_table('discord_server')
async def get_server(self, server_id: int) -> Optional[DiscordServer]:
"""Get Discord server by ID"""
try:
response = self.table.select('*').eq('server_id', server_id).execute()
if response.data:
return DiscordServer(**response.data[0])
return None
except Exception as e:
print(f"Error getting server: {e}")
return None
async def create_server(self, server_id: int, server_name: str) -> Optional[DiscordServer]:
"""Create new Discord server"""
try:
server = DiscordServer(server_id=server_id, server_name=server_name)
response = self.table.insert(server.dict()).execute()
return DiscordServer(**response.data[0])
except Exception as e:
print(f"Error creating server: {e}")
return None
async def update_server(self, server_id: int, server_name: str) -> Optional[DiscordServer]:
"""Update Discord server"""
try:
response = self.table.update({'server_name': server_name}).eq('server_id', server_id).execute()
if response.data:
return DiscordServer(**response.data[0])
return None
except Exception as e:
print(f"Error updating server: {e}")
return None