You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.0 KiB

8 months ago
8 months ago
8 months ago
8 months ago
  1. using CustomerOnlineV2.Common.Configs;
  2. using Microsoft.AspNetCore.Http;
  3. using System;
  4. using System.Text.RegularExpressions;
  5. namespace CustomerOnlineV2.Common.Helper
  6. {
  7. public static class Utilities
  8. {
  9. public static string GetIpAddressv2(HttpContext httpContext)
  10. {
  11. var ipDetails = httpContext.Connection.RemoteIpAddress;
  12. return ipDetails.ToString();
  13. }
  14. public static string ReadAppSettings(string key)
  15. {
  16. return ConfigurationManager.AppSettings[key];
  17. }
  18. public static string ReadFromAppSettings(string key, string defValue = "")
  19. {
  20. return ConfigurationManager.AppSettings[key] ?? defValue;
  21. }
  22. public static decimal TryParseDecimal(string amount)
  23. {
  24. decimal output;
  25. decimal.TryParse(amount, out output);
  26. return output;
  27. }
  28. public static string GetRowCellValue(object v)
  29. {
  30. try
  31. {
  32. if (v == DBNull.Value)
  33. return "";
  34. else
  35. return v.ToString();
  36. }
  37. catch (Exception)
  38. {
  39. return "";
  40. }
  41. }
  42. public static int GetRowCellValueInt(object v)
  43. {
  44. try
  45. {
  46. if (v == DBNull.Value)
  47. return 101;
  48. else
  49. return Convert.ToInt32(v);
  50. }
  51. catch (Exception)
  52. {
  53. return 101;
  54. }
  55. }
  56. public static string ShowDecimal(string strVal)
  57. {
  58. if (strVal != "")
  59. return string.Format("{0:0,0.00}", double.Parse(strVal));
  60. else
  61. return strVal;
  62. }
  63. public static String ShowDecimal2(String strVal)
  64. {
  65. if (strVal != "")
  66. return TruncateDecimalPlaces(decimal.Parse(strVal), 2).Value.ToString("0.00");
  67. else
  68. return strVal;
  69. }
  70. public static decimal? TruncateDecimalPlaces(this decimal? value, int places)
  71. {
  72. if (value == null)
  73. {
  74. return null;
  75. }
  76. return Math.Floor((decimal)value * (decimal)Math.Pow(10, places)) / (decimal)Math.Pow(10, places);
  77. } //
  78. public static string GenerateOTP()
  79. {
  80. string[] strChar = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
  81. Random random = new Random();
  82. string otpChar = strChar[random.Next(0, 10)] + strChar[random.Next(0, 10)] +
  83. strChar[random.Next(0, 10)] + strChar[random.Next(0, 10)] +
  84. strChar[random.Next(0, 10)] + strChar[random.Next(0, 10)];
  85. return otpChar;
  86. }
  87. public static bool IsValidEmail(this string email)
  88. {
  89. return Regex.IsMatch(email ?? "", ApplicationConfig.ReadWebConfig("regex"), RegexOptions.IgnoreCase);
  90. }
  91. }
  92. }