UpdateLaptopUseCase.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. double? 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 (laptop.Ram != null)
  36. {
  37. if (laptop.Ram.Size == 0)
  38. {
  39. laptop.Ram.Size = null;
  40. }
  41. if (laptop.Ram.Mts == 0)
  42. {
  43. laptop.Ram.Mts = null;
  44. }
  45. if (laptop.Ram.Size == null && laptop.Ram.Mts == null)
  46. {
  47. laptop.Ram = null;
  48. }
  49. }
  50. if (notes != null) laptop.Notes = notes;
  51. await repository.UpdateAsync(laptop);
  52. }
  53. }