Пример #1
0
 private void ExexuteDeleteUserCommand()
 {
     try
     {
         if (SelectedUser.IdUser == 0)
         {
             throw new Exception("Нельзя удалить\nадминистратора");
         }
         if (SelectedUser == null)
         {
             throw new Exception("Необходимо выбрать пользователя");
         }
         using (CP.UnitOfWork.UnitOfWork unitOfWork = new CP.UnitOfWork.UnitOfWork())
         {
             if (unitOfWork.ReservationRepository.GetAll().Where(a => a.IdUser == SelectedUser.IdUser).Count() != 0)
             {
                 throw new Exception("Выбранного пользователя\nнельзя удалить.\nОн забронировал рейс");
             }
             unitOfWork.UserRepository.Delete(SelectedUser.IdUser);
             unitOfWork.Save();
             SelectedUser = unitOfWork.UserRepository.GetAll().Last();
             Users        = unitOfWork.UserRepository.GetAll().ToList();
         }
     }
     catch (Exception ex)
     {
         Message message = new Message(ex.Message);
         message.Show();
     }
 }
 private void ExexuteUpdateCommand(object obj)
 {
     try
     {
         var values = (object[])obj;
         if (_name != "" || _surname != "" || _mail != "" || Phone != "")
         {
             using (CP.UnitOfWork.UnitOfWork unitOfWork = new CP.UnitOfWork.UnitOfWork())
             {
                 User        user           = unitOfWork.UserRepository.Get(id);
                 PasswordBox password       = (PasswordBox)values[0];
                 PasswordBox repeatPassword = (PasswordBox)values[1];
                 if (password.Password != "")
                 {
                     MD5    md5      = new MD5CryptoServiceProvider();
                     byte[] checkSum = md5.ComputeHash(Encoding.UTF8.GetBytes(password.Password));
                     string pass     = BitConverter.ToString(checkSum).Replace("-", String.Empty);
                     if (pass != user.Password)
                     {
                         throw new Exception("Неверный пароль!!!");
                     }
                     if (repeatPassword == null)
                     {
                         throw new Exception("Новый пароль не указан!!!");
                     }
                     if (repeatPassword.Password.Length != 8)
                     {
                         throw new Exception("Новый пароль\nнедопустимой длины!!!");
                     }
                     checkSum      = md5.ComputeHash(Encoding.UTF8.GetBytes(repeatPassword.Password));
                     pass          = BitConverter.ToString(checkSum).Replace("-", String.Empty);
                     user.Password = pass;
                 }
                 if (unitOfWork.UserRepository.GetAll().Where(a => a.Name == Name).Count() == 1 &&
                     user.Name != Name)
                 {
                     throw new Exception("Такой ник уже существует");
                 }
                 user.Name         = Name;
                 user.Surname      = Surname;
                 user.Mail         = Mail;
                 user.Phone_number = Phone;
                 unitOfWork.Save();
                 throw new Exception("Сохранено успешно");
             }
         }
         else
         {
             throw new Exception("Поля не могут быть пустыми");
         }
     }
     catch (Exception a)
     {
         Message message = new Message(a.Message);
         message.Show();
     }
 }
 private void ExexuteAddPlaneCommand(object obj)
 {
     try
     {
         Window window = (Window)obj;
         if (_name == null || _name == "" || _seats == 0 || _cruisingSpeed == 0 || _maxHeight == 0)
         {
             throw new Exception("Все поля\nнеобходимо заполнить!");
         }
         using (CP.UnitOfWork.UnitOfWork unitOfWork = new CP.UnitOfWork.UnitOfWork())
         {
             if (_idplane != -1)
             {
                 Plane plane = unitOfWork.PlaneRepository.Get(_idplane);
                 if (unitOfWork.PlaneRepository.GetAll().Where(a => a.Name == _name).Count() == 1 &&
                     plane.Name != Name)
                 {
                     throw new Exception("Такой самолет уже существует\nВыберите другое имя");
                 }
                 plane.Name = Name;
                 if (plane.Seats > Seats)
                 {
                     IEnumerable <int> Flight = unitOfWork.FlightRepository.GetAll()
                                                .Where(a => a.IdPlane == plane.IdPlane)
                                                .Select(a => a.IdFlight);
                     foreach (int idFlightCurrentPlane in Flight)
                     {
                         if (unitOfWork.ReservationRepository.GetAll()
                             .Where(a => a.IdFlight == idFlightCurrentPlane)
                             .Where(a => a.YourSeat > Seats)
                             .Select(a => a.YourSeat)
                             .Count() != 0)
                         {
                             throw new Exception("Недопустимо\nизменение вместимости\nМестa забронированы");
                         }
                     }
                 }
                 plane.Seats         = Seats;
                 plane.CruisingSpeed = CruisingSpeed;
                 plane.MaxHeight     = MaxHeight;
                 unitOfWork.Save();
                 window.Close();
             }
             else
             {
                 if (unitOfWork.PlaneRepository.GetAll().Where(a => a.Name == _name).Count() == 1)
                 {
                     throw new Exception("Такой самолет уже существует\nВыберите другое имя");
                 }
                 unitOfWork.PlaneRepository.Create(new Plane(Name, Seats, CruisingSpeed, MaxHeight));
                 unitOfWork.Save();
                 window.Close();
             }
         }
     }
     catch (Exception ex)
     {
         Message message = new Message(ex.Message);
         message.Show();
     }
 }