fix(group): 管理员清空分组描述时正确持久化
UpdateGroup 之前用 `if input.Description != ""` 判空, 把"未提供"和"显式置空"混为一谈,导致管理员在分组编辑表单 里清空备注后保存无效。 将 UpdateGroupRequest / UpdateGroupInput 的 Description 改为 *string:nil 表示未提供(保持原值),"" 表示显式清空。
This commit is contained in:
parent
f1aa589646
commit
bc7ce18574
@ -123,7 +123,7 @@ type CreateGroupRequest struct {
|
|||||||
// UpdateGroupRequest represents update group request
|
// UpdateGroupRequest represents update group request
|
||||||
type UpdateGroupRequest struct {
|
type UpdateGroupRequest struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description *string `json:"description"`
|
||||||
Platform string `json:"platform" binding:"omitempty,oneof=anthropic openai gemini antigravity"`
|
Platform string `json:"platform" binding:"omitempty,oneof=anthropic openai gemini antigravity"`
|
||||||
RateMultiplier *float64 `json:"rate_multiplier"`
|
RateMultiplier *float64 `json:"rate_multiplier"`
|
||||||
IsExclusive *bool `json:"is_exclusive"`
|
IsExclusive *bool `json:"is_exclusive"`
|
||||||
|
|||||||
@ -230,7 +230,7 @@ type CreateGroupInput struct {
|
|||||||
|
|
||||||
type UpdateGroupInput struct {
|
type UpdateGroupInput struct {
|
||||||
Name string
|
Name string
|
||||||
Description string
|
Description *string
|
||||||
Platform string
|
Platform string
|
||||||
RateMultiplier *float64 // 使用指针以支持设置为0
|
RateMultiplier *float64 // 使用指针以支持设置为0
|
||||||
IsExclusive *bool
|
IsExclusive *bool
|
||||||
@ -1924,8 +1924,8 @@ func (s *adminServiceImpl) UpdateGroup(ctx context.Context, id int64, input *Upd
|
|||||||
if input.Name != "" {
|
if input.Name != "" {
|
||||||
group.Name = input.Name
|
group.Name = input.Name
|
||||||
}
|
}
|
||||||
if input.Description != "" {
|
if input.Description != nil {
|
||||||
group.Description = input.Description
|
group.Description = *input.Description
|
||||||
}
|
}
|
||||||
if input.Platform != "" {
|
if input.Platform != "" {
|
||||||
group.Platform = input.Platform
|
group.Platform = input.Platform
|
||||||
|
|||||||
@ -280,8 +280,9 @@ func TestAdminService_UpdateGroup_PreservesImageGenerationControlsWhenOmitted(t
|
|||||||
repo := &groupRepoStubForAdmin{getByID: existingGroup}
|
repo := &groupRepoStubForAdmin{getByID: existingGroup}
|
||||||
svc := &adminServiceImpl{groupRepo: repo}
|
svc := &adminServiceImpl{groupRepo: repo}
|
||||||
|
|
||||||
|
updatedDesc := "updated"
|
||||||
group, err := svc.UpdateGroup(context.Background(), 1, &UpdateGroupInput{
|
group, err := svc.UpdateGroup(context.Background(), 1, &UpdateGroupInput{
|
||||||
Description: "updated",
|
Description: &updatedDesc,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, group)
|
require.NotNil(t, group)
|
||||||
@ -291,6 +292,45 @@ func TestAdminService_UpdateGroup_PreservesImageGenerationControlsWhenOmitted(t
|
|||||||
require.InDelta(t, 0.5, repo.updated.ImageRateMultiplier, 1e-12)
|
require.InDelta(t, 0.5, repo.updated.ImageRateMultiplier, 1e-12)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAdminService_UpdateGroup_ClearsDescriptionWhenEmptyString(t *testing.T) {
|
||||||
|
existingGroup := &Group{
|
||||||
|
ID: 1,
|
||||||
|
Name: "existing-group",
|
||||||
|
Description: "Auto-created default group",
|
||||||
|
Platform: PlatformOpenAI,
|
||||||
|
Status: StatusActive,
|
||||||
|
}
|
||||||
|
repo := &groupRepoStubForAdmin{getByID: existingGroup}
|
||||||
|
svc := &adminServiceImpl{groupRepo: repo}
|
||||||
|
|
||||||
|
empty := ""
|
||||||
|
_, err := svc.UpdateGroup(context.Background(), 1, &UpdateGroupInput{
|
||||||
|
Description: &empty,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, repo.updated)
|
||||||
|
require.Equal(t, "", repo.updated.Description, "empty string should clear description")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAdminService_UpdateGroup_PreservesDescriptionWhenNil(t *testing.T) {
|
||||||
|
existingGroup := &Group{
|
||||||
|
ID: 1,
|
||||||
|
Name: "existing-group",
|
||||||
|
Description: "keep me",
|
||||||
|
Platform: PlatformOpenAI,
|
||||||
|
Status: StatusActive,
|
||||||
|
}
|
||||||
|
repo := &groupRepoStubForAdmin{getByID: existingGroup}
|
||||||
|
svc := &adminServiceImpl{groupRepo: repo}
|
||||||
|
|
||||||
|
_, err := svc.UpdateGroup(context.Background(), 1, &UpdateGroupInput{
|
||||||
|
Description: nil,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, repo.updated)
|
||||||
|
require.Equal(t, "keep me", repo.updated.Description, "nil should preserve existing description")
|
||||||
|
}
|
||||||
|
|
||||||
func TestAdminService_UpdateGroup_RejectsNegativeImageRateMultiplier(t *testing.T) {
|
func TestAdminService_UpdateGroup_RejectsNegativeImageRateMultiplier(t *testing.T) {
|
||||||
existingGroup := &Group{
|
existingGroup := &Group{
|
||||||
ID: 1,
|
ID: 1,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user