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
+44
View File
@@ -0,0 +1,44 @@
from typing import Optional, List
from models.channel import DiscordChannel
from infra.db import postgres
class ChannelRepository:
def __init__(self):
self.table = postgres.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, server_id: int, channel_id: int, app: str) -> Optional[DiscordChannel]:
try:
channel = DiscordChannel(server_id=server_id, channel_id=channel_id, app=app)
response = self.table.insert(channel.dict(exclude_none=True)).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, app: str) -> Optional[DiscordChannel]:
try:
response = self.table.update({'app': app}).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