136 lines
4.8 KiB
Python
136 lines
4.8 KiB
Python
"""Wiki plugin routes — articles CRUD, categories, versioning."""
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.audit import log_audit
|
|
from app.core.db import get_db
|
|
from app.deps import require_permission
|
|
from app.plugins.builtins.wiki import services
|
|
from app.plugins.builtins.wiki.schemas import (
|
|
ArticleCreate,
|
|
ArticleUpdate,
|
|
CategoryCreate,
|
|
)
|
|
|
|
router = APIRouter(prefix="/api/v1/wiki", tags=["wiki"])
|
|
|
|
|
|
@router.get("/articles")
|
|
async def list_articles(
|
|
page: int = Query(1, ge=1),
|
|
page_size: int = Query(20, ge=1, le=100),
|
|
category_id: str | None = None,
|
|
status: str | None = None,
|
|
search: str | None = None,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:read")),
|
|
):
|
|
return await services.list_articles(
|
|
db, uuid.UUID(current_user["tenant_id"]),
|
|
page=page, page_size=page_size, category_id=category_id, status=status, search=search,
|
|
)
|
|
|
|
|
|
@router.post("/articles", status_code=status.HTTP_201_CREATED)
|
|
async def create_article(
|
|
body: ArticleCreate,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:write")),
|
|
):
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
user_id = uuid.UUID(current_user["user_id"])
|
|
result = await services.create_article(db, tenant_id, user_id, body.model_dump())
|
|
if result and result.get("id"):
|
|
await log_audit(db, tenant_id, user_id, "create", "wiki_article", uuid.UUID(result["id"]), changes={"title": body.title})
|
|
return result
|
|
|
|
|
|
@router.get("/articles/{article_id}")
|
|
async def get_article(
|
|
article_id: str,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:read")),
|
|
):
|
|
result = await services.get_article(db, uuid.UUID(current_user["tenant_id"]), uuid.UUID(article_id))
|
|
if result is None:
|
|
raise HTTPException(status_code=404, detail={"detail": "Article not found", "code": "not_found"})
|
|
return result
|
|
|
|
|
|
@router.patch("/articles/{article_id}")
|
|
async def update_article(
|
|
article_id: str,
|
|
body: ArticleUpdate,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:write")),
|
|
):
|
|
result = await services.update_article(
|
|
db, uuid.UUID(current_user["tenant_id"]), uuid.UUID(current_user["user_id"]),
|
|
uuid.UUID(article_id), body.model_dump(exclude_unset=True),
|
|
)
|
|
if result is None:
|
|
raise HTTPException(status_code=404, detail={"detail": "Article not found", "code": "not_found"})
|
|
return result
|
|
|
|
|
|
@router.delete("/articles/{article_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_article(
|
|
article_id: str,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:delete")),
|
|
):
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
user_id = uuid.UUID(current_user["user_id"])
|
|
ok = await services.delete_article(db, tenant_id, uuid.UUID(article_id))
|
|
if not ok:
|
|
raise HTTPException(status_code=404, detail={"detail": "Article not found", "code": "not_found"})
|
|
await log_audit(db, tenant_id, user_id, "delete", "wiki_article", uuid.UUID(article_id))
|
|
|
|
|
|
@router.get("/articles/{article_id}/versions")
|
|
async def list_versions(
|
|
article_id: str,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:read")),
|
|
):
|
|
return {"items": await services.list_versions(db, uuid.UUID(current_user["tenant_id"]), uuid.UUID(article_id))}
|
|
|
|
|
|
@router.post("/articles/{article_id}/versions/{version}/restore")
|
|
async def restore_version(
|
|
article_id: str,
|
|
version: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:write")),
|
|
):
|
|
result = await services.restore_version(
|
|
db, uuid.UUID(current_user["tenant_id"]), uuid.UUID(current_user["user_id"]),
|
|
uuid.UUID(article_id), version,
|
|
)
|
|
if result is None:
|
|
raise HTTPException(status_code=404, detail={"detail": "Version not found", "code": "not_found"})
|
|
return result
|
|
|
|
|
|
@router.get("/categories")
|
|
async def list_categories(
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:read")),
|
|
):
|
|
return {"items": await services.list_categories(db, uuid.UUID(current_user["tenant_id"]))}
|
|
|
|
|
|
@router.post("/categories", status_code=status.HTTP_201_CREATED)
|
|
async def create_category(
|
|
body: CategoryCreate,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:write")),
|
|
):
|
|
return await services.create_category(
|
|
db, uuid.UUID(current_user["tenant_id"]), uuid.UUID(current_user["user_id"]), body.model_dump(),
|
|
)
|