private IEmail GetEmailModelProperties(EmailRequestModel model) { var emailModel = new EmailTypeFactory().Create(model.Type); var properties = emailModel.GetType().GetProperties(); var emailContent = new Dictionary <string, string>(model.Properties, StringComparer.OrdinalIgnoreCase); foreach (var property in properties) { var propertyValue = emailContent[property.Name]; if (property.PropertyType.IsEnum) { try { property.SetValue(emailModel, Enum.Parse(property.PropertyType, propertyValue.ToString())); } catch (Exception ex) { throw new DemoException(ex.Message, HttpStatusCode.BadRequest); } } else { property.SetValue(emailModel, propertyValue); } } return(emailModel); }
private ModelState ValidateEmailChildProperties() { var results = new List <ValidationResult>(); var emailModel = new EmailTypeFactory().Create(_model.Type); var properties = emailModel.GetType().GetProperties(); var emailContent = new Dictionary <string, string>(_model.Properties, StringComparer.OrdinalIgnoreCase); foreach (var property in properties) { if (emailContent.ContainsKey(property.Name)) { if (string.IsNullOrEmpty(emailContent[property.Name])) { var errorMessage = $"The '{property.Name}' value can not be empty."; var validationResult = new ValidationResult(errorMessage); results.Add(validationResult); } if (emailContent[property.Name].GetType().Equals(typeof(string))) { if (string.IsNullOrEmpty(emailContent[property.Name].ToString())) { var errorMessage = $"The '{property.Name}' value can not be empty."; var validationResult = new ValidationResult(errorMessage); results.Add(validationResult); } } } else { var errorMessage = $"The '{property.Name}' is missed."; var validationResult = new ValidationResult(errorMessage); results.Add(validationResult); } } return(new ModelState { IsValid = results.Any() ? false : true, ModelValidationResult = results }); }