Пример #1
0
 private void Login(object obj)
 {
     try
     {
         using (TastyAndHealthyContext context = new TastyAndHealthyContext())
         {
             User user = context.Users.Where(x => (x.Email == UserLogin || x.Login == UserLogin)).FirstOrDefault();
             if (user != null)
             {
                 if (EncryptionUtility.DecryptDate(user.Password) == Password)
                 {
                     MainWindow mainWindow = new MainWindow(user);
                     mainWindow.Show();
                     foreach (Window el in Application.Current.Windows)
                     {
                         if (el.ToString().Contains("Login"))
                         {
                             el.Close();
                             break;
                         }
                     }
                 }
                 else
                 {
                     MessageBox.Show("Invalid email or password!");
                 }
             }
             else
             {
                 MessageBox.Show("Invalid email or password!");
             }
         }
     }
     catch (Exception) { MessageBox.Show("Error connection to database!"); }
 }
 private void Registration(object obj)
 {
     if (Password == ConfirmPassword)
     {
         using (TastyAndHealthyContext context = new TastyAndHealthyContext())
         {
             if (!context.Users.Where(x => (x.Email == Email || x.Email == Name || x.Login == Email || x.Login == Name)).Any())
             {
                 User user = new User();
                 user.Login         = Name;
                 user.Email         = Email;
                 user.Password      = EncryptionUtility.EncryptData(Password);
                 user.Age           = Age;
                 user.Height        = Height;
                 user.CurrentWeight = CurrentWeight;
                 user.TargetWeight  = TargetWeight;
                 user.BloodType     = context.BloodTypes
                                      .Where(x => x.Name == BloodType).FirstOrDefault();
                 user.Sex = Sex;
                 context.Users.Add(user);
                 context.SaveChanges();
                 MainWindow mainWindow = new MainWindow(user);
                 mainWindow.Show();
                 foreach (Window el in Application.Current.Windows)
                 {
                     if (el.ToString().Contains("Registration"))
                     {
                         el.Close();
                         break;
                     }
                 }
             }
             else
             {
                 if (context.Users.Where(x => x.Email == Email).Any())
                 {
                     MessageBox.Show("User with this email already exists!!!");
                 }
                 else
                 {
                     MessageBox.Show("User with this login already exists!!!");
                 }
             }
         }
     }
     else
     {
         MessageBox.Show("Password confirmation does not match password");
     }
 }