api/app/routers/tenants.py
Yoga Pangestu 7bbe4e7295 Implement subscription, tenant, and user services with CRUD operations and associated tests
- Added subscription_service.py for managing subscriptions with functions to create, read, update, and delete subscriptions.
- Added tenant_service.py for managing tenants with functions to create, read, update, delete tenants, and assign users to tenants.
- Added user_service.py for managing users with functions to create, read, update, and delete users.
- Created tests for business types, plans, subscriptions, tenants, and users, ensuring proper functionality and error handling.
- Implemented pagination in user listing and included user profiles in responses.
- Established fixtures for database setup and teardown in tests.
2026-07-17 23:36:31 +07:00

51 lines
1.5 KiB
Python

from fastapi import APIRouter, Depends, Request, status
from sqlalchemy.orm import Session
from app.database import get_db
from app.responses import created_response
from app.schemas.tenant import TenantCreate, TenantResponse, TenantUpdate
from app.services.tenant_service import (
assign_user_to_tenant,
create_tenant,
delete_tenant,
get_tenant_by_id,
get_tenant_list,
update_tenant,
)
router = APIRouter(prefix="/tenants", tags=["tenants"])
@router.get("/", response_model=list[TenantResponse])
def list_tenants(db: Session = Depends(get_db)):
return get_tenant_list(db)
@router.get("/{id}", response_model=TenantResponse)
def get_tenant(id: str, db: Session = Depends(get_db)):
return get_tenant_by_id(db, id)
@router.post("/", status_code=status.HTTP_201_CREATED)
def create_tenant_route(
request: Request, data: TenantCreate, db: Session = Depends(get_db)
):
tenant = create_tenant(db, data)
location = f"{request.url.path}{tenant.id}"
return created_response(location)
@router.put("/{id}", response_model=TenantResponse)
def update_tenant_route(id: str, data: TenantUpdate, db: Session = Depends(get_db)):
return update_tenant(db, id, data)
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_tenant_route(id: str, db: Session = Depends(get_db)):
delete_tenant(db, id)
@router.post("/{tenant_id}/assign-user")
def assign_user_route(tenant_id: str, user_id: str, db: Session = Depends(get_db)):
return assign_user_to_tenant(db, tenant_id, user_id)