feat: players and teams in DB and API
This commit is contained in:
parent
2d1b1491b0
commit
10b6ebebe7
69
main.py
69
main.py
@ -4,6 +4,7 @@ from fastapi.staticfiles import StaticFiles
|
||||
from sqlmodel import (
|
||||
ARRAY,
|
||||
Column,
|
||||
Relationship,
|
||||
SQLModel,
|
||||
Field,
|
||||
Session,
|
||||
@ -39,6 +40,32 @@ def utctime():
|
||||
return datetime.now(tz=timezone.utc)
|
||||
|
||||
|
||||
class PlayerTeamLink(SQLModel, table=True):
|
||||
team_id: int | None = Field(default=None, foreign_key="team.id", primary_key=True)
|
||||
player_id: int | None = Field(
|
||||
default=None, foreign_key="player.id", primary_key=True
|
||||
)
|
||||
|
||||
|
||||
class Team(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
name: str
|
||||
location: str | None
|
||||
country: str | None
|
||||
players: list["Player"] | None = Relationship(
|
||||
back_populates="teams", link_model=PlayerTeamLink
|
||||
)
|
||||
|
||||
|
||||
class Player(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
name: str
|
||||
number: str | None = None
|
||||
teams: list[Team] | None = Relationship(
|
||||
back_populates="players", link_model=PlayerTeamLink
|
||||
)
|
||||
|
||||
|
||||
class Chemistry(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
time: datetime | None = Field(default_factory=utctime)
|
||||
@ -58,6 +85,46 @@ class MVPRanking(SQLModel, table=True):
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
|
||||
def add_team(team: Team):
|
||||
with Session(engine) as session:
|
||||
session.add(team)
|
||||
session.commit()
|
||||
|
||||
|
||||
def add_player(player: Player):
|
||||
with Session(engine) as session:
|
||||
session.add(player)
|
||||
session.commit()
|
||||
|
||||
|
||||
def add_players(players: list[Player]):
|
||||
with Session(engine) as session:
|
||||
for player in players:
|
||||
session.add(player)
|
||||
session.commit()
|
||||
|
||||
|
||||
def list_players():
|
||||
with Session(engine) as session:
|
||||
statement = select(Player)
|
||||
return session.exec(statement).fetchall()
|
||||
|
||||
|
||||
def list_teams():
|
||||
with Session(engine) as session:
|
||||
statement = select(Team)
|
||||
return session.exec(statement)
|
||||
|
||||
|
||||
player_router = APIRouter(prefix="/player")
|
||||
player_router.add_api_route("/list", endpoint=list_players, methods=["GET"])
|
||||
player_router.add_api_route("/add", endpoint=add_player, methods=["POST"])
|
||||
|
||||
team_router = APIRouter(prefix="/team")
|
||||
team_router.add_api_route("/list", endpoint=list_teams, methods=["GET"])
|
||||
team_router.add_api_route("/add", endpoint=add_team, methods=["POST"])
|
||||
|
||||
|
||||
@app.post("/mvps/", status_code=status.HTTP_200_OK)
|
||||
def submit_mvps(mvps: MVPRanking):
|
||||
with Session(engine) as session:
|
||||
@ -72,4 +139,6 @@ def submit_chemistry(chemistry: Chemistry):
|
||||
session.commit()
|
||||
|
||||
|
||||
app.include_router(player_router)
|
||||
app.include_router(team_router)
|
||||
app.mount("/", StaticFiles(directory="dist", html=True), name="site")
|
||||
|
Loading…
x
Reference in New Issue
Block a user