Spaces:
Sleeping
Sleeping
Upload app/api/seed.py
Browse files- app/api/seed.py +18 -3
app/api/seed.py
CHANGED
|
@@ -4,6 +4,8 @@ from sqlalchemy.orm import Session
|
|
| 4 |
from app.db.base import get_db
|
| 5 |
from app.models.product import Product, ProductImage, Category
|
| 6 |
from app.models.cart import Coupon
|
|
|
|
|
|
|
| 7 |
from app.schemas.auth import AuthResponse
|
| 8 |
|
| 9 |
router = APIRouter(prefix="/seed", tags=["Seed Data"])
|
|
@@ -222,13 +224,26 @@ DEMO_PRODUCTS = [
|
|
| 222 |
|
| 223 |
@router.post("", response_model=AuthResponse)
|
| 224 |
async def seed_database(db: Session = Depends(get_db)):
|
| 225 |
-
"""Populate database with demo categories and
|
| 226 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
existing = db.query(Category).count()
|
| 228 |
if existing > 0:
|
| 229 |
return AuthResponse(
|
| 230 |
isSuccess=True,
|
| 231 |
-
value={"message": "Database already has data.
|
| 232 |
statusCode=200,
|
| 233 |
)
|
| 234 |
|
|
|
|
| 4 |
from app.db.base import get_db
|
| 5 |
from app.models.product import Product, ProductImage, Category
|
| 6 |
from app.models.cart import Coupon
|
| 7 |
+
from app.models.user import User, UserRole, AuthProvider
|
| 8 |
+
from app.core.security import hash_password
|
| 9 |
from app.schemas.auth import AuthResponse
|
| 10 |
|
| 11 |
router = APIRouter(prefix="/seed", tags=["Seed Data"])
|
|
|
|
| 224 |
|
| 225 |
@router.post("", response_model=AuthResponse)
|
| 226 |
async def seed_database(db: Session = Depends(get_db)):
|
| 227 |
+
"""Populate database with demo categories, products, and an admin user."""
|
| 228 |
+
# Always ensure admin user exists (even on re-seed)
|
| 229 |
+
existing_admin = db.query(User).filter(User.email == "admin@vortex.com").first()
|
| 230 |
+
if not existing_admin:
|
| 231 |
+
admin_user = User(
|
| 232 |
+
name="Admin",
|
| 233 |
+
email="admin@vortex.com",
|
| 234 |
+
password_hash=hash_password("admin123"),
|
| 235 |
+
role=UserRole.ADMIN,
|
| 236 |
+
auth_provider=AuthProvider.LOCAL,
|
| 237 |
+
)
|
| 238 |
+
db.add(admin_user)
|
| 239 |
+
db.commit()
|
| 240 |
+
|
| 241 |
+
# Check if products are already seeded
|
| 242 |
existing = db.query(Category).count()
|
| 243 |
if existing > 0:
|
| 244 |
return AuthResponse(
|
| 245 |
isSuccess=True,
|
| 246 |
+
value={"message": "Database already has data. Admin user ensured."},
|
| 247 |
statusCode=200,
|
| 248 |
)
|
| 249 |
|