AddDriveUseCase.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.Servers;
  5. using RackPeek.Domain.Resources.SubResources;
  6. namespace RackPeek.Domain.UseCases.Drives;
  7. public interface IAddDriveUseCase<T> : IResourceUseCase<T>
  8. where T : Resource
  9. {
  10. public Task ExecuteAsync(
  11. string name,
  12. string? type,
  13. int? size);
  14. }
  15. public class AddDriveUseCase<T>(IResourceCollection repository) : IAddDriveUseCase<T> where T : Resource
  16. {
  17. public async Task ExecuteAsync(
  18. string name,
  19. string? type,
  20. int? size)
  21. {
  22. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  23. // ToDo validate / normalize all inputs
  24. name = Normalize.HardwareName(name);
  25. ThrowIfInvalid.ResourceName(name);
  26. var resource = await repository.GetByNameAsync<T>(name) ??
  27. throw new NotFoundException($"Resource '{name}' not found.");
  28. if (resource is not IDriveResource dr) throw new NotFoundException($"Resource '{name}' not found.");
  29. dr.Drives ??= new List<Drive>();
  30. dr.Drives.Add(new Drive
  31. {
  32. Type = type,
  33. Size = size
  34. });
  35. await repository.UpdateAsync(resource);
  36. }
  37. }