Files
MAIA/backend/modules/admin/api.py
2025-04-22 00:10:57 +02:00

46 lines
1.6 KiB
Python

# modules/admin/api.py
from typing import Annotated
from fastapi import APIRouter, Depends, Body # Import Body
from pydantic import BaseModel # Import BaseModel
from sqlalchemy.orm import Session
from core.database import Base, get_db
from modules.auth.models import User, UserRole
from modules.auth.dependencies import admin_only
router = APIRouter(prefix="/admin", tags=["admin"], dependencies=[Depends(admin_only)])
# Define a Pydantic model for the request body
class ClearDbRequest(BaseModel):
hard: bool
@router.get("/")
def read_admin():
return {"message": "Admin route"}
# Change to POST and use the request body model
@router.post("/cleardb")
def clear_db(payload: ClearDbRequest, db: Annotated[Session, Depends(get_db)]):
"""
Clear the database based on the 'hard' flag in the request body.
'hard'=True: Drop and recreate all tables.
'hard'=False: Delete data from tables except users.
"""
hard = payload.hard # Get 'hard' from the payload
if hard:
# ... existing hard clear logic ...
Base.metadata.drop_all(bind=db.get_bind())
Base.metadata.create_all(bind=db.get_bind())
db.commit()
return {"message": "Database reset (HARD)"}
else:
# ... existing soft clear logic ...
tables = Base.metadata.tables.keys()
for table_name in tables:
# delete all tables that isn't the users table
if table_name != "users":
table = Base.metadata.tables[table_name]
print(f"Deleting table: {table_name}")
db.execute(table.delete())
db.commit()
return {"message": "Database cleared"}