Implement a fix for some SQL issues
This commit is contained in:
parent
2b6e4e8b38
commit
e8018e51f7
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,5 @@
|
||||
.venv/
|
||||
.ropeproject/
|
||||
.idea/
|
||||
__pycache__/
|
||||
database.db
|
||||
|
@ -1,10 +1,10 @@
|
||||
from sql import *
|
||||
from sqlmodel import select
|
||||
|
||||
def get_hero_by_email(email: str, session) -> User | None:
|
||||
def get_hero_by_email(email: str, session) -> Hero | None:
|
||||
statement = select(Hero).where(Hero.email == email)
|
||||
return session.exec(statement).first()
|
||||
|
||||
def get_hero_by_id(id: str, session: SessionDep) -> User | None:
|
||||
def get_hero_by_id(id: str, session: SessionDep) -> Hero | None:
|
||||
statement = select(Hero).where(Hero.id == id)
|
||||
return session.exec(statement).first()
|
||||
|
26
main.py
26
main.py
@ -12,7 +12,7 @@ async def lifespan(app: FastAPI):
|
||||
yield # Code before the yield will run on startup, code after yield won't run until the program is over
|
||||
|
||||
# Define the app
|
||||
app = FastAPI()
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
# Routes
|
||||
@app.post("/heroes/create", response_model=HeroPublic)
|
||||
@ -23,13 +23,25 @@ def create_hero(hero: HeroCreate, session: SessionDep):
|
||||
session.refresh(db_hero)
|
||||
return db_hero
|
||||
|
||||
@app.get("/heros/{type}", response_model=HeroPublic)
|
||||
@app.get("/heroes/{type}", response_model=HeroPublic)
|
||||
def get_hero(type: str, session: SessionDep):
|
||||
user = get_hero_by_id(type, session)
|
||||
if user is None:
|
||||
user = get_hero_by_email(type, session)
|
||||
hero = get_hero_by_id(type, session)
|
||||
if hero is None:
|
||||
hero = get_hero_by_email(type, session)
|
||||
|
||||
if not user:
|
||||
if not hero:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
return user
|
||||
return hero
|
||||
|
||||
@app.patch("/heroes/update/{hero_id}/", response_model=HeroPublic)
|
||||
def update_hero(hero_id: int, hero: HeroUpdate, session: SessionDep):
|
||||
hero_db = session.get(Hero, hero_id)
|
||||
if not hero_db:
|
||||
raise HTTPException(status_code=404, detail="Hero not found")
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
hero_db.sqlmodel_update(hero_data)
|
||||
session.add(hero_db)
|
||||
session.commit()
|
||||
session.refresh(hero_db)
|
||||
return hero_db
|
||||
|
19
sql.py
19
sql.py
@ -1,34 +1,33 @@
|
||||
# Imports
|
||||
from typing import Annotated
|
||||
from fastapi import Depends, HTTPException, Query
|
||||
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
||||
from fastapi import Depends
|
||||
from sqlmodel import Field, Session, SQLModel, create_engine
|
||||
|
||||
# The base hero class
|
||||
class Hero(SQLModel):
|
||||
class HeroBase(SQLModel):
|
||||
name: str = Field(index=True)
|
||||
gold: int | None = 0
|
||||
health: int | None = 100
|
||||
email: str = Field(index=True)
|
||||
|
||||
# The player
|
||||
class Player(Hero, table=True):
|
||||
class Hero(HeroBase, table=True):
|
||||
id: int = Field(default=None, primary_key=True, index=True)
|
||||
email: str = Field(index=True)
|
||||
weapons: dict[str, int]
|
||||
pickaxes: dict[str, int]
|
||||
upgrades: dict[str, int]
|
||||
|
||||
# This class will be returned to the public
|
||||
class HeroPublic(Hero):
|
||||
class HeroPublic(HeroBase):
|
||||
id: int
|
||||
|
||||
# The class used to create a hero
|
||||
class HeroCreate(Hero):
|
||||
class HeroCreate(HeroBase):
|
||||
secret_name: str
|
||||
|
||||
# The class used to update a hero
|
||||
class HeroUpdate(Hero):
|
||||
name: str | None = None
|
||||
gold: int | None = 0
|
||||
health: int | None = 0
|
||||
email: str | None = None
|
||||
|
||||
# SQLModel initialization
|
||||
sqlite_file_name = "database.db"
|
||||
|
Loading…
Reference in New Issue
Block a user