refactor: DB related -> db.py
This commit is contained in:
parent
dcbb507174
commit
17904468f8
57
db.py
Normal file
57
db.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
from datetime import datetime, timezone
|
||||||
|
from sqlmodel import ARRAY, Column, Relationship, SQLModel, Field, create_engine, String
|
||||||
|
|
||||||
|
with open("db.secrets", "r") as f:
|
||||||
|
db_secrets = f.readline().strip()
|
||||||
|
|
||||||
|
engine = create_engine(db_secrets)
|
||||||
|
del db_secrets
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
user: str
|
||||||
|
love: list[str] = Field(sa_column=Column(ARRAY(String)))
|
||||||
|
hate: list[str] = Field(sa_column=Column(ARRAY(String)))
|
||||||
|
undecided: list[str] = Field(sa_column=Column(ARRAY(String)))
|
||||||
|
|
||||||
|
|
||||||
|
class MVPRanking(SQLModel, table=True):
|
||||||
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
|
time: datetime | None = Field(default_factory=utctime)
|
||||||
|
user: str
|
||||||
|
mvps: list[str] = Field(sa_column=Column(ARRAY(String)))
|
||||||
|
|
||||||
|
|
||||||
|
SQLModel.metadata.create_all(engine)
|
64
main.py
64
main.py
@ -1,25 +1,14 @@
|
|||||||
from datetime import datetime, timezone
|
|
||||||
from fastapi import APIRouter, FastAPI, status
|
from fastapi import APIRouter, FastAPI, status
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
from db import Player, Team, Chemistry, MVPRanking, engine
|
||||||
from sqlmodel import (
|
from sqlmodel import (
|
||||||
ARRAY,
|
|
||||||
Column,
|
|
||||||
Relationship,
|
|
||||||
SQLModel,
|
|
||||||
Field,
|
|
||||||
Session,
|
Session,
|
||||||
String,
|
|
||||||
create_engine,
|
|
||||||
select,
|
select,
|
||||||
)
|
)
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
with open("db.secrets", "r") as f:
|
|
||||||
db_secrets = f.readline().strip()
|
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(title="cutt")
|
app = FastAPI(title="cutt")
|
||||||
engine = create_engine(db_secrets)
|
|
||||||
origins = [
|
origins = [
|
||||||
"*",
|
"*",
|
||||||
"http://localhost",
|
"http://localhost",
|
||||||
@ -36,55 +25,6 @@ app.add_middleware(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
user: str
|
|
||||||
love: list[str] = Field(sa_column=Column(ARRAY(String)))
|
|
||||||
hate: list[str] = Field(sa_column=Column(ARRAY(String)))
|
|
||||||
undecided: list[str] = Field(sa_column=Column(ARRAY(String)))
|
|
||||||
|
|
||||||
|
|
||||||
class MVPRanking(SQLModel, table=True):
|
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
|
||||||
time: datetime | None = Field(default_factory=utctime)
|
|
||||||
user: str
|
|
||||||
mvps: list[str] = Field(sa_column=Column(ARRAY(String)))
|
|
||||||
|
|
||||||
|
|
||||||
SQLModel.metadata.create_all(engine)
|
|
||||||
|
|
||||||
|
|
||||||
def add_team(team: Team):
|
def add_team(team: Team):
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
session.add(team)
|
session.add(team)
|
||||||
@ -113,7 +53,7 @@ def list_players():
|
|||||||
def list_teams():
|
def list_teams():
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
statement = select(Team)
|
statement = select(Team)
|
||||||
return session.exec(statement)
|
return session.exec(statement).fetchall()
|
||||||
|
|
||||||
|
|
||||||
player_router = APIRouter(prefix="/player")
|
player_router = APIRouter(prefix="/player")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user