Spaces:
Sleeping
Sleeping
Upload app/api/settings.py
Browse files- app/api/settings.py +47 -0
app/api/settings.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException, status
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
+
from typing import Any
|
| 4 |
+
|
| 5 |
+
from app.db.base import get_db
|
| 6 |
+
from app.models.settings import StoreSettings
|
| 7 |
+
from app.schemas.settings import StoreSettingsResponse, StoreSettingsUpdate
|
| 8 |
+
from app.api.auth import get_current_admin_user
|
| 9 |
+
from app.schemas.api_response import APIResponse
|
| 10 |
+
|
| 11 |
+
router = APIRouter(prefix="/settings", tags=["Settings"])
|
| 12 |
+
|
| 13 |
+
@router.get("/", response_model=APIResponse)
|
| 14 |
+
def get_settings(db: Session = Depends(get_db)) -> Any:
|
| 15 |
+
settings = db.query(StoreSettings).first()
|
| 16 |
+
if not settings:
|
| 17 |
+
settings = StoreSettings() # create default
|
| 18 |
+
db.add(settings)
|
| 19 |
+
db.commit()
|
| 20 |
+
db.refresh(settings)
|
| 21 |
+
return APIResponse(
|
| 22 |
+
isSuccess=True,
|
| 23 |
+
value=StoreSettingsResponse.from_orm(settings).dict(),
|
| 24 |
+
statusCode=200
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
@router.put("/", response_model=APIResponse)
|
| 28 |
+
def update_settings(
|
| 29 |
+
settings_in: StoreSettingsUpdate,
|
| 30 |
+
db: Session = Depends(get_db),
|
| 31 |
+
current_admin=Depends(get_current_admin_user)
|
| 32 |
+
) -> Any:
|
| 33 |
+
settings = db.query(StoreSettings).first()
|
| 34 |
+
if not settings:
|
| 35 |
+
settings = StoreSettings(**settings_in.dict())
|
| 36 |
+
db.add(settings)
|
| 37 |
+
else:
|
| 38 |
+
for field, value in settings_in.dict().items():
|
| 39 |
+
setattr(settings, field, value)
|
| 40 |
+
|
| 41 |
+
db.commit()
|
| 42 |
+
db.refresh(settings)
|
| 43 |
+
return APIResponse(
|
| 44 |
+
isSuccess=True,
|
| 45 |
+
value=StoreSettingsResponse.from_orm(settings).dict(),
|
| 46 |
+
statusCode=200
|
| 47 |
+
)
|