private static string ValidateContactTypeQuantity(MessageRequest message, ContactType contactType) { var chosenContactsCount = message.Contacts.Where(x => x?.ContactType == contactType).Count(); return(chosenContactsCount == 1 ? null : $"Nie istnieje dokładnie jeden wpis o żądanym rodzaju w kontaktach. Jest ich {chosenContactsCount}"); }
private static string ValidateEmailFormat(MessageRequest message, ContactType contactType) { var eMail = message.Contacts.Single(x => x?.ContactType == contactType).Value; try { var address = new MailAddress(eMail); return(null); } catch (Exception) { return($"Wprowadzono niepoprawny format adresu e-mail: {eMail}"); } }
public static MessageResponse SendMessage(MessageRequest messageRequest, out string mailBody) { var configurator = new ConfigHandler(); const string subject = "WcfDemo Sample Header by ka_res"; const string host = "smtp.gmail.com"; mailBody = configurator.GetMailBody(); var emailAddress = messageRequest.LegalForm == LegalForm.Person ? messageRequest.Contacts.Single(x => x?.ContactType == ContactType.Email).Value : messageRequest.Contacts.Single(x => x?.ContactType == ContactType.OfficeEmail).Value; var eMail = new MailMessage(); eMail.From = new MailAddress(configurator.GetUserName()); eMail.To.Add(new MailAddress(emailAddress)); eMail.IsBodyHtml = true; eMail.Subject = subject; eMail.Body = mailBody; var smtpClient = new SmtpClient { Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(eMail.From.Address, configurator.GetPassword()), Host = host }; try { smtpClient.Send(eMail); } catch (Exception) { return(new MessageResponse { ReturnCode = ReturnCode.InternalError, ErrorMessage = "Wystąpił błąd związany z wysłaniem wiadomości za pomocą GMail" }); } return(new MessageResponse { ReturnCode = ReturnCode.Success, ErrorMessage = null }); }
private void SendMessage(MessageRequest message, out string mailBody, out MessageResponse messageResponse) { mailBody = string.Empty; try { var response = SmtpClientHelper.SendMessage(message, out var mailBodyFromFile); messageResponse = response; mailBody = mailBodyFromFile; } catch (Exception) { messageResponse = new MessageResponse { ReturnCode = ReturnCode.InternalError, ErrorMessage = $"Wystąpił błąd związany z połączeniem z usługą Gmail" }; } }
private void SaveMessageData(MessageRequest message, string mailBody, MessageResponse messageResponse) { var messageRequestModel = new MessageRequestModel() { FirstName = message.FirstName, LastName = message.LastName, MailBody = mailBody, LegalFormId = (int)message.LegalForm }; messageRequestModel.Contacts = message.Contacts .Where(x => x != null) .Select(x => new ContactModel { ContactTypeId = (int)x?.ContactType, MessageRequestId = messageRequestModel.Id, Value = x?.Value }).ToList(); _messageRequestRepository.Add(messageRequestModel); foreach (var contact in message.Contacts) { var contactModel = new ContactModel { ContactTypeId = (int)contact.ContactType, MessageRequestId = messageRequestModel.Id, Value = contact.Value }; _contactRepository.Add(contactModel); } var messageResponseModel = new MessageResponseModel { ErrorMessage = messageResponse.ErrorMessage, MessageRequestId = messageRequestModel.Id, ReturnCodeId = (int)messageResponse.ReturnCode }; _messageResponseRepository.Add(messageResponseModel); }
private static MessageResponse ValidateForCompany(MessageRequest message) { var lastNamePresence = ValidateCompanyLastName(message); if (!string.IsNullOrEmpty(lastNamePresence)) { return(new MessageResponse { ReturnCode = ReturnCode.ValidationError, ErrorMessage = lastNamePresence }); } var contactTypeQuantity = ValidateContactTypeQuantity(message, ContactType.OfficeEmail); if (!string.IsNullOrEmpty(contactTypeQuantity)) { return(new MessageResponse { ReturnCode = ReturnCode.ValidationError, ErrorMessage = contactTypeQuantity }); } var emailFormat = ValidateEmailFormat(message, ContactType.OfficeEmail); if (!string.IsNullOrEmpty(emailFormat)) { return(new MessageResponse { ReturnCode = ReturnCode.ValidationError, ErrorMessage = emailFormat }); } return(new MessageResponse { ReturnCode = ReturnCode.Success, ErrorMessage = null }); }
private static MessageResponse ValidateForPerson(MessageRequest message) { var personalData = ValidateForPersonalData(message); if (!string.IsNullOrEmpty(personalData)) { return(new MessageResponse { ReturnCode = ReturnCode.ValidationError, ErrorMessage = personalData }); } var contactTypeQuantity = ValidateContactTypeQuantity(message, ContactType.Email); if (!string.IsNullOrEmpty(contactTypeQuantity)) { return(new MessageResponse { ReturnCode = ReturnCode.ValidationError, ErrorMessage = contactTypeQuantity }); } var emailFormat = ValidateEmailFormat(message, ContactType.Email); if (!string.IsNullOrEmpty(emailFormat)) { return(new MessageResponse { ReturnCode = ReturnCode.ValidationError, ErrorMessage = emailFormat }); } return(new MessageResponse { ReturnCode = ReturnCode.Success, ErrorMessage = null }); }
public MessageResponse Send(MessageRequest message) { var messageResponse = ValidationHelper.ValidateMessage(message); if (messageResponse.ReturnCode != ReturnCode.Success) { return(messageResponse); } SendMessage(message, out var mailBody, out messageResponse); if (messageResponse.ReturnCode != ReturnCode.Success) { return(messageResponse); } else { SaveMessageData(message, mailBody, messageResponse); } return(messageResponse); }
private static string ValidateCompanyLastName(MessageRequest message) { return(!string.IsNullOrWhiteSpace(message.LastName) ? null : $"Nie podano nazwy firmy"); }
private static string ValidateForPersonalData(MessageRequest message) { return(!string.IsNullOrWhiteSpace(message.FirstName) && !string.IsNullOrWhiteSpace(message.LastName) ? null : $"Nie podano imienia lub nazwiska (imię: {message.FirstName}, nazwisko: {message.LastName})"); }
private static void PrepareMessageRequest(out string returnMessage, out ConsoleDisplayType consoleDisplayType) { PrintConsoleLog("Następuje przygotowanie serwisu", ConsoleDisplayType.Initialization); var messageService = _container.Resolve <IMessageService>(); PrintConsoleLog("Wiadomość będzie teraz opracowywana", ConsoleDisplayType.Preparing); var messageRequest = new MessageRequest(); string legalForm; bool isInvalid = true; var legalFormEnumCount = Enum.GetNames(typeof(LegalForm)).Length; do { PrintConsoleLog("Wprowadź rodzaj kontaktu", ConsoleDisplayType.Instruction); PrintConsoleLog("Dostępne rodzaje to:\r\n\t\t\t\t1 - osoba\r\n\t\t\t\t2 - firma", ConsoleDisplayType.Option); legalForm = AskForInput(); isInvalid = !int.TryParse(legalForm, out int legalFormVerified) || Convert.ToInt32(legalForm) > legalFormEnumCount || Convert.ToInt32(legalForm) < 1; if (isInvalid) { PrintConsoleLog("Niepoprawna wartość!", ConsoleDisplayType.Missing); } } while (isInvalid); var legalFormNumber = Convert.ToInt32(legalForm); var legalFormEnum = (LegalForm)Enum.ToObject(typeof(LegalForm), legalFormNumber); messageRequest.LegalForm = legalFormEnum; string firstName; if (legalFormEnum == LegalForm.Person) { PrintConsoleLog("Wprowadź imię kontaktu", ConsoleDisplayType.Instruction); firstName = AskForInput(); messageRequest.FirstName = firstName; } string lastName; PrintConsoleLog(legalFormEnum == LegalForm.Person ? "Wprowadź nazwisko kontaktu:" : "Wprowadź nazwę firmy", ConsoleDisplayType.Instruction); lastName = AskForInput(); messageRequest.LastName = lastName; AskForContacts(messageRequest); PrintConsoleLog("Następuje próba wysłania wiadomości", ConsoleDisplayType.Information); var message = messageService.Send(messageRequest); PrintConsoleLog("Oczekiwanie na odpowiedź serwisu", ConsoleDisplayType.Information); returnMessage = string.Empty; switch (message.ReturnCode) { case ReturnCode.InternalError: returnMessage = message.ErrorMessage; consoleDisplayType = ConsoleDisplayType.InternalError; break; case ReturnCode.ValidationError: returnMessage = message.ErrorMessage; consoleDisplayType = ConsoleDisplayType.ValidationError; break; case ReturnCode.Success: returnMessage = null; consoleDisplayType = ConsoleDisplayType.Success; break; default: returnMessage = "Nieznany błąd"; consoleDisplayType = ConsoleDisplayType.InternalError; break; } }
private static void AskForContacts(MessageRequest messageRequest) { bool isInvalidCountLimit = true; string contactCountLimit; do { PrintConsoleLog("Podaj maksymalną ilość kontaktów, które chcesz wprowadzić", ConsoleDisplayType.Instruction); contactCountLimit = AskForInput(); isInvalidCountLimit = !int.TryParse(contactCountLimit, out int contactCountLimitVerified); if (isInvalidCountLimit) { PrintConsoleLog("Niepoprawna wartość!", ConsoleDisplayType.Missing); } } while (isInvalidCountLimit); var contactCountLimitNumber = Convert.ToInt32(contactCountLimit); var contacts = new Contact[contactCountLimitNumber]; PrintConsoleLog("Wprowadź inforamcje kontaktowe", ConsoleDisplayType.Instruction); ConsoleKeyInfo input; int counter = 0; do { var contact = new Contact(); PrintConsoleLog($"Istnieje możliwość wprowadzaenia do {contactCountLimitNumber} rodzajów informacji kontaktowych", ConsoleDisplayType.Information); var contactTypeEnumCount = Enum.GetNames(typeof(ContactType)).Length; bool isInvalidContactType = true; string contactType; do { PrintConsoleLog("Podaj rodzaj informacji kontakowej", ConsoleDisplayType.Instruction); PrintConsoleLog("Dostępne rodzaje to:\r\n\t\t\t\t1 - telefon komórkowy\r\n\t\t\t\t2 - fax\r\n\t\t\t\t3 - e-mail\r\n\t\t\t\t" + "4 - telefon służbowy\r\n\t\t\t\t5 - fax służbowy\r\n\t\t\t\t6 - e-mail służbowy", ConsoleDisplayType.Option); contactType = AskForInput(); isInvalidContactType = !int.TryParse(contactType, out int contactTypeVerified) || Convert.ToInt32(contactType) > contactTypeEnumCount || Convert.ToInt32(contactType) < 1; if (isInvalidContactType) { PrintConsoleLog("Niepoprawna wartość!", ConsoleDisplayType.Missing); } } while (isInvalidContactType); var contactTypeNumber = Convert.ToInt32(contactType); var contactTypeEnum = (ContactType)Enum.ToObject(typeof(ContactType), contactTypeNumber); contact.ContactType = contactTypeEnum; PrintConsoleLog("Podaj wartość informacji kontaktowej", ConsoleDisplayType.Instruction); string value; value = AskForInput(); contact.Value = value; contacts[counter] = contact; PrintConsoleLog($"Wciśnij dowolny przycisk inny od ESC,\r\n\t\t\tby dodać nową inforamcję kontaktową...", ConsoleDisplayType.Option); input = Console.ReadKey(); Console.WriteLine(); counter++; if (counter == contactCountLimitNumber) { PrintConsoleLog($"Osiągnięto zaplanowany limit {contactCountLimitNumber} kontaktów", ConsoleDisplayType.Notification); } } while (input.Key != ConsoleKey.Escape && counter < contactCountLimitNumber); messageRequest.Contacts = contacts; }