Esempio n. 1
0
        public async Task <bool> IsSatisfiedByAsync(TEntity entity, Execute execute)
        {
            return(await Task.Factory.StartNew(() =>
            {
                var value = Selector(entity);
                Regex regex = null;

                try
                {
                    regex = new Regex(RegexExpression);
                }
                catch (Exception ex)
                {
                    if (execute != null)
                    {
                        execute.AddException(ex, "Invalid regex", RegexExpression);
                    }

                    return false;
                }

                var result = value.IsNotEmpty() && regex.IsMatch(value);

                if (!result && execute != null && !string.IsNullOrEmpty(Message))
                {
                    execute.AddMessage(ExecuteMessageType.Error, Message, MessageArgs);
                }

                return result;
            }));
        }
Esempio n. 2
0
        public void AddMessageExceptionMessageArgs()
        {
            var execute = new Execute();

            execute.AddException(new Exception("Check this message"), "Errors: {0} - {1}", "Three", "Four");

            execute.HasErro.Should().Be(true, "There is error message");
            execute.HasWarning.Should().Be(false, "There is warning message");
            execute.HasException.Should().Be(true, "There is exception message");

            execute.Messages.Count.Should().Be(1, "There isn 1 message");

            var message = execute.Messages[0];

            message.Message.Should().Be("Errors: Three - Four", "Needs to format the message");
            message.MessageInternal.MessageException.Count.Should().Be(1, "Had been added one exception");
            message.MessageInternal.MessageException[0].Should().Be("Check this message", "That is the exception message");
        }
Esempio n. 3
0
        public async Task <Execute> SendAsync(string from, string fromName, string to, string toName, string subject, string body)
        {
            var result = new Execute();

            if (ApplicationSettings.Mail == null ||
                ApplicationSettings.Mail.MailGunKey.IsEmpty() ||
                ApplicationSettings.Mail.MailGunDomain.IsEmpty())
            {
                result.AddMessage(ExecuteMessageType.Error, "MailGun API Key/Domain is not defined on ApplicationSettings.");
                return(result);
            }

            try
            {
                var client = new HttpClient();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes("api" + ":" + ApplicationSettings.Mail.MailGunKey)));

                var form = new Dictionary <string, string>
                {
                    ["from"]    = $"{fromName} <{from}>",
                    ["to"]      = $"{toName} <{to}>",
                    ["subject"] = subject,
                    ["text"]    = body
                };

                var response = await client.PostAsync($"https://api.mailgun.net/v3/{ApplicationSettings.Mail.MailGunDomain}/messages", new FormUrlEncodedContent(form));

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    result.AddMessage(ExecuteMessageType.Error, $"Error sending email by MailGun. HTTP: {response.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                result.AddException(ex, "Error sending email by MailGun");
            }

            return(await Task.FromResult(result));
        }
Esempio n. 4
0
        public async Task <Execute> SendAsync(string from, string fromName, string to, string toName, string subject, string body)
        {
            var result = new Execute();

            if (ApplicationSettings.Mail == null || ApplicationSettings.Mail.SendGridKey.IsEmpty())
            {
                result.AddMessage(ExecuteMessageType.Error, "SendGrid API Key is not defined on ApplicationSettings.");
                return(result);
            }

            try
            {
                var client   = new SendGridClient(ApplicationSettings.Mail.SendGridKey);
                var msg      = MailHelper.CreateSingleEmail(new EmailAddress(from, fromName), new EmailAddress(to, toName), subject, body, body);
                var response = await client.SendEmailAsync(msg);
            }
            catch (Exception ex)
            {
                result.AddException(ex, "Error sending email by SendGrid");
            }

            return(result);
        }
Esempio n. 5
0
        protected virtual async Task <Execute> SaveInternalAsync(List <TEntity> entities, DbContext context)
        {
            var execute = new Execute();

            // Call Before Save
            await BeforeSaveAsync(entities);

            // Validate the enties
            execute.AddMessage(await ValidateManyAsync(entities));

            // Something wrong? STOP!
            if (execute.HasErro)
            {
                return(execute);
            }

            // Attach deleted entities to the DBContext
            foreach (var entity in entities.Where(c => c.Action == EntityAction.Delete))
            {
                try
                {
                    var dataEntity = entity.Convert <TData>();
                    context.Entry(dataEntity).State = EntityState.Deleted;
                }
                catch (Exception ex)
                {
                    execute.AddException(ex, "Error on delete info to: {0}", GetEntityName());
                }
            }

            // Attach new entities to the DBContext
            foreach (var entity in entities.Where(c => c.Action == EntityAction.New))
            {
                try
                {
                    var dataEntity = entity.Convert <TData>();
                    context.Entry(dataEntity).State = EntityState.Added;
                }
                catch (Exception ex)
                {
                    execute.AddException(ex, "Error on insert info to: {0}", GetEntityName());
                }
            }

            // Attach updated entities to the DBContext
            foreach (var entity in entities.Where(c => c.Action == EntityAction.Update))
            {
                try
                {
                    var dataEntity = entity.Convert <TData>();
                    context.Entry(dataEntity).State = EntityState.Modified;
                }
                catch (Exception ex)
                {
                    execute.AddException(ex, "Error on update info to: {0}", GetEntityName());
                }
            }

            // If it's okay so far, try to save it and call after save.
            if (!execute.HasErro)
            {
                try
                {
                    await context.SaveChangesAsync();
                    await AfterSaveAsync(entities);
                }
                catch (Exception ex)
                {
                    execute.AddException(ex, "Error on savechanges in: {0}", GetEntityName());
                }
            }


            return(execute);
        }