using RackPeek.Domain.Helpers; using RackPeek.Domain.Persistence; using RackPeek.Domain.Resources; namespace RackPeek.Domain.UseCases.Labels; /// /// Adds or updates a key-value label on a resource. /// public interface IAddLabelUseCase : IResourceUseCase where T : Resource { /// /// Adds or overwrites a label on the resource. If the key already exists, the value is updated. /// /// Resource name. /// Label key. /// Label value. /// Thrown when the resource does not exist. /// Thrown when key or value fails validation. Task ExecuteAsync(string name, string key, string value); } /// /// Adds or updates a key-value label on a resource. /// public class AddLabelUseCase(IResourceCollection repo) : IAddLabelUseCase where T : Resource { /// public async Task ExecuteAsync(string name, string key, string value) { key = Normalize.LabelKey(key); value = Normalize.LabelValue(value); name = Normalize.HardwareName(name); ThrowIfInvalid.ResourceName(name); ThrowIfInvalid.LabelKey(key); ThrowIfInvalid.LabelValue(value); Resource? resource = await repo.GetByNameAsync(name); if (resource is null) throw new NotFoundException($"Resource '{name}' not found."); resource.Labels[key] = value; await repo.UpdateAsync(resource); } }