catalog_delete.py 1.9 KB
# coding=utf-8
#author:        4N
#createtime:    2021/3/9
#email:         nheweijun@sina.com



from app.models import ServiceCatalog,db,Service
from app.util.component.ApiTemplate import ApiTemplate
class Api(ApiTemplate):
    api_name = "删除目录"
    def process(self):

    
        # 返回结果
        res = {}
        try:
            # 业务逻辑


            catalog_guid = self.para.get("guid")
    
            catalog =  ServiceCatalog.query.filter_by(guid=catalog_guid).one_or_none()
            if not catalog:
                res["msg"]="目录不存在!"
                return res

            else:

                pguid = catalog.pguid
                # 所有目录
                catalogs = ServiceCatalog.query.filter(ServiceCatalog.path.like("%" + catalog_guid + "%")).all()
                for cata in catalogs:
                    if pguid.__eq__("0"):
                        Service.query.filter_by(catalog_guid=cata.guid).update({"catalog_guid": None})
                        db.session.delete(cata)
                    else:
                        Service.query.filter_by(catalog_guid=cata.guid).update({"catalog_guid": pguid})
                        db.session.delete(cata)
    
                db.session.commit()
                res["msg"] = "目录删除成功!"
                res["result"] = True
        except Exception as e:
            db.session.rollback()
            raise e
        return res
    
    
    api_doc = {
        "tags": ["服务目录接口"],
        "parameters": [
            {"name": "guid",
             "in": "formData",
             "type": "string",
             "description": "目录guid", "required": "true"},
        ],
        "responses": {
            200: {
                "schema": {
                    "properties": {
                    }
                }
            }
        }
    }