feat: seed initial cards and chest catalog
This commit is contained in:
@@ -2,17 +2,30 @@ from fastapi import FastAPI, Depends, HTTPException
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
from app.database import get_db, engine
|
from app.database import get_db, engine
|
||||||
from app import models
|
from app import models, seed
|
||||||
|
|
||||||
# Create all tables
|
# Create all tables
|
||||||
models.Base.metadata.create_all(bind=engine)
|
models.Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
app = FastAPI(title="Card Game Backend")
|
app = FastAPI(title="Card Game Backend")
|
||||||
|
|
||||||
|
@app.on_event("startup")
|
||||||
|
def startup_event():
|
||||||
|
db = next(get_db())
|
||||||
|
seed.seed_data(db)
|
||||||
|
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
def health_check():
|
def health_check():
|
||||||
return {"status": "ok"}
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
@app.get("/catalog/cards")
|
||||||
|
def get_cards(db: Session = Depends(get_db)):
|
||||||
|
return db.query(models.Card).all()
|
||||||
|
|
||||||
|
@app.get("/catalog/chests")
|
||||||
|
def get_chests(db: Session = Depends(get_db)):
|
||||||
|
return db.query(models.Chest).all()
|
||||||
|
|
||||||
@app.get("/db-check")
|
@app.get("/db-check")
|
||||||
def db_check(db: Session = Depends(get_db)):
|
def db_check(db: Session = Depends(get_db)):
|
||||||
try:
|
try:
|
||||||
|
|||||||
36
backend/app/seed.py
Normal file
36
backend/app/seed.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app import models
|
||||||
|
|
||||||
|
INITIAL_CARDS = [
|
||||||
|
{"name": "Goblin Grunt", "rarity": "COMMON"},
|
||||||
|
{"name": "Elven Archer", "rarity": "COMMON"},
|
||||||
|
{"name": "Orc Warrior", "rarity": "COMMON"},
|
||||||
|
{"name": "Dwarven Shield", "rarity": "COMMON"},
|
||||||
|
{"name": "Healing Potion", "rarity": "COMMON"},
|
||||||
|
{"name": "Fireball Scroll", "rarity": "RARE"},
|
||||||
|
{"name": "Silver Sword", "rarity": "RARE"},
|
||||||
|
{"name": "Mystic Amulet", "rarity": "RARE"},
|
||||||
|
{"name": "Dragon Scale", "rarity": "LEGENDARY"},
|
||||||
|
{"name": "King's Crown", "rarity": "LEGENDARY"},
|
||||||
|
]
|
||||||
|
|
||||||
|
INITIAL_CHESTS = [
|
||||||
|
{"name": "Wooden Chest", "cost_gold": 100, "cards_per_open": 3}
|
||||||
|
]
|
||||||
|
|
||||||
|
def seed_data(db: Session):
|
||||||
|
# Seed Cards
|
||||||
|
for card_data in INITIAL_CARDS:
|
||||||
|
exists = db.query(models.Card).filter_by(name=card_data["name"]).first()
|
||||||
|
if not exists:
|
||||||
|
card = models.Card(**card_data)
|
||||||
|
db.add(card)
|
||||||
|
|
||||||
|
# Seed Chests
|
||||||
|
for chest_data in INITIAL_CHESTS:
|
||||||
|
exists = db.query(models.Chest).filter_by(name=chest_data["name"]).first()
|
||||||
|
if not exists:
|
||||||
|
chest = models.Chest(**chest_data)
|
||||||
|
db.add(chest)
|
||||||
|
|
||||||
|
db.commit()
|
||||||
Reference in New Issue
Block a user