public async Task <IActionResult> Contact(CreateContactMessageCommand command) { if (!ModelState.IsValid) { ViewBag.ShowMessage = true; ViewBag.Message = "Something went wrong"; return(View()); } try { CreateContactMessageValidator.CommandValidation(command); await _contactMessageService.AddContactMessageAsync(command); ViewBag.Added = true; return(View()); } catch (InternalSystemException ex) { ViewBag.ShowMessage = true; ViewBag.Message = ex.Message; return(View()); } catch (Exception) { ViewBag.ShowMessage = true; ViewBag.Message = "Something went wrong!"; return(View()); } }
public async Task AddContactMessageAsync(CreateContactMessageCommand command) { var contactmessage = new ContactMessage(command.Name, command.Email, command.Text); await _context.ContactMessages.AddAsync(contactmessage); await _context.SaveChangesAsync(); }
public static void CommandValidation(CreateContactMessageCommand command) { if (command.Name == null || command.Email == null || command.Text == null) { throw new InternalSystemException("Fields can't be empty!"); } else if (!nameRegex.IsMatch(command.Name) || !emailRegex.IsMatch(command.Email) || !textRegex.IsMatch(command.Text)) { throw new InternalSystemException("Please provide correct data!"); } }
public async Task <string> CreateMessageAsync(CreateContactMessageCommand cmd) { var dto = new ContactMessageDTO { ID = Guid.NewGuid(), Name = cmd.Name, Phone = cmd.Phone, Email = cmd.Email, Message = cmd.Message, CreatedOnUtc = DateTime.UtcNow }; var msgCode = await ContactMessageDAL.InsertMessageAsync(dto).ConfigureAwait(false); if (msgCode.IsSuccess()) { var subject = $"[mylightangel.com]: Contact message from {dto.Email}"; var body = $"<p>Name: {dto.Name}</p><p>Phone: {dto.Phone}</p><p>Email: {dto.Email}</p><p>Message: {dto.Message}</p>"; msgCode = await EmailSender.SendAsync(subject, body).ConfigureAwait(false); } return(msgCode); }