using RackPeek.Domain.Helpers; using RackPeek.Domain.Persistence; using RackPeek.Domain.Resources; namespace RackPeek.Domain.UseCases.Labels; /// /// Removes a label by key from a resource. /// public interface IRemoveLabelUseCase : IResourceUseCase where T : Resource { /// /// Removes the label with the given key. No-op if the key does not exist. /// /// Resource name. /// Label key to remove. /// Thrown when the resource does not exist. /// Thrown when key fails validation. Task ExecuteAsync(string name, string key); } /// /// Removes a label by key from a resource. /// public class RemoveLabelUseCase(IResourceCollection repo) : IRemoveLabelUseCase where T : Resource { /// public async Task ExecuteAsync(string name, string key) { key = Normalize.LabelKey(key); name = Normalize.HardwareName(name); ThrowIfInvalid.ResourceName(name); ThrowIfInvalid.LabelKey(key); Resource? resource = await repo.GetByNameAsync(name); if (resource is null) throw new NotFoundException($"Resource '{name}' not found."); if (!resource.Labels.Remove(key)) return; await repo.UpdateAsync(resource); } }