package controllers import ( "fmt" "github.com/gin-gonic/gin" ) type JsonStruct struct { Code int `json:"code"` Msg interface{} `json:"msg"` Data interface{} `json:"data"` } type JsonErrStruct struct { Code int `json:"code"` Msg interface{} `json:"msg"` Details interface{} `json:"details,omitempty"` } func ReturnSuccess(ctx *gin.Context, code int, msg interface{}, data interface{}) { json := &JsonStruct{ Code: code, Msg: msg, Data: data, } ctx.JSON(200, json) } func ReturnError(ctx *gin.Context, code int, msg interface{}, details interface{}) { json := &JsonErrStruct{ Code: code, Msg: msg, Details: details, } ctx.JSON(code, json) } // BuildBatchOperationResponse creates a structured response map and summary message func BuildBatchOperationResponse(action string, resourceType string, succeededItems []map[string]interface{}, failedItems []map[string]interface{}, ) (map[string]interface{}, string) { overallStatus := "" summaryMsg := "" totalRequested := len(succeededItems) + len(failedItems) if len(failedItems) == 0 && totalRequested > 0 { overallStatus = "accepted" summaryMsg = fmt.Sprintf("%s control <%s> accepted for all %d requested %s.", resourceType, action, totalRequested, resourceType) // Making it more generic } else if len(succeededItems) == 0 && totalRequested > 0 { overallStatus = "failed" summaryMsg = fmt.Sprintf("%s control <%s> failed for all %d requested %s.", resourceType, action, totalRequested, resourceType) } else if totalRequested > 0 { overallStatus = "partially_accepted" summaryMsg = fmt.Sprintf("%s control <%s> partially accepted: %d received, %d failed.", resourceType, action, len(succeededItems), len(failedItems)) } else { // Should not happen if input validation is correct, but handle defensively overallStatus = "no_items_processed" summaryMsg = fmt.Sprintf("No %s were provided for the '%s' action.", resourceType, action) } response := map[string]interface{}{ "status": overallStatus, "accepted": succeededItems, "failed": failedItems, } return response, summaryMsg }