AddDesktopDriveUseCase.cs 934 B

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