fix: 接通服务器V2排序链路

- 将排序参数从V2 Handler传递至Service与Repository

- 增加仓储分页排序与参数白名单测试
This commit is contained in:
Logan.Li committed 2026-07-26 23:38:06 +08:00
1 parent 09e6271bb4
commit 83662487cb
7 files changed
+98 -41

No files matched your search

+2 -14
View File
@@ -74,9 +74,8 @@ func (h *ServerHandler) ListServers(c *gin.Context) {
status := c.Query("status")
regionID := c.Query("region_id")
// 构建缓存键(按查询参数区分) // Logan.Li
// 构建缓存键(按查询参数区分)
cacheKey := "servers:list:" + query.Keyword + ":" + status + ":" + regionID +
":" + query.Sort + ":" + query.Order +
":" + strconv.Itoa(query.Page) + ":" + strconv.Itoa(query.PageSize)
const cacheTTL = 10 * time.Second
@@ -115,7 +114,7 @@ func (h *ServerHandler) ListServers(c *gin.Context) {
var servers []model.Server
db.Preload("Region").Preload("Cloud").
Offset(query.GetOffset()).Limit(query.GetLimit()).
Order(serverOrderClause(query.Sort, query.Order)).
Order("created_at DESC").
Find(&servers)
// 填充 has_auth 字段(密码和SSH密钥在JSON中隐藏,但前端需知道是否有凭据) // Logan.Li
@@ -143,17 +142,6 @@ func (h *ServerHandler) ListServers(c *gin.Context) {
c.JSON(http.StatusOK, resp)
}
// serverOrderClause 返回服务器列表允许使用的排序表达式,避免直接拼接请求参数。 // Logan.Li
func serverOrderClause(sortField, order string) string {
if sortField != "hostname" {
return "created_at DESC"
}
if order == "desc" {
return "LOWER(hostname) DESC, id DESC"
}
return "LOWER(hostname) ASC, id ASC"
}
// GetServer 获取服务器详情
// @Summary 获取服务器详情
// @Description 获取指定服务器的详细信息