2022-10-16 19:50:37 +00:00
|
|
|
from loguru import logger
|
|
|
|
from typing import Type, TypeVar
|
|
|
|
from asyncpg.connection import Connection
|
|
|
|
|
|
|
|
|
|
|
|
class BaseRepo:
|
|
|
|
def __init__(self, conn: Connection):
|
|
|
|
self.conn = conn
|
|
|
|
|
|
|
|
|
2022-10-17 10:58:17 +00:00
|
|
|
T = TypeVar("T", bound=BaseRepo)
|
2022-10-16 19:50:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Repos(BaseRepo):
|
|
|
|
def get_repo(self, Repo: Type[T]) -> T:
|
|
|
|
return Repo(self.conn)
|
|
|
|
|
|
|
|
|
|
|
|
class UserRepo(BaseRepo):
|
|
|
|
async def add(self, user_id: int, chat_id: int):
|
|
|
|
await self.conn.execute(
|
|
|
|
"INSERT INTO users (user_id, chat_id) "
|
|
|
|
"VALUES ($1, $2) ON CONFLICT (user_id) DO NOTHING",
|
2022-10-17 10:58:17 +00:00
|
|
|
user_id,
|
|
|
|
chat_id,
|
2022-10-16 19:50:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def get(self, user_id: int):
|
|
|
|
return await self.conn.fetchval(
|
|
|
|
"SELECT user_id FROM users WHERE user_id = $1", user_id
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectRepo(BaseRepo):
|
2022-10-17 11:17:49 +00:00
|
|
|
async def add(self, *args, user_id: int):
|
2022-10-16 19:50:37 +00:00
|
|
|
await self.conn.execute(
|
|
|
|
"INSERT INTO projects "
|
2022-10-17 11:17:49 +00:00
|
|
|
"(creator, name, description, contacts, "
|
|
|
|
"contributors, status, tag, category) "
|
|
|
|
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
|
|
|
|
user_id, *args
|
2022-10-16 19:50:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def get(self, user_id: int):
|
|
|
|
return await self.conn.fetchval(
|
2022-10-17 10:58:17 +00:00
|
|
|
"SELECT name, description FROM projects WHERE creator = $1", user_id
|
2022-10-16 19:50:37 +00:00
|
|
|
)
|