123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System;
- using System.Linq;
- using System.Text.RegularExpressions;
- using Microsoft.Win32;
- using System.IO;
- using System.Windows.Media.Imaging;
- namespace RaspisKusach
- {
- public class Functions
- {
- // Получение направления по маршруту или поездке
- public static string GetRouteDirection(Trips trip)
- {
- return GetDepartureStationLocation(trip.Routes) + " - " + GetArrivalStationLocation(trip.Routes);
- }
- // Получение времени прибытия поезда на станцию
- public static DateTime GetArrivalTime(Stations station, Trips trip)
- {
- DateTime date = trip.TripStartDate;
- foreach (RoutesStations item in cnt.db.RoutesStations.Where(item => item.IdRoute == trip.IdRoute))
- {
- if (item.IdStation == station.IdStation)
- break;
- date += item.StopTime + item.TravelTime;
- }
- return date;
- }
- // Получение времени отбытия поезда со станции
- public static DateTime GetDepartureTime(Stations station, Trips trip)
- {
- DateTime date = trip.TripStartDate;
- foreach (RoutesStations item in cnt.db.RoutesStations.Where(item => item.IdRoute == trip.IdRoute))
- {
- date += item.StopTime;
- if (item.IdStation == station.IdStation)
- break;
- date += item.TravelTime;
- }
- return date;
- }
- // Получение станции отправления (первой)
- public static string GetDepartureStationLocation(Routes route)
- {
- return cnt.db.RoutesStations.Where(item => item.IdRoute == route.IdRoute).OrderByDescending(item => item.IdRouteStation).Select(item => item.Stations.Location).FirstOrDefault();
- }
- // Получение станции прибытия (последней)
- public static string GetArrivalStationLocation(Routes route)
- {
- return cnt.db.RoutesStations.Where(item => item.IdRoute == route.IdRoute).Select(item => item.Stations.Location).FirstOrDefault();
- }
- // Получение количества свободных мест в вагоне
- public static int GetAvailableSeats(Carriages carriage)
- {
- //temp
- return 0;
- }
- // Валидация номера телефона
- public static bool IsPhoneNumberCorrect(string phoneNumber)
- {
- foreach (char c in phoneNumber)
- if (!char.IsDigit(c))
- return false;
- if (phoneNumber.Length != 11)
- return false;
- return true;
- }
- // Проверка электронной почты на правильность ввода
- public static bool IsEmailCorrect(string email)
- {
- return Regex.IsMatch(email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
- }
- // Проверка на уникальность электронной почты
- public static bool IsEmailAlreadyTaken(string Email)
- {
- return cnt.db.Users.Select(item => item.Email).Contains(Email);
- }
- // Валидация дня рождения
- public static bool IsDateOfBirthdayCorrect(DateTime Date)
- {
- return Date <= DateTime.Now;
- }
- // Валидация логина и пароля при входе
- public static bool IsLogAndPassCorrect(string login, string password)
- {
- return login != "" && password != "";
- }
- // Валидация логина и пароля
- public static bool IsLogEqualPass(string login, string password)
- {
- return login != password;
- }
- // Валидация логина и пароля
- public static bool IsLengthCorrect(string str)
- {
- return str.Trim().Length >= 5;
- }
- // Проверка на правильность введеных данных при входе
- public static bool LoginCheck(string login, string password)
- {
- return cnt.db.Users.Select(item => item.Login + item.Password).Contains(login + Encrypt.GetHash(password));
- }
- // Проверка на уникальность логина
- public static bool IsLoginAlreadyTaken(string login)
- {
- return cnt.db.Users.Select(item => item.Login).Contains(login);
- }
- // Преобразует из "string" в "String"
- public static string ToUlower(string str)
- {
- return str.Substring(0, 1).ToUpper() + str.Substring(1, str.Length);
- }
- // Получение всех станций в маршруте в виде строки
- public static string GetDirection(Routes route)
- {
- string stationsList = "";
- foreach (RoutesStations rs in cnt.db.RoutesStations.Where(item => item.IdRoute == route.IdRoute))
- stationsList += rs.Stations.Location == GetDepartureStationLocation(route) ? rs.Stations.Name : $"{rs.Stations.Name} → ";
- return stationsList;
- }
- //// Проверка на уникальность номера телефона
- //public static bool IsPhoneNumberAlreadyTaken(string Phone)
- //{
- // return cnt.db.Users.Select(item => item.).Contains(Phone);
- //}
- //Кодирование картинки
- public static byte[] BitmapSourceToByteArray(BitmapSource image)
- {
- using (var stream = new MemoryStream())
- {
- var encoder = new PngBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(image));
- encoder.Save(stream);
- return stream.ToArray();
- }
- }
- // Выбор картинки
- public static BitmapImage SelectImage()
- {
- #region Выбор картинки
- OpenFileDialog op = new OpenFileDialog
- {
- Title = "Выбрать изображение",
- Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
- "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
- "Portable Network Graphic (*.png)|*.png"
- };
- return op.ShowDialog() == true ? new BitmapImage(new Uri(op.FileName)) : null;
- #endregion
- }
- ////Декодирование картинки
- //public static BitmapImage NewImage(Users user)
- //{
- // MemoryStream ms = new MemoryStream(user.Image);
- // BitmapImage image = new BitmapImage();
- // image.BeginInit();
- // image.StreamSource = ms;
- // image.EndInit();
- // return image;
- //}
- }
- }
|