瀏覽代碼

知识库类型管理(添加分页查询知识库类型接口)

liangxunge 1 月之前
父節點
當前提交
45ad0dc236
共有 2 個文件被更改,包括 19 次插入4 次删除
  1. 15 1
      api/controllers/console/tag/tags.py
  2. 4 3
      api/services/tag_service.py

+ 15 - 1
api/controllers/console/tag/tags.py

@@ -1,6 +1,6 @@
 from flask import request
 from flask_login import current_user  # type: ignore
-from flask_restful import Resource, marshal_with, reqparse  # type: ignore
+from flask_restful import Resource, marshal_with, reqparse, marshal  # type: ignore
 from werkzeug.exceptions import Forbidden
 
 from controllers.console import api
@@ -51,6 +51,19 @@ class TagListApi(Resource):
 
         return response, 200
 
+class TagPageListApi(Resource):
+
+    @setup_required
+    @login_required
+    @account_initialization_required
+    def get(self):
+        page = request.args.get("page", default=1, type=int)
+        limit = request.args.get("limit", default=20, type=int)
+        tag_type = request.args.get("tag_type", type=str, default="")
+        tags, total = TagService.get_page_tags(page, limit, tag_type, current_user.current_tenant_id)
+        data = marshal(tags, tag_fields)
+        response = {"data": data, "has_more": len(tags) == limit, "limit": limit, "total": total, "page": page}
+        return response, 200
 
 class TagUpdateDeleteApi(Resource):
     @setup_required
@@ -136,6 +149,7 @@ class TagBindingDeleteApi(Resource):
 
 
 api.add_resource(TagListApi, "/tags")
+api.add_resource(TagPageListApi, "/tags/page")
 api.add_resource(TagUpdateDeleteApi, "/tags/<uuid:tag_id>")
 api.add_resource(TagBindingCreateApi, "/tag-bindings/create")
 api.add_resource(TagBindingDeleteApi, "/tag-bindings/remove")

+ 4 - 3
api/services/tag_service.py

@@ -77,10 +77,11 @@ class TagService:
     @staticmethod
     def get_page_tags(page, per_page, tag_type, tenant_id):
         query = (
-            db.session.query(Tag)
-            .filter(Tag.tenant_id == tenant_id, Tag.type == tag_type)
-            .order_by(Tag.created_at.desc())
+            db.session.query(Tag.id, Tag.type, Tag.name, func.count(TagBinding.id).label("binding_count"))
+            .outerjoin(TagBinding, Tag.id == TagBinding.tag_id)
+            .filter(Tag.type == tag_type, Tag.tenant_id == tenant_id)
         )
+        query = query.group_by(Tag.id, Tag.type, Tag.name)
         tags = query.paginate(page=page, per_page=per_page, error_out=False)
         return tags.items, tags.total