Functions.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4. using Microsoft.Win32;
  5. using System.IO;
  6. using System.Windows.Media.Imaging;
  7. namespace RaspisKusach
  8. {
  9. public class Functions
  10. {
  11. // Получение направления по маршруту или поездке
  12. public static string GetRouteDirection(Trips trip)
  13. {
  14. return GetDepartureStationLocation(trip) + " - " + GetArrivalStationLocation(trip);
  15. }
  16. // Получение времени прибытия поезда на станцию
  17. public static DateTime GetArrivalTime(Stations station, Trips trip)
  18. {
  19. DateTime date = trip.TripStartDate;
  20. foreach (RoutesStations item in cnt.db.RoutesStations.Where(item => item.IdRoute == trip.IdRoute))
  21. {
  22. if (item.IdStation == station.IdStation)
  23. break;
  24. date += item.StopTime + item.TravelTime;
  25. }
  26. return date;
  27. }
  28. // Получение времени отбытия поезда со станции
  29. public static DateTime GetDepartureTime(Stations station, Trips trip)
  30. {
  31. DateTime date = trip.TripStartDate;
  32. foreach (RoutesStations item in cnt.db.RoutesStations.Where(item => item.IdRoute == trip.IdRoute))
  33. {
  34. date += item.StopTime;
  35. if (item.IdStation == station.IdStation)
  36. break;
  37. date += item.TravelTime;
  38. }
  39. return date;
  40. }
  41. // Получение станции отправления (первой)
  42. public static string GetDepartureStationLocation(Trips trip)
  43. {
  44. return cnt.db.RoutesStations.Where(item => item.IdRoute == trip.Routes.IdRoute).OrderByDescending(item => item.IdRouteStation).Select(item => item.Stations.Location).FirstOrDefault();
  45. }
  46. public static string GetDepartureStationLocation(Routes route)
  47. {
  48. return cnt.db.RoutesStations.Where(item => item.IdRoute == route.IdRoute).OrderByDescending(item => item.IdRouteStation).Select(item => item.Stations.Location).FirstOrDefault();
  49. }
  50. // Получение станции прибытия (последней)
  51. public static string GetArrivalStationLocation(Trips trip)
  52. {
  53. return cnt.db.RoutesStations.Where(item => item.IdRoute == trip.Routes.IdRoute).Select(item => item.Stations.Location).FirstOrDefault();
  54. }
  55. public static string GetArrivalStationLocation(Routes route)
  56. {
  57. return cnt.db.RoutesStations.Where(item => item.IdRoute == route.IdRoute).Select(item => item.Stations.Location).FirstOrDefault();
  58. }
  59. // Валидация номера телефона
  60. public static bool IsPhoneNumberCorrect(string phoneNumber)
  61. {
  62. foreach (char c in phoneNumber)
  63. if (!char.IsDigit(c))
  64. return false;
  65. if (phoneNumber.Length != 11)
  66. return false;
  67. return true;
  68. }
  69. // Валидация электронной почты
  70. public static bool IsEmailCorrect(string email)
  71. {
  72. return Regex.IsMatch(email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
  73. }
  74. // Валидация дня рождения
  75. public static bool IsDateOfBirthdayCorrect(DateTime Date)
  76. {
  77. return Date <= DateTime.Now;
  78. }
  79. // Валидация логина и пароля при входе
  80. public static bool IsLogAndPassCorrect(string login, string password)
  81. {
  82. return login != "" && password != "";
  83. }
  84. // Валидация логина и пароля
  85. public static bool IsLogEqualPass(string login, string password)
  86. {
  87. return login == password; // RE
  88. }
  89. // Валидация логина и пароля
  90. public static bool IsLengthCorrect(string str)
  91. {
  92. return str.Length >= 5;
  93. }
  94. // Проверка на правильность введеных данных при входе
  95. public static bool LoginCheck(string login, string password)
  96. {
  97. return cnt.db.Users.Select(item => item.Login + item.Password).Contains(login + Encrypt.GetHash(password));
  98. }
  99. // Проверка на уникальность логина
  100. public static bool IsLoginAlreadyTaken(string login)
  101. {
  102. return cnt.db.Users.Select(item => item.Login).Contains(login);
  103. }
  104. //// Проверка на уникальность электронной почты
  105. //public static bool IsEmailAlreadyTaken(string Email)
  106. //{
  107. // return cnt.db.Users.Select(item => item.).Contains(Email);
  108. //}
  109. //// Проверка на уникальность электронной почты
  110. //public static bool IsPhoneNumberAlreadyTaken(string Phone)
  111. //{
  112. // return cnt.db.Users.Select(item => item.).Contains(Phone);
  113. //}
  114. //Кодирование картинки
  115. public static byte[] BitmapSourceToByteArray(BitmapSource image)
  116. {
  117. using (var stream = new MemoryStream())
  118. {
  119. var encoder = new PngBitmapEncoder();
  120. encoder.Frames.Add(BitmapFrame.Create(image));
  121. encoder.Save(stream);
  122. return stream.ToArray();
  123. }
  124. }
  125. // Выбор картинки
  126. public static BitmapImage SelectImage()
  127. {
  128. #region Выбор картинки
  129. OpenFileDialog op = new OpenFileDialog
  130. {
  131. Title = "Выбрать изображение",
  132. Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
  133. "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
  134. "Portable Network Graphic (*.png)|*.png"
  135. };
  136. return op.ShowDialog() == true ? new BitmapImage(new Uri(op.FileName)) : null;
  137. #endregion
  138. }
  139. ////Декодирование картинки
  140. //public static BitmapImage NewImage(Users user)
  141. //{
  142. // MemoryStream ms = new MemoryStream(user.Image);
  143. // BitmapImage image = new BitmapImage();
  144. // image.BeginInit();
  145. // image.StreamSource = ms;
  146. // image.EndInit();
  147. // return image;
  148. //}
  149. }
  150. }