using RackPeek.Domain.Helpers; using RackPeek.Domain.Persistence; using RackPeek.Domain.Resources; using RackPeek.Domain.Resources.Servers; using RackPeek.Domain.Resources.SubResources; namespace RackPeek.Domain.UseCases.Gpus; public interface IAddGpuUseCase : IResourceUseCase where T : Resource { public Task ExecuteAsync( string name, string? model, int? vram); } public class AddGpuUseCase(IResourceCollection repository) : IAddGpuUseCase where T : Resource { public async Task ExecuteAsync( string name, string? model, int? vram) { // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable // ToDo validate / normalize all inputs name = Normalize.HardwareName(name); ThrowIfInvalid.ResourceName(name); T resource = await repository.GetByNameAsync(name) ?? throw new NotFoundException($"Resource '{name}' not found."); if (resource is not IGpuResource gr) throw new NotFoundException($"Resource '{name}' not found."); gr.Gpus ??= new List(); gr.Gpus.Add(new Gpu { Model = model, Vram = vram }); await repository.UpdateAsync(resource); } }