39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.auth import get_current_admin
|
|
from app.database import get_db
|
|
from app import models, schemas
|
|
|
|
router = APIRouter(prefix="/api/brand", tags=["Brand"])
|
|
|
|
|
|
def _get_or_create_brand(db: Session) -> models.Brand:
|
|
brand = db.get(models.Brand, 1)
|
|
if not brand:
|
|
brand = models.Brand(id=1)
|
|
db.add(brand)
|
|
db.commit()
|
|
db.refresh(brand)
|
|
return brand
|
|
|
|
|
|
@router.get("", response_model=schemas.BrandOut)
|
|
def get_brand(db: Session = Depends(get_db)):
|
|
"""اطلاعات کلی برند (نام، لوگو، تلفن، ایمیل، آدرس و...) — یک رکورد ثابت."""
|
|
return _get_or_create_brand(db)
|
|
|
|
|
|
@router.put("", response_model=schemas.BrandOut)
|
|
def update_brand(
|
|
payload: schemas.BrandUpdate,
|
|
db: Session = Depends(get_db),
|
|
_admin=Depends(get_current_admin),
|
|
):
|
|
brand = _get_or_create_brand(db)
|
|
for field, value in payload.model_dump(exclude_unset=True).items():
|
|
setattr(brand, field, value)
|
|
db.commit()
|
|
db.refresh(brand)
|
|
return brand
|