UpdateLaptopUseCase.cs 1.2 KB

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