diff --git a/backend/app/main.py b/backend/app/main.py index 7d52dc8..499a7a7 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -2,17 +2,30 @@ from fastapi import FastAPI, Depends, HTTPException from sqlalchemy.orm import Session from sqlalchemy import text from app.database import get_db, engine -from app import models +from app import models, seed # Create all tables models.Base.metadata.create_all(bind=engine) app = FastAPI(title="Card Game Backend") +@app.on_event("startup") +def startup_event(): + db = next(get_db()) + seed.seed_data(db) + @app.get("/health") def health_check(): 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") def db_check(db: Session = Depends(get_db)): try: diff --git a/backend/app/seed.py b/backend/app/seed.py new file mode 100644 index 0000000..d25e178 --- /dev/null +++ b/backend/app/seed.py @@ -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()