diff --git a/backend/internal/handler/admin/group_handler.go b/backend/internal/handler/admin/group_handler.go index dbf6f709a4..102ee02fdc 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 00205d1f79..ae9dd8f64e 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 0a2020eacb..eb3eff7f69 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,