File size: 1,195 Bytes
ba19a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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()