AddDesktopDriveUseCase.cs 979 B

1234567891011121314151617181920212223242526272829303132
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources.Models;
  4. namespace RackPeek.Domain.Resources.Hardware.Desktops.Drives;
  5. public class AddDesktopDriveUseCase(IResourceCollection repository) : IUseCase
  6. {
  7. public async Task ExecuteAsync(
  8. string name,
  9. string? type,
  10. int? size)
  11. {
  12. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  13. // ToDo validate / normalize all inputs
  14. name = Normalize.HardwareName(name);
  15. ThrowIfInvalid.ResourceName(name);
  16. var desktop = await repository.GetByNameAsync(name) as Desktop
  17. ?? throw new NotFoundException($"Desktop '{name}' not found.");
  18. desktop.Drives ??= new List<Drive>();
  19. desktop.Drives.Add(new Drive
  20. {
  21. Type = type,
  22. Size = size
  23. });
  24. await repository.UpdateAsync(desktop);
  25. }
  26. }