Beispiel #1
0
        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            string idStr     = TBId.Text.Trim();
            string firstname = TBFirstname.Text.Trim();
            string lastname  = TBLastname.Text.Trim();
            string email     = TBEmail.Text.Trim();
            string password  = PBPassword.Password;

            if (idStr.Equals("") || firstname.Equals("") || lastname.Equals("") || email.Equals("") || password.Equals(""))
            {
                MessageBox.Show("You must fill all the fields.", "Attention !", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            else
            {
                if (int.TryParse(idStr, out int id))
                {
                    User newUser = new User(id, firstname, lastname, email, password);
                    if (newUser.IsSavable())
                    {
                        if (!Functions.IsPasswordValid(password))
                        {
                            MessageBox.Show("This password format is invalid.", "Attention !", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }
                        else if (!Functions.IsEmailValid(email))
                        {
                            MessageBox.Show("This email format is invalid.", "Attention !", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }
                        else if (_usersModel.Exists <User>("Id", id))
                        {
                            MessageBox.Show("This id is already taken.", "Attention !", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }
                        else if (_usersModel.Exists <User>("Email", email))
                        {
                            MessageBox.Show("This email is already taken.", "Attention !", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }
                        else
                        {
                            try {
                                _usersModel.Add <User>(newUser);
                                _users.Add(newUser);
                                MessageBox.Show("The user has been added.", "User added !", MessageBoxButton.OK, MessageBoxImage.Information);
                                Close();
                            } catch (Exception ex) {
                                MessageBox.Show(ex.Message, "Attention !", MessageBoxButton.OK, MessageBoxImage.Warning);
                            }
                        }
                    }
                    else
                    {
                        SortedDictionary <string, string> errors = newUser.GetInvalidFields();
                        MessageBox.Show(errors.Values.First(), "Attention !", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                }
                else
                {
                    MessageBox.Show("The id must be a positive integer.", "Attention !", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }
 public ActionResult Add(UsersModel model)
 {
     model.sesion = SessionDB.start(Request, Response, false, model.db);
     try
     {
         if (model.Add())
         {
             //Log.write(this, "Add", LOG.REGISTRO, "SQL:" + sql, sesion);
             return(Json(new { msg = Notification.Succes("Usuario agregado con exito: " + model.Nombre) }));
         }
         else
         {
             return(Json(new { msg = Notification.Error("Error al agregar '" + model.Nombre + "' (" + model.ErrorMessage + ")") }));
         }
     }
     catch (Exception e)
     {
         return(Json(new { msg = Factory.Notification.Error(e.Message) }));
     }
 }