|
from sqlalchemy import Column, Integer, String |
|
from sqlalchemy.ext.asyncio import AsyncSession |
|
from sqlalchemy.orm import declarative_base |
|
|
|
Base = declarative_base() |
|
|
|
class Team(Base): |
|
__tablename__ = "teams" |
|
|
|
id = Column(Integer, primary_key=True) |
|
name = Column(String, unique=True) |
|
|
|
async def create(self): |
|
async with AsyncSession() as session: |
|
session.add(self) |
|
await session.commit() |
|
return self |
|
|
|
async def read_all(): |
|
async with AsyncSession() as session: |
|
return await session.execute(select(Team)).scalars().all() |
|
|
|
async def read_one(team_id: int): |
|
async with AsyncSession() as session: |
|
return await session.get(Team, team_id) |
|
|
|
async def update(self, team_id: int, team: Team): |
|
async with AsyncSession() as session: |
|
team = await session.get(Team, team_id) |
|
team.name = team.name |
|
await session.commit() |
|
return team |
|
|
|
async def delete(team_id: int): |
|
async with AsyncSession() as session: |
|
team = await session.get(Team, team_id) |
|
await session.delete(team) |
|
await session.commit() |