ThrowIfInvalid.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.ComponentModel.DataAnnotations;
  2. using RackPeek.Domain.Resources.Hardware.Models;
  3. namespace RackPeek.Domain.Helpers;
  4. public static class ThrowIfInvalid
  5. {
  6. public static void ResourceName(string name)
  7. {
  8. if (string.IsNullOrWhiteSpace(name)) throw new ValidationException("Name is required.");
  9. if (name.Length > 50) throw new ValidationException("Name is too long.");
  10. }
  11. public static void AccessPointModelName(string name)
  12. {
  13. if (string.IsNullOrWhiteSpace(name))
  14. throw new ValidationException("Model name is required.");
  15. if (name.Length > 50)
  16. throw new ValidationException("Model name is too long.");
  17. }
  18. public static void RamGb(int? value)
  19. {
  20. if (value is null) throw new ValidationException("RAM value must be specified.");
  21. if (value < 0) throw new ValidationException("RAM value must be a non negative number of gigabytes.");
  22. }
  23. #region Nics
  24. public static void NicType(string nicType)
  25. {
  26. if (string.IsNullOrWhiteSpace(nicType)) throw new ValidationException("NIC type is required.");
  27. var normalized = nicType.Trim().ToLowerInvariant();
  28. if (Nic.ValidNicTypes.Contains(normalized)) return;
  29. var suggestions = GetNicTypeSuggestions(normalized).ToList();
  30. var message = suggestions.Any()
  31. ? $"NIC type '{nicType}' is not valid. Did you mean: {string.Join(", ", suggestions)}?"
  32. : $"NIC type '{nicType}' is not valid. Valid NIC types include rj45, sfp, sfp+, qsfp+ etc";
  33. throw new ValidationException(message);
  34. }
  35. private static IEnumerable<string> GetNicTypeSuggestions(string input)
  36. {
  37. return Nic.ValidNicTypes.Select(type => new { Type = type, Score = SimilarityScore(input, type) })
  38. .Where(x => x.Score >= 0.5)
  39. .OrderByDescending(x => x.Score)
  40. .Take(3)
  41. .Select(x => x.Type);
  42. }
  43. private static double SimilarityScore(string a, string b)
  44. {
  45. if (a == b) return 1.0;
  46. if (b.StartsWith(a) || a.StartsWith(b)) return 0.9;
  47. var commonChars = a.Intersect(b).Count();
  48. return (double)commonChars / Math.Max(a.Length, b.Length);
  49. }
  50. public static void NicSpeed(int speed)
  51. {
  52. if (speed < 0) throw new ValidationException("NIC speed must be a non negative number of gigabits per second.");
  53. }
  54. public static void NetworkSpeed(double speed)
  55. {
  56. if (speed < 0)
  57. throw new ValidationException(
  58. "Network speed must be a non negative number of gigabits per second.");
  59. }
  60. public static void NicPorts(int ports)
  61. {
  62. if (ports < 0) throw new ValidationException("NIC port count must be a non negative integer.");
  63. }
  64. #endregion
  65. #region Drives
  66. public static void DriveType(string driveType)
  67. {
  68. if (string.IsNullOrWhiteSpace(driveType)) throw new ValidationException("Drive type is required.");
  69. var normalized = driveType.Trim().ToLowerInvariant();
  70. if (Drive.ValidDriveTypes.Contains(normalized)) return;
  71. var suggestions = GetDriveTypeSuggestions(normalized).ToList();
  72. var message = suggestions.Any()
  73. ? $"Drive type '{driveType}' is not valid. Did you mean: {string.Join(", ", suggestions)}?"
  74. : $"Drive type '{driveType}' is not valid. Valid Drive types include nvme, ssd, hdd, sata, sas etc.";
  75. throw new ValidationException(message);
  76. }
  77. private static IEnumerable<string> GetDriveTypeSuggestions(string input)
  78. {
  79. return Drive.ValidDriveTypes.Select(type => new { Type = type, Score = SimilarityScore(input, type) })
  80. .Where(x => x.Score >= 0.5)
  81. .OrderByDescending(x => x.Score)
  82. .Take(3)
  83. .Select(x => x.Type);
  84. }
  85. public static void DriveSize(int size)
  86. {
  87. if (size < 0) throw new ValidationException("Drive size value must be a non negative number of gigabytes.");
  88. }
  89. #endregion
  90. }