CopyChannel handles cloning an existing channel with its key. POST /api/channel/copy/:id Optional query params: suffix - string appended to the original name (default "_复制") reset_balance - bool, when true will reset balance & used_quota to 0 (default true)
(c *gin.Context)
| 872 | // suffix - string appended to the original name (default "_复制") |
| 873 | // reset_balance - bool, when true will reset balance & used_quota to 0 (default true) |
| 874 | func CopyChannel(c *gin.Context) { |
| 875 | id, err := strconv.Atoi(c.Param("id")) |
| 876 | if err != nil { |
| 877 | c.JSON(http.StatusOK, gin.H{"success": false, "message": "invalid id"}) |
| 878 | return |
| 879 | } |
| 880 | |
| 881 | suffix := c.DefaultQuery("suffix", "_复制") |
| 882 | resetBalance := true |
| 883 | if rbStr := c.DefaultQuery("reset_balance", "true"); rbStr != "" { |
| 884 | if v, err := strconv.ParseBool(rbStr); err == nil { |
| 885 | resetBalance = v |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | // fetch original channel with key |
| 890 | origin, err := model.GetChannelById(id, true) |
| 891 | if err != nil { |
| 892 | c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()}) |
| 893 | return |
| 894 | } |
| 895 | |
| 896 | // clone channel |
| 897 | clone := *origin // shallow copy is sufficient as we will overwrite primitives |
| 898 | clone.Id = 0 // let DB auto-generate |
| 899 | clone.CreatedTime = common.GetTimestamp() |
| 900 | clone.Name = origin.Name + suffix |
| 901 | clone.TestTime = 0 |
| 902 | clone.ResponseTime = 0 |
| 903 | if resetBalance { |
| 904 | clone.Balance = 0 |
| 905 | clone.UsedQuota = 0 |
| 906 | } |
| 907 | |
| 908 | // insert |
| 909 | if err := model.BatchInsertChannels([]model.Channel{clone}); err != nil { |
| 910 | c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()}) |
| 911 | return |
| 912 | } |
| 913 | model.InitChannelCache() |
| 914 | // success |
| 915 | c.JSON(http.StatusOK, gin.H{"success": true, "message": "", "data": gin.H{"id": clone.Id}}) |
| 916 | } |
nothing calls this directly
no test coverage detected