| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using RackPeek.Domain.Helpers;
- using RackPeek.Domain.Persistence;
- namespace RackPeek.Domain.Resources.SystemResources.UseCases;
- public class UpdateSystemUseCase(IResourceCollection repository) : IUseCase
- {
- public async Task ExecuteAsync(
- string name,
- string? type = null,
- string? os = null,
- int? cores = null,
- double? ram = null,
- List<string>? runsOn = null,
- string? notes = null
- )
- {
- // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
- // ToDo validate / normalize all inputs
- name = Normalize.SystemName(name);
- ThrowIfInvalid.ResourceName(name);
- var system = await repository.GetByNameAsync(name) as SystemResource;
- if (system is null)
- throw new InvalidOperationException($"System '{name}' not found.");
- if (!string.IsNullOrWhiteSpace(type))
- {
- var normalizedSystemType = Normalize.SystemType(type);
- ThrowIfInvalid.SystemType(normalizedSystemType);
- system.Type = normalizedSystemType;
- }
- if (!string.IsNullOrWhiteSpace(os))
- system.Os = os;
- if (cores.HasValue)
- system.Cores = cores.Value;
- if (ram.HasValue)
- system.Ram = ram.Value;
- if (notes != null) system.Notes = notes;
- if (runsOn?.Count > 0)
- {
- foreach(string parent in runsOn) {
- if (!string.IsNullOrWhiteSpace(parent)) {
- ThrowIfInvalid.ResourceName(parent);
- var parentHardware = await repository.GetByNameAsync(parent) as Hardware.Hardware;
- if (parentHardware == null) throw new NotFoundException($"Parent hardware '{parent}' not found.");
- if (!system.RunsOn.Contains(parent)) system.RunsOn.Add(parent);
- }
- }
- }
- await repository.UpdateAsync(system);
- }
- }
|