Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>master
| @@ -0,0 +1,55 @@ | |||||
| package controller | |||||
| import ( | |||||
| "github.com/QuantumNous/new-api/common" | |||||
| "github.com/QuantumNous/new-api/model" | |||||
| "github.com/gin-gonic/gin" | |||||
| ) | |||||
| // ReorderVendors 批量更新供应商排序 | |||||
| func ReorderVendors(c *gin.Context) { | |||||
| var req struct { | |||||
| Items []struct { | |||||
| Id int `json:"id"` | |||||
| SortOrder int `json:"sort_order"` | |||||
| } `json:"items"` | |||||
| } | |||||
| if err := c.ShouldBindJSON(&req); err != nil { | |||||
| common.ApiError(c, err) | |||||
| return | |||||
| } | |||||
| if len(req.Items) == 0 { | |||||
| common.ApiErrorMsg(c, "items 不能为空") | |||||
| return | |||||
| } | |||||
| if err := model.ReorderVendors(req.Items); err != nil { | |||||
| common.ApiError(c, err) | |||||
| return | |||||
| } | |||||
| common.ApiSuccess(c, nil) | |||||
| } | |||||
| // ReorderModels 批量更新模型排序 | |||||
| func ReorderModels(c *gin.Context) { | |||||
| var req struct { | |||||
| Items []struct { | |||||
| Id int `json:"id"` | |||||
| SortOrder int `json:"sort_order"` | |||||
| } `json:"items"` | |||||
| } | |||||
| if err := c.ShouldBindJSON(&req); err != nil { | |||||
| common.ApiError(c, err) | |||||
| return | |||||
| } | |||||
| if len(req.Items) == 0 { | |||||
| common.ApiErrorMsg(c, "items 不能为空") | |||||
| return | |||||
| } | |||||
| if err := model.ReorderModels(req.Items); err != nil { | |||||
| common.ApiError(c, err) | |||||
| return | |||||
| } | |||||
| model.RefreshPricing() | |||||
| common.ApiSuccess(c, nil) | |||||
| } | |||||
| @@ -171,3 +171,18 @@ func SearchModels(keyword string, vendor string, offset int, limit int) ([]*Mode | |||||
| } | } | ||||
| return models, total, nil | return models, total, nil | ||||
| } | } | ||||
| // ReorderModels 批量更新模型排序值 | |||||
| func ReorderModels(items []struct { | |||||
| Id int `json:"id"` | |||||
| SortOrder int `json:"sort_order"` | |||||
| }) error { | |||||
| return DB.Transaction(func(tx *gorm.DB) error { | |||||
| for _, item := range items { | |||||
| if err := tx.Model(&Model{}).Where("id = ?", item.Id).Update("sort_order", item.SortOrder).Error; err != nil { | |||||
| return err | |||||
| } | |||||
| } | |||||
| return nil | |||||
| }) | |||||
| } | |||||
| @@ -87,3 +87,18 @@ func SearchVendors(keyword string, offset int, limit int) ([]*Vendor, int64, err | |||||
| } | } | ||||
| return vendors, total, nil | return vendors, total, nil | ||||
| } | } | ||||
| // ReorderVendors 批量更新供应商排序值 | |||||
| func ReorderVendors(items []struct { | |||||
| Id int `json:"id"` | |||||
| SortOrder int `json:"sort_order"` | |||||
| }) error { | |||||
| return DB.Transaction(func(tx *gorm.DB) error { | |||||
| for _, item := range items { | |||||
| if err := tx.Model(&Vendor{}).Where("id = ?", item.Id).Update("sort_order", item.SortOrder).Error; err != nil { | |||||
| return err | |||||
| } | |||||
| } | |||||
| return nil | |||||
| }) | |||||
| } | |||||
| @@ -356,6 +356,7 @@ func SetApiRouter(router *gin.Engine) { | |||||
| vendorRoute.GET("/:id", controller.GetVendorMeta) | vendorRoute.GET("/:id", controller.GetVendorMeta) | ||||
| vendorRoute.POST("/", controller.CreateVendorMeta) | vendorRoute.POST("/", controller.CreateVendorMeta) | ||||
| vendorRoute.PUT("/", controller.UpdateVendorMeta) | vendorRoute.PUT("/", controller.UpdateVendorMeta) | ||||
| vendorRoute.PUT("/reorder", controller.ReorderVendors) | |||||
| vendorRoute.DELETE("/:id", controller.DeleteVendorMeta) | vendorRoute.DELETE("/:id", controller.DeleteVendorMeta) | ||||
| } | } | ||||
| @@ -370,6 +371,7 @@ func SetApiRouter(router *gin.Engine) { | |||||
| modelsRoute.GET("/:id", controller.GetModelMeta) | modelsRoute.GET("/:id", controller.GetModelMeta) | ||||
| modelsRoute.POST("/", controller.CreateModelMeta) | modelsRoute.POST("/", controller.CreateModelMeta) | ||||
| modelsRoute.PUT("/", controller.UpdateModelMeta) | modelsRoute.PUT("/", controller.UpdateModelMeta) | ||||
| modelsRoute.PUT("/reorder", controller.ReorderModels) | |||||
| modelsRoute.DELETE("/:id", controller.DeleteModelMeta) | modelsRoute.DELETE("/:id", controller.DeleteModelMeta) | ||||
| } | } | ||||