diff --git a/controller/reorder.go b/controller/reorder.go new file mode 100644 index 0000000..5a02481 --- /dev/null +++ b/controller/reorder.go @@ -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) +} diff --git a/model/model_meta.go b/model/model_meta.go index b55faea..b9c1ad5 100644 --- a/model/model_meta.go +++ b/model/model_meta.go @@ -171,3 +171,18 @@ func SearchModels(keyword string, vendor string, offset int, limit int) ([]*Mode } 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 + }) +} diff --git a/model/vendor_meta.go b/model/vendor_meta.go index a0bbf52..f6b7e16 100644 --- a/model/vendor_meta.go +++ b/model/vendor_meta.go @@ -87,3 +87,18 @@ func SearchVendors(keyword string, offset int, limit int) ([]*Vendor, int64, err } 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 + }) +} diff --git a/router/api-router.go b/router/api-router.go index 9d56d21..7a0d166 100644 --- a/router/api-router.go +++ b/router/api-router.go @@ -356,6 +356,7 @@ func SetApiRouter(router *gin.Engine) { vendorRoute.GET("/:id", controller.GetVendorMeta) vendorRoute.POST("/", controller.CreateVendorMeta) vendorRoute.PUT("/", controller.UpdateVendorMeta) + vendorRoute.PUT("/reorder", controller.ReorderVendors) vendorRoute.DELETE("/:id", controller.DeleteVendorMeta) } @@ -370,6 +371,7 @@ func SetApiRouter(router *gin.Engine) { modelsRoute.GET("/:id", controller.GetModelMeta) modelsRoute.POST("/", controller.CreateModelMeta) modelsRoute.PUT("/", controller.UpdateModelMeta) + modelsRoute.PUT("/reorder", controller.ReorderModels) modelsRoute.DELETE("/:id", controller.DeleteModelMeta) }