123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- using System;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Text.RegularExpressions;
- namespace Cafe.AddWindows
- {
- public partial class AddEmployeeWindow : Window
- {
- public AddEmployeeWindow()
- {
- InitializeComponent();
- RoleBox.ItemsSource = Connection.db.Roles.Select(item => item.RoleName).ToList();
- }
- private void RegistrationClick(object sender, RoutedEventArgs e)
- {
- bool regRes = RegistrationValidation(Login.Text.ToString(), Password.Password.ToString(), FirstName.Text.ToString(),
- SecondName.Text.ToString(), MiddleName.Text.ToString(), Email.Text.ToString(),
- RoleBox.SelectedItem.ToString());
- if (regRes)
- {
- int roleID = GetRoleID(RoleBox.SelectedItem.ToString());
- Users user = new Users()
- {
- Login = Login.Text,
- Password = Encrypt.Hash(Password.Password),
- SecondName = SecondName.Text,
- FirstName = FirstName.Text,
- Email = Email.Text,
- IDRole = roleID,
- IsFired = false
- };
- if (MiddleName.Text != "")
- {
- user.MiddleName = MiddleName.Text;
- }
- Connection.db.Users.Add(user);
- Connection.db.SaveChanges();
- MainWindow mainWindow = new MainWindow();
- mainWindow.Show();
- this.Close();
- }
- }
- public bool RegistrationValidation(string login, string password, string Fname, string Sname, string Mname, string email, string roleName)
- {
- if (login == "" || password == "" || Fname == "" || Sname == "" || email == "" || roleName == "")
- {
- ErrorWindow errorWindow = new ErrorWindow("пустые поля");
- errorWindow.Show();
- return false;
- }
- if (UserLoginIsExist(login))
- {
- ErrorWindow errorWindow = new ErrorWindow("такой пользователь уже существует");
- errorWindow.Show();
- return false;
- }
- if (!IsValidEmail(email))
- {
- ErrorWindow errorWindow = new ErrorWindow("неверный формат почты");
- errorWindow.Show();
- return false;
- }
- return true;
- }
- public bool UserLoginIsExist(string login)
- {
- return Connection.db.Users.Select(item => item.Login).Contains(login);
- }
- public int GetRoleID(string roleName)
- {
- return Connection.db.Roles.Where(item => item.RoleName == roleName).Select(item => item.ID).FirstOrDefault();
- }
- public bool IsValidEmail(string email)
- {
- string regex = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$";
- if (Regex.IsMatch(email, regex))
- return true;
- else
- return false;
- }
- private void BackButtonClick(object sender, RoutedEventArgs e)
- {
- MainWindow mainWindow = new MainWindow();
- mainWindow.Show();
- this.Close();
- }
- private void TextChanged(object sender, TextChangedEventArgs e)
- {
- if (Regex.IsMatch((((TextBox)sender).Text).ToString(), "[^А-я-:]"))
- {
- ((TextBox)sender).Text = ((TextBox)sender).Text.Remove(((TextBox)sender).Text.Length - 1);
- ((TextBox)sender).SelectionStart = ((TextBox)sender).Text.Length;
- }
- }
- }
- }
|