4
0

UpdateLaptopUseCase.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. public async Task ExecuteAsync(
  7. string name,
  8. string? model = null,
  9. double? ramGb = null,
  10. int? ramMts = null,
  11. string? notes = null
  12. ) {
  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. if (laptop == null)
  18. throw new NotFoundException($"Laptop '{name}' not found.");
  19. if (!string.IsNullOrWhiteSpace(model))
  20. laptop.Model = model;
  21. // ---- RAM ----
  22. if (ramGb.HasValue) {
  23. ThrowIfInvalid.RamGb(ramGb);
  24. laptop.Ram ??= new Ram();
  25. laptop.Ram.Size = ramGb.Value;
  26. }
  27. if (ramMts.HasValue) {
  28. laptop.Ram ??= new Ram();
  29. laptop.Ram.Mts = ramMts.Value;
  30. }
  31. if (laptop.Ram != null) {
  32. if (laptop.Ram.Size == 0) laptop.Ram.Size = null;
  33. if (laptop.Ram.Mts == 0) laptop.Ram.Mts = null;
  34. if (laptop.Ram.Size == null && laptop.Ram.Mts == null) laptop.Ram = null;
  35. }
  36. if (notes != null) laptop.Notes = notes;
  37. await repository.UpdateAsync(laptop);
  38. }
  39. }