UpdateLaptopUseCase.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Laptops;
  4. public class UpdateLaptopUseCase(IHardwareRepository repository) : IUseCase
  5. {
  6. public async Task ExecuteAsync(
  7. string name,
  8. string? model = null,
  9. int? ramGb = null,
  10. int? ramMts = null,
  11. string? notes = null
  12. )
  13. {
  14. // ToDo validate / normalize all inputs
  15. name = Normalize.HardwareName(name);
  16. ThrowIfInvalid.ResourceName(name);
  17. var laptop = await repository.GetByNameAsync(name) as Laptop;
  18. if (laptop == null)
  19. throw new NotFoundException($"Laptop '{name}' not found.");
  20. if (!string.IsNullOrWhiteSpace(model))
  21. laptop.Model = model;
  22. // ---- RAM ----
  23. if (ramGb.HasValue)
  24. {
  25. ThrowIfInvalid.RamGb(ramGb);
  26. laptop.Ram ??= new Ram();
  27. laptop.Ram.Size = ramGb.Value;
  28. }
  29. if (ramMts.HasValue)
  30. {
  31. laptop.Ram ??= new Ram();
  32. laptop.Ram.Mts = ramMts.Value;
  33. }
  34. if (notes != null)
  35. {
  36. laptop.Notes = notes;
  37. }
  38. await repository.UpdateAsync(laptop);
  39. }
  40. }