AddDesktopDriveUseCase.cs 969 B

12345678910111213141516171819202122232425262728293031
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources.Models;
  4. namespace RackPeek.Domain.Resources.Hardware.Laptops.Drives;
  5. public class AddLaptopDriveUseCase(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 laptop = await repository.GetByNameAsync(name) as Laptop
  17. ?? throw new NotFoundException($"Laptop '{name}' not found.");
  18. laptop.Drives ??= new List<Drive>();
  19. laptop.Drives.Add(new Drive
  20. {
  21. Type = type,
  22. Size = size
  23. });
  24. await repository.UpdateAsync(laptop);
  25. }
  26. }