private OperationStatus setupOperationStatus(Person p) { var opStatus = new OperationStatus(); opStatus.Data = p; return opStatus; }
public static OperationStatus CreateFromException(string message, Exception ex) { OperationStatus opStatus = new OperationStatus { Success = true, Messages = new List<string>(), }; if (ex != null) { opStatus.Messages.Add(ex.Message); opStatus.Success = false; } return opStatus; }
public OperationStatus Enroll(EnrollmentRequest enrollmentRequest) { try { using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork()) { // Verify that the provided enrollment data matches a person in the system var personQueryable = unitOfWork.Persons.GetQueryable() .Where(p => p.LastName == enrollmentRequest.LastName && p.Accounts.Any(a => a.Account.AccountNumber == enrollmentRequest.AccountNumber)); if (personQueryable.Any()) { var person = personQueryable.First(); // Verify that the person does not already have an account var personLoginQueryable = unitOfWork.PersonLogins.GetQueryable() .Where(p => p.PersonID == person.PersonID); if (personLoginQueryable.Any()) { return(new OperationStatus { Success = false, Messages = new List <string> { "The holder of this account is already registered in the system." } }); } else { // Verify that the username is not already used personLoginQueryable = unitOfWork.PersonLogins.GetQueryable() .Where(p => p.LoginID.ToLower() == enrollmentRequest.Username.ToLower()); if (personLoginQueryable.Any()) { return(new OperationStatus { Success = false, Messages = new List <string> { "The username is already in use." } }); } else { var passwordValidationStatus = PasswordUtils.ValidatePassword(enrollmentRequest.Password); if (passwordValidationStatus.Success) { var personLogin = new PersonLogin(); personLogin.PersonID = person.PersonID; personLogin.LoginID = enrollmentRequest.Username; // The stored password will be a hash based on a salt and the password provided var salt = PasswordUtils.CreateSalt(PASSWORD_SALT_SIZE); personLogin.Salt = salt; personLogin.HashedPassword = PasswordUtils.GenerateHashedPassword(enrollmentRequest.Password, salt); unitOfWork.PersonLogins.Add(personLogin); unitOfWork.Commit(); return(new OperationStatus { Success = true }); } else { return(passwordValidationStatus); } } } } else { return(new OperationStatus { Success = false, Messages = new List <string> { "There is no one in the system that matches the information provided" } }); } } } catch (Exception e) { return(OperationStatus.CreateFromException("Error deleting person.", e)); } }
public OperationStatus UpdatePassword(string user, string oldPassword, string newPassword) { try { using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork()) { var personLoginQueryable = unitOfWork.PersonLogins.GetQueryable().Where(p => p.LoginID == user); if (personLoginQueryable.Any()) { // Validate the old password var personLogin = personLoginQueryable.First(); var generatedHashedPassword = PasswordUtils.GenerateHashedPassword(oldPassword, personLogin.Salt); if (generatedHashedPassword == personLogin.HashedPassword) { // Now verify that the new password meet the criteria for valid passwords var passwordValidationStatus = PasswordUtils.ValidatePassword(newPassword); if (passwordValidationStatus.Success) { // Now generate a salt and hash for the new password var salt = PasswordUtils.CreateSalt(PASSWORD_SALT_SIZE); personLogin.Salt = salt; personLogin.HashedPassword = PasswordUtils.GenerateHashedPassword(newPassword, salt); unitOfWork.PersonLogins.Update(personLogin); unitOfWork.Commit(); return(new OperationStatus { Success = true }); } else { return(passwordValidationStatus); } } else { return(new OperationStatus { Success = false, Messages = new List <string> { "The old password provided is not correct" } }); } } else { return(new OperationStatus { Success = false, Messages = new List <string> { "The username provided does not match a user in the system" } }); } } } catch (Exception e) { return(OperationStatus.CreateFromException("Error deleting person.", e)); } }
public OperationStatus UpdatePerson(Person updatedPerson) { try { // Use the ValidationContext to validate the Product model against the product data annotations // before saving it to the database var validationContext = new ValidationContext(updatedPerson, serviceProvider: null, items: null); var validationResults = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(updatedPerson, validationContext, validationResults, true); // If there any exception return them in the return result if (!isValid) { OperationStatus opStatus = new OperationStatus(); opStatus.Success = false; foreach (ValidationResult message in validationResults) { opStatus.Messages.Add(message.ErrorMessage); } return(opStatus); } else { // Otherwise connect to the data source using the db context and save the // person to the database Person person; using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork()) { person = unitOfWork.Persons.GetQueryable().Where(p => p.PersonID == updatedPerson.PersonID) .Include("Addresses.Address") .Include("Phones") .First(); person.FirstName = updatedPerson.FirstName; person.LastName = updatedPerson.LastName; person.EmailAddress = updatedPerson.EmailAddress; updatePhone(person, updatedPerson, (int)PhoneTypes.Home, unitOfWork); updatePhone(person, updatedPerson, (int)PhoneTypes.Work, unitOfWork); updatePhone(person, updatedPerson, (int)PhoneTypes.Cell, unitOfWork); updateAddress(person, updatedPerson, (int)AddressTypes.Home, unitOfWork); updateAddress(person, updatedPerson, (int)AddressTypes.Work, unitOfWork); updateAddress(person, updatedPerson, (int)AddressTypes.Alternate, unitOfWork); unitOfWork.Persons.Update(person); unitOfWork.Commit(); } return(new OperationStatus { Success = true, Data = person }); } } catch (Exception e) { return(OperationStatus.CreateFromException("Error updating person.", e)); } }
public OperationStatus UpdatePerson(Person updatedPerson) { try { // Use the ValidationContext to validate the Product model against the product data annotations // before saving it to the database var validationContext = new ValidationContext(updatedPerson, serviceProvider: null, items: null); var validationResults = new List<ValidationResult>(); var isValid = Validator.TryValidateObject(updatedPerson, validationContext, validationResults, true); // If there any exception return them in the return result if (!isValid) { OperationStatus opStatus = new OperationStatus(); opStatus.Success = false; foreach (ValidationResult message in validationResults) { opStatus.Messages.Add(message.ErrorMessage); } return opStatus; } else { // Otherwise connect to the data source using the db context and save the // person to the database Person person; using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork()) { person = unitOfWork.Persons.GetQueryable().Where(p => p.PersonID == updatedPerson.PersonID) .Include("Addresses.Address") .Include("Phones") .First(); person.FirstName = updatedPerson.FirstName; person.LastName = updatedPerson.LastName; person.EmailAddress = updatedPerson.EmailAddress; updatePhone(person, updatedPerson, (int)PhoneTypes.Home, unitOfWork); updatePhone(person, updatedPerson, (int)PhoneTypes.Work, unitOfWork); updatePhone(person, updatedPerson, (int)PhoneTypes.Cell, unitOfWork); updateAddress(person, updatedPerson, (int)AddressTypes.Home, unitOfWork); updateAddress(person, updatedPerson, (int)AddressTypes.Work, unitOfWork); updateAddress(person, updatedPerson, (int)AddressTypes.Alternate, unitOfWork); unitOfWork.Persons.Update(person); unitOfWork.Commit(); } return new OperationStatus { Success = true, Data = person }; } } catch (Exception e) { return OperationStatus.CreateFromException("Error updating person.", e); } }
public OperationStatus InsertPerson(Person person) { try { // Use the ValidationContext to validate the Product model against the product data annotations // before saving it to the database var validationContext = new ValidationContext(person, serviceProvider: null, items: null); var validationResults = new List<ValidationResult>(); var isValid = Validator.TryValidateObject(person, validationContext, validationResults, true); // If there any exception return them in the return result if (!isValid) { OperationStatus opStatus = new OperationStatus(); opStatus.Success = false; foreach (ValidationResult message in validationResults) { opStatus.Messages.Add(message.ErrorMessage); } return opStatus; } else { // Otherwise connect to the data source using the db context and save the // person to the database using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork()) { unitOfWork.Persons.Add(person); unitOfWork.Commit(); } return new OperationStatus { Success = true, Data = person }; } } catch (Exception e) { return OperationStatus.CreateFromException("Error inserting person.", e); } }