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; } }
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; }