60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
# Imports
|
|
from typing import Annotated
|
|
from fastapi import Depends
|
|
from sqlmodel import Field, Session, SQLModel, create_engine
|
|
from pydantic import BaseModel
|
|
|
|
# The base hero class
|
|
class HeroBase(SQLModel):
|
|
name: str
|
|
gold: int | None = Field(default=0)
|
|
health: int | None = Field(default=100)
|
|
|
|
# The main hero class. It inherits the name, gold, and health
|
|
class Hero(HeroBase, table=True):
|
|
id: int | None = Field(default=None, primary_key=True, index=True)
|
|
email: str = Field(index=True, unique=True)
|
|
password: str
|
|
salt: int
|
|
|
|
# The class that will be returned to the public. It will return the id, name, gold, and health
|
|
class HeroPublic(HeroBase):
|
|
id: int
|
|
|
|
# The stuff required to create an account, you need an email, name, and password
|
|
class HeroCreate(HeroBase):
|
|
email: str
|
|
password: str
|
|
salt: int | None = Field(default=0)
|
|
|
|
# The stuff needed to update a user
|
|
class HeroUpdate(HeroBase):
|
|
name: str | None = None
|
|
email : str | None = None
|
|
gold: int | None = None
|
|
health: int | None = None
|
|
|
|
# The class for verifying heroes
|
|
class VerifyHero(BaseModel):
|
|
email: str
|
|
password: str
|
|
|
|
# SQLModel initialization
|
|
sqlite_file_name = "../database.db"
|
|
sqlite_url = f"sqlite:///{sqlite_file_name}"
|
|
|
|
connect_args = {"check_same_thread": False}
|
|
engine = create_engine(sqlite_url, connect_args=connect_args)
|
|
|
|
# Create DB
|
|
def create_db_and_tables():
|
|
SQLModel.metadata.create_all(engine)
|
|
|
|
# Get the session
|
|
def get_session():
|
|
with Session(engine) as session:
|
|
yield session
|
|
|
|
# Return the SessionDep
|
|
SessionDep = Annotated[Session, Depends(get_session)]
|