36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
from fastapi import APIRouter, Depends, Body
|
|
from sqlalchemy.orm import Session
|
|
|
|
from database import get_db
|
|
from models import Organization
|
|
from responses import ok, fail
|
|
|
|
router = APIRouter(tags=["组织"])
|
|
|
|
|
|
@router.get("/organizations")
|
|
def list_orgs(page: int = 1, limit: int = 20, db: Session = Depends(get_db)):
|
|
q = db.query(Organization)
|
|
total = q.count()
|
|
items = (
|
|
q.order_by(Organization.created_at.desc())
|
|
.offset((page - 1) * limit)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
return ok({"data": [o.to_dict() for o in items], "total": total})
|
|
|
|
|
|
@router.post("/organizations")
|
|
def create_org(payload: dict = Body(...), db: Session = Depends(get_db)):
|
|
o = Organization(
|
|
name=payload.get("name", "未命名组织"),
|
|
description=payload.get("desc", "") or payload.get("description", ""),
|
|
emoji=payload.get("emoji", "🏢"),
|
|
org_type=payload.get("type", "") or payload.get("orgType", "team"),
|
|
)
|
|
db.add(o)
|
|
db.commit()
|
|
db.refresh(o)
|
|
return ok(o.to_dict())
|