Summary • Backend – Moved duplicate-name validation and total vendor-count aggregation from controllers (`controller/model_meta.go`, `controller/vendor_meta.go`, `controller/prefill_group.go`) to model layer (`model/model_meta.go`, `model/vendor_meta.go`, `model/prefill_group.go`). – Added `GetVendorModelCounts()` and `Is*NameDuplicated()` helpers; controllers now call these instead of duplicating queries. – API response for `/api/models` now returns `vendor_counts` with per-vendor totals across all pages, plus `all` summary. – Removed redundant checks and unused imports, eliminating `go vet` warnings. • Frontend – `useModelsData.js` updated to consume backend-supplied `vendor_counts`, calculate the `all` total once, and drop legacy client-side counting logic. – Simplified initial data flow: first render now triggers only one models request. – Deleted obsolete `updateVendorCounts` helper and related comments. – Ensured search flow also sets `vendorCounts`, keeping tab badges accurate. Why This refactor enforces single-responsibility (aggregation in model layer), delivers consistent totals irrespective of pagination, and removes redundant client queries, leading to cleaner code and better performance.
91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package controller
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"one-api/common"
|
|
"one-api/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// GetPrefillGroups 获取预填组列表,可通过 ?type=xxx 过滤
|
|
func GetPrefillGroups(c *gin.Context) {
|
|
groupType := c.Query("type")
|
|
groups, err := model.GetAllPrefillGroups(groupType)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
common.ApiSuccess(c, groups)
|
|
}
|
|
|
|
// CreatePrefillGroup 创建新的预填组
|
|
func CreatePrefillGroup(c *gin.Context) {
|
|
var g model.PrefillGroup
|
|
if err := c.ShouldBindJSON(&g); err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
if g.Name == "" || g.Type == "" {
|
|
common.ApiErrorMsg(c, "组名称和类型不能为空")
|
|
return
|
|
}
|
|
// 创建前检查名称
|
|
if dup, err := model.IsPrefillGroupNameDuplicated(0, g.Name); err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
} else if dup {
|
|
common.ApiErrorMsg(c, "组名称已存在")
|
|
return
|
|
}
|
|
|
|
if err := g.Insert(); err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
common.ApiSuccess(c, &g)
|
|
}
|
|
|
|
// UpdatePrefillGroup 更新预填组
|
|
func UpdatePrefillGroup(c *gin.Context) {
|
|
var g model.PrefillGroup
|
|
if err := c.ShouldBindJSON(&g); err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
if g.Id == 0 {
|
|
common.ApiErrorMsg(c, "缺少组 ID")
|
|
return
|
|
}
|
|
// 名称冲突检查
|
|
if dup, err := model.IsPrefillGroupNameDuplicated(g.Id, g.Name); err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
} else if dup {
|
|
common.ApiErrorMsg(c, "组名称已存在")
|
|
return
|
|
}
|
|
|
|
if err := g.Update(); err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
common.ApiSuccess(c, &g)
|
|
}
|
|
|
|
// DeletePrefillGroup 删除预填组
|
|
func DeletePrefillGroup(c *gin.Context) {
|
|
idStr := c.Param("id")
|
|
id, err := strconv.Atoi(idStr)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
if err := model.DeletePrefillGroupByID(id); err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
common.ApiSuccess(c, nil)
|
|
}
|