Functions.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.ComponentModel.DataAnnotations;
  2. using System.Reflection;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.ModelBinding;
  5. using bitforum.Models;
  6. namespace bitforum.Helpers
  7. {
  8. public static class Functions
  9. {
  10. // SaveConfig 함수는 Config 모델에 대한 정보를 저장하는 함수
  11. public static void SaveConfig<T>(T request, Action<Config> saveAction)
  12. {
  13. var properties = typeof(T).GetProperties();
  14. foreach (var property in properties)
  15. {
  16. var key = (property.GetCustomAttribute<BindPropertyAttribute>()?.Name ?? property.Name); // 속성 Key 이름
  17. var value = property.GetValue(request)?.ToString(); // 속성 값
  18. var displayName = (property.GetCustomAttribute<DisplayAttribute>()?.Name ?? key); // 속성 표시 이름
  19. saveAction(new Config
  20. {
  21. Key = key,
  22. Value = value,
  23. Description = displayName
  24. });
  25. }
  26. }
  27. // Config 값 조회
  28. public static string? GetConfig(this Dictionary<string, string> config, string k, string? d = null)
  29. {
  30. return config.TryGetValue(k, out var v) ? v : d;
  31. }
  32. // 날짜 포맷 조회
  33. public static string GetDateAt(this DateTime? dateTime, string format = "yyyy.MM.dd HH:mm:ss") => dateTime?.ToString(format) ?? string.Empty;
  34. public static string GetDateAt(this DateTime dateTime, string format = "yyyy.MM.dd HH:mm:ss") => dateTime.ToString(format);
  35. // ModelState 오류 조회
  36. public static Dictionary<string, List<string>> GetErrors(this ModelStateDictionary modelState)
  37. {
  38. return modelState.Where(ms => ms.Value.Errors.Any()).ToDictionary(
  39. ms => ms.Key,
  40. ms => ms.Value.Errors.Select(e => e.ErrorMessage).ToList()
  41. );
  42. }
  43. // Client IP 조회
  44. public static string GetClientIP(this HttpContext context)
  45. {
  46. return context.Request.Headers["X-Forwarded-For"].FirstOrDefault() ?? context.Connection.RemoteIpAddress?.ToString() ?? "Unknown";
  47. }
  48. // Dictionary 데이터를 특정 객체에 자동 매핑
  49. public static T Mapping<T>(Dictionary<string, string> dict) where T : new()
  50. {
  51. var obj = new T();
  52. var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  53. foreach (var prop in properties)
  54. {
  55. var key = prop.GetCustomAttribute<BindPropertyAttribute>()?.Name ?? prop.Name;
  56. if (dict.TryGetValue(key, out var value) && !string.IsNullOrEmpty(value))
  57. {
  58. try
  59. {
  60. prop.SetValue(obj, ConvertValue(value, prop.PropertyType));
  61. }
  62. catch (Exception ex)
  63. {
  64. Console.WriteLine($"`{prop.Name}` 객체 변환 오류: {ex.Message}");
  65. }
  66. }
  67. }
  68. return obj;
  69. }
  70. // 문자열 값을 해당 프로퍼티 타입으로 자동 변환
  71. public static object? ConvertValue(string value, Type t)
  72. {
  73. if (string.IsNullOrEmpty(value))
  74. {
  75. return null;
  76. }else if (Nullable.GetUnderlyingType(t) is Type underlyingType) {
  77. t = underlyingType;
  78. }
  79. if (t == typeof(bool) && bool.TryParse(value, out bool boolValue)) {
  80. return boolValue;
  81. }else if (t == typeof(byte) && byte.TryParse(value, out byte byteValue)) {
  82. return byteValue;
  83. }else if (t == typeof(sbyte) && sbyte.TryParse(value, out sbyte sbyteValue)) {
  84. return sbyteValue;
  85. }else if (t == typeof(char) && value.Length > 0) {
  86. return value[0];
  87. }else if (t == typeof(short) && short.TryParse(value, out short shortValue)) {
  88. return shortValue;
  89. }else if (t == typeof(ushort) && ushort.TryParse(value, out ushort ushortValue)) {
  90. return ushortValue;
  91. }else if (t == typeof(int) && int.TryParse(value, out int intValue)) {
  92. return intValue;
  93. }else if (t == typeof(uint) && uint.TryParse(value, out uint uintValue)) {
  94. return uintValue;
  95. }else if (t == typeof(long) && long.TryParse(value, out long longValue)) {
  96. return longValue;
  97. }else if (t == typeof(ulong) && ulong.TryParse(value, out ulong ulongValue)) {
  98. return ulongValue;
  99. }else if (t == typeof(float) && float.TryParse(value, out float floatValue)) {
  100. return floatValue;
  101. }else if (t == typeof(double) && double.TryParse(value, out double doubleValue)) {
  102. return doubleValue;
  103. }else if (t == typeof(decimal) && decimal.TryParse(value, out decimal decimalValue)) {
  104. return decimalValue;
  105. }else if (t == typeof(DateTime) && DateTime.TryParse(value, out DateTime dateTimeValue)) {
  106. return dateTimeValue;
  107. }else if (t == typeof(Guid) && Guid.TryParse(value, out Guid guidValue)) {
  108. return guidValue;
  109. }
  110. return value;
  111. }
  112. }
  113. }