Skip to content

Commit

Permalink
Merge pull request #5 from alquimistas-org/models
Browse files Browse the repository at this point in the history
feat: adding database models
  • Loading branch information
natilou authored Sep 5, 2023
2 parents e5968ca + d23b832 commit db80e9b
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# sqlite database
sqlite.db
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ name = "pypi"
[packages]
fastapi = "~= 0.101"
uvicorn = {extras = ["standard"], version = "~= 0.23"}
sqlalchemy = "~=2.0"

[dev-packages]
pytest = "*"
Expand Down
115 changes: 114 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from fastapi import FastAPI

from models import Base
from service.global_results_service import GlobalResultsService
from sql_alchemy_repository.global_results_repository_sql_alchemy import GlobalResultsSQLAlchemy
from sql_conf.database import engine

app = FastAPI()
Base.metadata.create_all(bind=engine)


@app.get("/global_results")
Expand Down
85 changes: 85 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from datetime import date

from sqlalchemy import CheckConstraint, Column
from sqlalchemy import Enum as SqlAlchemyEnum
from sqlalchemy import ForeignKey, Table
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship

from utils import Status


class Base(DeclarativeBase):
...


class User(Base):
__tablename__ = "users"

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str]
last_name: Mapped[str]
email: Mapped[str] = mapped_column(unique=True)
active: Mapped[bool] = mapped_column(default=False)


# TODO: create admin user


class Metric(Base):
__tablename__ = "metrics"

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(unique=True)


class Challenge(Base):
__tablename__ = "challenges"

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
user_challenger: Mapped["User"] = relationship(back_populates="challenges")
user_challenged: Mapped["User"] = relationship(back_populates="challenges")
tournament: Mapped["Tournament"] = relationship(back_populates="challenges")


class Tournament(Base):
__tablename__ = "tournaments"

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(unique=True)
start_date: Mapped[date]
end_date: Mapped[date]
status: Mapped[str] = mapped_column(SqlAlchemyEnum(Status), default=Status.ACTIVE)
CheckConstraint("start_date < end_date", name="check_start_end_dates")


association_user_and_metric_history = Table(
"association_user_and_metric_history",
Base.metadata,
Column("metric_daily_history_id", ForeignKey("metrics_daily_history.id")),
Column("user_id", ForeignKey("users.id")),
)

association_metric_and_metric_history = Table(
"association_metric_and_metric_history",
Base.metadata,
Column("metric_daily_history_id", ForeignKey("metrics_daily_history.id")),
Column("metric_id", ForeignKey("metrics.id")),
)

association_challenge_and_metric_history = Table(
"association_challenge_and_metric_history",
Base.metadata,
Column("metric_daily_history_id", ForeignKey("metrics_daily_history.id")),
Column("challenge_id", ForeignKey("challenges.id")),
)


class MetricDailyHistory(Base):
__tablename__ = "metrics_daily_history"

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
user_id: Mapped[list["User"]] = relationship(secondary=association_user_and_metric_history)
metric_id: Mapped[list["Metric"]] = relationship(secondary=association_metric_and_metric_history)
score: Mapped[int]
date: Mapped[date]
challenge_id: Mapped[list["Challenge"]] = relationship(secondary=association_challenge_and_metric_history)
10 changes: 10 additions & 0 deletions sql_conf/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# TODO: configure mysql engine with env viriable from docker compose
SQLALCHEMY_DATABASE_URL = os.getenv("DB_URL", "sqlite:///./sqlite.db")

engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
6 changes: 6 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import enum


class Status(enum.Enum):
ACTIVE = "active"
COMPLETED = "completed"

0 comments on commit db80e9b

Please sign in to comment.