From bc7ce185749c543df19cfb92b1909d2f41516386 Mon Sep 17 00:00:00 2001 From: ghostg00 <28946120+ghostg00@users.noreply.github.com> Date: Thu, 4 Jun 2026 19:48:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(group):=20=E7=AE=A1=E7=90=86=E5=91=98?= =?UTF-8?q?=E6=B8=85=E7=A9=BA=E5=88=86=E7=BB=84=E6=8F=8F=E8=BF=B0=E6=97=B6?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E6=8C=81=E4=B9=85=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit UpdateGroup 之前用 `if input.Description != ""` 判空, 把"未提供"和"显式置空"混为一谈,导致管理员在分组编辑表单 里清空备注后保存无效。 将 UpdateGroupRequest / UpdateGroupInput 的 Description 改为 *string:nil 表示未提供(保持原值),"" 表示显式清空。 --- .../internal/handler/admin/group_handler.go | 2 +- backend/internal/service/admin_service.go | 6 +-- .../service/admin_service_group_test.go | 42 ++++++++++++++++++- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/backend/internal/handler/admin/group_handler.go b/backend/internal/handler/admin/group_handler.go index dbf6f709..102ee02f 100644 --- a/backend/internal/handler/admin/group_handler.go +++ b/backend/internal/handler/admin/group_handler.go @@ -123,7 +123,7 @@ type CreateGroupRequest struct { // UpdateGroupRequest represents update group request type UpdateGroupRequest struct { Name string `json:"name"` - Description string `json:"description"` + Description *string `json:"description"` Platform string `json:"platform" binding:"omitempty,oneof=anthropic openai gemini antigravity"` RateMultiplier *float64 `json:"rate_multiplier"` IsExclusive *bool `json:"is_exclusive"` diff --git a/backend/internal/service/admin_service.go b/backend/internal/service/admin_service.go index 00205d1f..ae9dd8f6 100644 --- a/backend/internal/service/admin_service.go +++ b/backend/internal/service/admin_service.go @@ -230,7 +230,7 @@ type CreateGroupInput struct { type UpdateGroupInput struct { Name string - Description string + Description *string Platform string RateMultiplier *float64 // 使用指针以支持设置为0 IsExclusive *bool @@ -1924,8 +1924,8 @@ func (s *adminServiceImpl) UpdateGroup(ctx context.Context, id int64, input *Upd if input.Name != "" { group.Name = input.Name } - if input.Description != "" { - group.Description = input.Description + if input.Description != nil { + group.Description = *input.Description } if input.Platform != "" { group.Platform = input.Platform diff --git a/backend/internal/service/admin_service_group_test.go b/backend/internal/service/admin_service_group_test.go index 0a2020ea..eb3eff7f 100644 --- a/backend/internal/service/admin_service_group_test.go +++ b/backend/internal/service/admin_service_group_test.go @@ -280,8 +280,9 @@ func TestAdminService_UpdateGroup_PreservesImageGenerationControlsWhenOmitted(t repo := &groupRepoStubForAdmin{getByID: existingGroup} svc := &adminServiceImpl{groupRepo: repo} + updatedDesc := "updated" group, err := svc.UpdateGroup(context.Background(), 1, &UpdateGroupInput{ - Description: "updated", + Description: &updatedDesc, }) require.NoError(t, err) require.NotNil(t, group) @@ -291,6 +292,45 @@ func TestAdminService_UpdateGroup_PreservesImageGenerationControlsWhenOmitted(t 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) { existingGroup := &Group{ ID: 1,