AddDriveUseCase.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. public Task ExecuteAsync(
  10. string name,
  11. string? type,
  12. int? size);
  13. }
  14. public class AddDriveUseCase<T>(IResourceCollection repository) : IAddDriveUseCase<T> where T : Resource {
  15. public async Task ExecuteAsync(
  16. string name,
  17. string? type,
  18. int? size) {
  19. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  20. // ToDo validate / normalize all inputs
  21. name = Normalize.HardwareName(name);
  22. ThrowIfInvalid.ResourceName(name);
  23. T resource = await repository.GetByNameAsync<T>(name) ??
  24. throw new NotFoundException($"Resource '{name}' not found.");
  25. if (resource is not IDriveResource dr) throw new NotFoundException($"Resource '{name}' not found.");
  26. dr.Drives ??= new List<Drive>();
  27. dr.Drives.Add(new Drive {
  28. Type = type,
  29. Size = size
  30. });
  31. await repository.UpdateAsync(resource);
  32. }
  33. }