feat: AI Worker 平台 MVP v1.0.0 - 多租户/项目管理/AI Worker/任务编排/HITL审核/成本治理
This commit is contained in:
@@ -0,0 +1,51 @@
|
|||||||
|
"""Tenant service: CRUD operations for tenants."""
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from typing import List, Optional
|
||||||
|
from app.models.tenant import Tenant
|
||||||
|
from app.schemas.tenant import TenantCreate, TenantUpdate, TenantResponse
|
||||||
|
|
||||||
|
|
||||||
|
class TenantService:
|
||||||
|
@staticmethod
|
||||||
|
def list_tenants(db: Session, skip: int = 0, limit: int = 100) -> List[Tenant]:
|
||||||
|
return db.query(Tenant).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_tenant(db: Session, tenant_id: int) -> Optional[Tenant]:
|
||||||
|
return db.query(Tenant).filter(Tenant.id == tenant_id).first()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def create_tenant(db: Session, req: TenantCreate) -> Tenant:
|
||||||
|
# Check slug uniqueness
|
||||||
|
existing = db.query(Tenant).filter(Tenant.slug == req.slug).first()
|
||||||
|
if existing:
|
||||||
|
raise ValueError(f"Tenant slug '{req.slug}' already exists")
|
||||||
|
tenant = Tenant(
|
||||||
|
name=req.name,
|
||||||
|
slug=req.slug,
|
||||||
|
description=req.description,
|
||||||
|
)
|
||||||
|
db.add(tenant)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(tenant)
|
||||||
|
return tenant
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def update_tenant(db: Session, tenant_id: int, req: TenantUpdate) -> Tenant:
|
||||||
|
tenant = db.query(Tenant).filter(Tenant.id == tenant_id).first()
|
||||||
|
if not tenant:
|
||||||
|
raise ValueError(f"Tenant #{tenant_id} not found")
|
||||||
|
for field, value in req.model_dump(exclude_unset=True).items():
|
||||||
|
setattr(tenant, field, value)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(tenant)
|
||||||
|
return tenant
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def delete_tenant(db: Session, tenant_id: int) -> bool:
|
||||||
|
tenant = db.query(Tenant).filter(Tenant.id == tenant_id).first()
|
||||||
|
if not tenant:
|
||||||
|
return False
|
||||||
|
db.delete(tenant)
|
||||||
|
db.commit()
|
||||||
|
return True
|
||||||
Reference in New Issue
Block a user