mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-03 23:06:36 +08:00
fix: 配置版本列表按 created_at 倒序展示
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/apps/openflare/waf"
|
||||
"github.com/Rain-kl/Wavelet/internal/db"
|
||||
@@ -38,6 +39,40 @@ func setupConfigVersionTestDB(t *testing.T) func() {
|
||||
}
|
||||
}
|
||||
|
||||
func TestListConfigVersionsOrdersByCreatedAtDesc(t *testing.T) {
|
||||
cleanup := setupConfigVersionTestDB(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
conn := db.DB(ctx)
|
||||
require.NotNil(t, conn)
|
||||
|
||||
newer := &model.ConfigVersion{
|
||||
Version: "20260102-001",
|
||||
SnapshotJSON: "{}",
|
||||
RenderedConfig: "route {}",
|
||||
Checksum: "checksum-newer",
|
||||
CreatedBy: "tester",
|
||||
CreatedAt: time.Date(2026, 1, 2, 12, 0, 0, 0, time.UTC),
|
||||
}
|
||||
older := &model.ConfigVersion{
|
||||
Version: "20260101-001",
|
||||
SnapshotJSON: "{}",
|
||||
RenderedConfig: "route {}",
|
||||
Checksum: "checksum-older",
|
||||
CreatedBy: "tester",
|
||||
CreatedAt: time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC),
|
||||
}
|
||||
require.NoError(t, conn.Create(newer).Error)
|
||||
require.NoError(t, conn.Create(older).Error)
|
||||
require.Greater(t, older.ID, newer.ID)
|
||||
|
||||
versions, err := ListConfigVersions(ctx)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, versions, 2)
|
||||
assert.Equal(t, newer.Version, versions[0].Version)
|
||||
assert.Equal(t, older.Version, versions[1].Version)
|
||||
}
|
||||
|
||||
func TestPublishConfigVersionCreatesVersion(t *testing.T) {
|
||||
cleanup := setupConfigVersionTestDB(t)
|
||||
defer cleanup()
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
||||
func handleLogicError(c *gin.Context, err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
@@ -21,7 +20,7 @@ func handleLogicError(c *gin.Context, err error) bool {
|
||||
|
||||
// ListConfigVersionsHandler lists config versions.
|
||||
// @Summary 获取配置版本列表
|
||||
// @Description 返回所有已发布的 OpenResty 配置版本摘要,需要管理员权限
|
||||
// @Description 返回所有已发布的 OpenResty 配置版本摘要,按创建时间倒序排列,需要管理员权限
|
||||
// @Tags openflare-config-version
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
@@ -187,4 +186,4 @@ func CleanupConfigVersionsHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(result))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ func (ConfigVersion) TableName() string {
|
||||
return "of_config_versions"
|
||||
}
|
||||
|
||||
// ListConfigVersionSummaries returns config version summaries ordered by id desc.
|
||||
// ListConfigVersionSummaries returns config version summaries ordered by created_at desc.
|
||||
func ListConfigVersionSummaries(ctx context.Context) ([]*ConfigVersionSummary, error) {
|
||||
conn := db.DB(ctx)
|
||||
if conn == nil {
|
||||
@@ -50,7 +50,7 @@ func ListConfigVersionSummaries(ctx context.Context) ([]*ConfigVersionSummary, e
|
||||
var versions []*ConfigVersionSummary
|
||||
err := conn.Model(&ConfigVersion{}).
|
||||
Select("id", "version", "checksum", "is_active", "created_by", "created_at").
|
||||
Order("id desc").
|
||||
Order("created_at desc, id desc").
|
||||
Find(&versions).Error
|
||||
return versions, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user