DeleteMultipleKeys handles the business logic of deleting keys from a text block.
(groupID uint, keysText string)
| 253 | |
| 254 | // DeleteMultipleKeys handles the business logic of deleting keys from a text block. |
| 255 | func (s *KeyService) DeleteMultipleKeys(groupID uint, keysText string) (*DeleteKeysResult, error) { |
| 256 | keysToDelete := s.ParseKeysFromText(keysText) |
| 257 | if len(keysToDelete) > maxRequestKeys { |
| 258 | return nil, fmt.Errorf("batch size exceeds the limit of %d keys, got %d", maxRequestKeys, len(keysToDelete)) |
| 259 | } |
| 260 | if len(keysToDelete) == 0 { |
| 261 | return nil, fmt.Errorf("no valid keys found in the input text") |
| 262 | } |
| 263 | |
| 264 | var totalDeletedCount int64 |
| 265 | for i := 0; i < len(keysToDelete); i += chunkSize { |
| 266 | end := i + chunkSize |
| 267 | if end > len(keysToDelete) { |
| 268 | end = len(keysToDelete) |
| 269 | } |
| 270 | chunk := keysToDelete[i:end] |
| 271 | deletedCount, err := s.KeyProvider.RemoveKeys(groupID, chunk) |
| 272 | if err != nil { |
| 273 | return nil, err |
| 274 | } |
| 275 | totalDeletedCount += deletedCount |
| 276 | } |
| 277 | |
| 278 | ignoredCount := len(keysToDelete) - int(totalDeletedCount) |
| 279 | |
| 280 | var totalInGroup int64 |
| 281 | if err := s.DB.Model(&models.APIKey{}).Where("group_id = ?", groupID).Count(&totalInGroup).Error; err != nil { |
| 282 | return nil, err |
| 283 | } |
| 284 | |
| 285 | return &DeleteKeysResult{ |
| 286 | DeletedCount: int(totalDeletedCount), |
| 287 | IgnoredCount: ignoredCount, |
| 288 | TotalInGroup: totalInGroup, |
| 289 | }, nil |
| 290 | } |
| 291 | |
| 292 | // ListKeysInGroupQuery builds a query to list all keys within a specific group, filtered by status. |
| 293 | func (s *KeyService) ListKeysInGroupQuery(groupID uint, statusFilter string, searchHash string) *gorm.DB { |
nothing calls this directly
no test coverage detected