public bool Insert( ContactUsDetailsDto contactUsDetailsDto, int? addedByUserId, ref IList<string> errorMessages) { if (!contactUsDetailsDto.IsValid) { errorMessages = contactUsDetailsDto.ErrorMessages.ToList(); return false; } var contactUsLog = new ContactUsLog { Name = contactUsDetailsDto.Name, EmailAddress = contactUsDetailsDto.EmailAddress, Message = contactUsDetailsDto.Message, AddedByUserId = addedByUserId, AddedDateTime = DateTime.Now.ToGmtDateTime() }; _contactUsLogRepository.Insert(contactUsLog); if (addedByUserId.HasValue) { var userLog = new UserLog { UserId = addedByUserId.Value, UserLogTypeId = (int)UserLogType.Types.SubmittedContactUsMessage, AddedDateTime = DateTime.Now.ToGmtDateTime() }; _userLogRepository.Insert(userLog); } IEmail email = new Email(); foreach (var contactUsEmailAddress in email.ContactUsEmailAddresses) { email = new Email { ToEmailAddress = contactUsEmailAddress, ToRecipientName = (!string.IsNullOrWhiteSpace(contactUsDetailsDto.Name) ? contactUsDetailsDto.Name : "Unknown"), Subject = "Contact Us - Fallen Nova", EmailBody = contactUsDetailsDto.Message }; if (email.Dispatch()) { continue; } errorMessages.Add(string.Format("The contact us message wasn't sent successfully. Please contact the website administrator.")); return false; } UnitOfWork.Commit(); return true; }
public bool Insert( UserContactDetailsDto userContactDetailsDto, int addedByUserId, ref IList<string> errorMessages) { #region Validation if (!userContactDetailsDto.IsValid) { errorMessages = userContactDetailsDto.ErrorMessages.ToList(); return false; } if (_roleRepository.GetByUserId(addedByUserId).All(r => r.RoleId != (int)Role.Roles.Administrator)) { errorMessages.Add("Stop! You can't add a new user unless you're an administrator."); } if (errorMessages.Count > 0) { return false; } #endregion // Prepare the user entity. var addedDateTime = DateTime.Now.ToGmtDateTime(); var user = new User { UserStatusId = (int)UserStatus.Statuses.Active, FirstName = userContactDetailsDto.FirstName, Surname = userContactDetailsDto.Surname, EmailAddress = userContactDetailsDto.EmailAddress, HashedPassword = null, UnsuccessfulLoginAttempts = 0, AddedByUserId = addedByUserId, AddedDateTime = addedDateTime, ModifiedByUserId = addedByUserId, ModifiedDateTime = addedDateTime }; _userRepository.Insert(user); var userRole = new UserRole { UserId = user.UserId, RoleId = userContactDetailsDto.RoleId }; _userRoleRepository.Insert(userRole); var userLog = new UserLog { UserId = addedByUserId, UserLogTypeId = (int)UserLogType.Types.AddedUser, ActionAgainstUserId = userContactDetailsDto.UserId, AddedDateTime = DateTime.Now.ToGmtDateTime() }; _userLogRepository.Insert(userLog); // Dispatch email. IEmail email = new Email { ToEmailAddress = userContactDetailsDto.EmailAddress, ToRecipientName = string.Format("{0} {1}", userContactDetailsDto.FirstName, userContactDetailsDto.Surname), Subject = "Verify Email Address - Fallen Nova", EmailBody = "Email body goes here" }; if (!email.Dispatch()) { errorMessages.Add(string.Format("The user's account was created however the verification email wasn't sent succesfully. Contact the web admin.")); return false; } userContactDetailsDto.UserId = user.UserId; UnitOfWork.Commit(); return true; }