From 84d6c5bd326ffeee9698ff8fc0eb96a5c635414a Mon Sep 17 00:00:00 2001 From: huangzhuang_3rd Date: Wed, 12 Aug 2026 13:31:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20AI=20Worker=20=E5=B9=B3=E5=8F=B0=20MVP?= =?UTF-8?q?=20v1.0.0=20-=20=E5=A4=9A=E7=A7=9F=E6=88=B7/=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=AE=A1=E7=90=86/AI=20Worker/=E4=BB=BB=E5=8A=A1=E7=BC=96?= =?UTF-8?q?=E6=8E=92/HITL=E5=AE=A1=E6=A0=B8/=E6=88=90=E6=9C=AC=E6=B2=BB?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/services/tenant_service.py | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 backend/app/services/tenant_service.py diff --git a/backend/app/services/tenant_service.py b/backend/app/services/tenant_service.py new file mode 100644 index 0000000..f5eab09 --- /dev/null +++ b/backend/app/services/tenant_service.py @@ -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