MCPcopy Create free account
hub / github.com/53AI/53AIHub / BatchSubscriptionOperation

Function BatchSubscriptionOperation

api/controller/subscription.go:100–335  ·  view source on GitHub ↗

@Summary Batch operation for subscription settings @Description Batch create, update or delete subscription settings @Tags Subscription @Accept json @Produce json @Security BearerAuth @Param request body BatchSubscriptionRequest true "Batch operation request" @Success 200 {object} model.CommonRespon

(c *gin.Context)

Source from the content-addressed store, hash-verified

98// @Success 200 {object} model.CommonResponse "Success"
99// @Router /api/subscriptions/batch [post]
100func BatchSubscriptionOperation(c *gin.Context) {
101 var req BatchSubscriptionRequest
102 if err := c.ShouldBindJSON(&req); err != nil {
103 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err))
104 return
105 }
106
107 // Start transaction
108 tx := model.DB.Begin()
109 if tx.Error != nil {
110 c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(tx.Error))
111 return
112 }
113
114 // 添加循环依赖检查逻辑
115 dependencyMap := make(map[int64]int64) // 用于记录 GroupId -> TargetGroupId 的映射
116 for _, item := range req.Items {
117 if item.Delete && item.GroupId > 0 && item.TargetGroupId > 0 {
118 dependencyMap[item.GroupId] = item.TargetGroupId
119 }
120 }
121
122 // 检测循环依赖
123 for groupId := range dependencyMap {
124 visited := make(map[int64]bool) // 用于记录访问过的节点
125 current := groupId
126
127 for {
128 if visited[current] {
129 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(fmt.Errorf("检测到用户迁移循环,group_id=%d 存在循环", groupId)))
130 return
131 }
132
133 visited[current] = true
134
135 // 检查是否有下一个目标
136 next, exists := dependencyMap[current]
137 if !exists {
138 break // 如果没有下一个目标,说明没有循环
139 }
140
141 current = next
142 }
143 }
144
145 eid := config.GetEID(c)
146 userId := config.GetUserId(c)
147
148 var isUpdate bool
149 for _, item := range req.Items {
150 // 检查是否为默认订阅
151 var setting model.SubscriptionSetting
152 if err := tx.Where("setting_id = ?", item.SettingId).First(&setting).Error; err == nil && setting.IsDefault {
153 if item.Delete {
154 tx.Rollback()
155 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(fmt.Errorf("默认订阅不可删除")))
156 return
157 }

Callers

nothing calls this directly

Calls 7

ToResponseMethod · 0.80
ErrorfMethod · 0.80
GetEIDMethod · 0.80
UpdateMethod · 0.65
DeleteMethod · 0.65
SaveMethod · 0.65
CreateMethod · 0.45

Tested by

no test coverage detected