Exemple #1
0
        public CheckResult SaveUser()
        {
            using (var db = DbManager.CreateInstance())
            {
                UserValidator validator = new UserValidator();
                CheckResult check = validator.ValidateUser(db, this.CurrentUser);
                if (!check.Success)
                {
                    return check;
                }

                if (this.CurrentUser.UserId == 0)
                {
                    check = validator.ValidateIfUserExistInDatabase(db, this.CurrentUser);
                    if (!check.Success)
                    {
                        return check;
                    }
                }

                UserRepository rep = new UserRepository();
                rep.SaveUser(db, this.CurrentUser);
                db.SaveChanges();
                this.CurrentUser.AcceptChanges();

                if (this.CurrentUser.UserId == 0)
                {
                    this.AddUser();
                }

                return check;
            }
        }
Exemple #2
0
 private static void CheckIfExistUser(UserModel model, SmartCRMEntitiesModel db, CheckResult result)
 {
     UserRepository repository = new UserRepository();
     var user = repository.GetUserByModel(db, model);
     if (user == null)
     {
         result.Details.Add(
             new CheckResultDetail(CheckResultDetail.ErrorType.Error, "NoUser", "The username or password is incorect!"));
     }
 }
Exemple #3
0
        public CheckResult Login()
        {
            using (var db = DbManager.CreateInstance())
            {
                LoginValidator validator = new LoginValidator();
                var result = validator.ValidateLogin(this.CurrentLogin, db);
                if (result.Success)
                {
                    UserRepository rep = new UserRepository();
                    var userModel = rep.GetUserByModel(db, this.CurrentLogin);
                    Global.CurrentUser = userModel;
                }

                return result;
            }
        }
Exemple #4
0
        public CheckResult DeleteUser(UserModel focusedUser)
        {
            CheckResult check = CheckResult.Default;
            using (var db = DbManager.CreateInstance())
            {
                try
                {
                    UserRepository rep = new UserRepository();
                    rep.DeleteUser(db, focusedUser);
                    db.SaveChanges();
                    this.Users.Remove(focusedUser);
                }
                catch (Exception ex)
                {
                    check.Details.Add(new CheckResultDetail(CheckResultDetail.ErrorType.Error, "InvalidDeletion", "Cannot delete this user!"));
                    return check;
                }

                return check;
            }
        }
Exemple #5
0
 public UserModel GetUserByEmployeeId(uint employeeId)
 {
     using (var db = DbManager.CreateInstance())
     {
         UserRepository rep = new UserRepository();
         var user = rep.GetUserByEmployeeId(db, employeeId);
         return user;
     }
 }
Exemple #6
0
 private void LoadAllUsers(bool isActive)
 {
     using (var db = DbManager.CreateInstance())
     {
         UserRepository rep = new UserRepository();
         this.Users = rep.LoadAllUsers(db, isActive);
     }
 }
        public CheckResult SaveAccount()
        {
            CheckResult result = CheckResult.Default;
            uint employeeId = this.CurrentEmployee.Id;
            using (var db = DbManager.CreateInstance())
            {
                try
                {
                    if (this.CurentEmployeeIsDirty())
                    {
                        EmployeeValidator employeeValidator = new EmployeeValidator();
                        var checkEmployee = employeeValidator.ValidateEmployee(db, this.CurrentEmployee);

                        if (!checkEmployee.Success)
                        {
                            result.Details.AddRange(checkEmployee.Details);
                        }
                        else
                        {
                            EmployeeRepository employeeRepository = new EmployeeRepository();
                            employeeRepository.SaveEmployee(db, this.CurrentEmployee);
                            this.CurrentUser.EmployeeId = this.CurrentEmployee.Id;
                        }
                    }

                    if (this.CurentUserIsDirty())
                    {
                        UserValidator userValidator = new UserValidator();
                        var checkUser = userValidator.ValidateUser(db, this.CurrentUser);
                        if (!checkUser.Success)
                        {
                            result.Details.AddRange(checkUser.Details);
                        }
                        else
                        {
                            checkUser = userValidator.ValidateIfUserExistInDatabase(db, this.CurrentUser);
                            if (!checkUser.Success)
                            {
                                result.Details.AddRange(checkUser.Details);
                            }
                            else
                            {
                                UserRepository userRepository = new UserRepository();
                                userRepository.SaveUser(db, this.CurrentUser);
                            }
                        }
                    }

                    if (!result.Success)
                    {
                        this.CurrentEmployee.Id = employeeId;
                        db.ClearChanges();
                        return result;
                    }

                    db.SaveChanges();

                    this.CurrentUser.AcceptChanges();
                    this.CurrentEmployee.AcceptChanges();
                }
                catch (Exception ex)
                {
                    //TODO Log
                    this.CurrentEmployee.Id = employeeId;
                    db.ClearChanges();
                    result.Details.Add(new CheckResultDetail(CheckResultDetail.ErrorType.Error, "MessageBox", "Save failed!\n" + ex.Message));
                    return result;
                }
            }


            return result;
        }
        public UserModel GetUserByEmployeeId(uint employeeId)
        {
            using (var db = DbManager.CreateInstance())
            {
                UserRepository rep = new UserRepository();
                var user = rep.GetUserByEmployeeId(db, employeeId);
                if (user != null)
                {
                    user.AcceptChanges();
                }

                return user;
            }
        }
        public CheckResult DeleteUser(UserModel focusedUser)
        {
            CheckResult check = CheckResult.Default;
            using (var db = DbManager.CreateInstance())
            {
                try
                {
                    UserRepository rep = new UserRepository();
                    rep.DeleteUser(db, focusedUser);
                    db.SaveChanges();
                    this.Users.Remove(focusedUser);
                }
                catch (NullReferenceException nREx)
                {
                    //TODO Log
                    check.Details.Add(new CheckResultDetail(CheckResultDetail.ErrorType.Error, "", nREx.Message));
                    return check;
                }
                catch (DataStoreException)
                {
                    check.Details.Add(
                        new CheckResultDetail(
                            CheckResultDetail.ErrorType.Error,
                            "InvalidDeletion",
                            Messages.CannotDeleteLinkedItem));
                    return check;
                }
                catch (Exception)
                {
                    //TODO Log
                    check.Details.Add(
                        new CheckResultDetail(
                            CheckResultDetail.ErrorType.Error,
                            "InvalidDeletion",
                            "Deletion failed!"));
                    return check;
                }

                return check;
            }
        }