public virtual async Task <Execute <List <TEntity> > > SaveManyAsync(Execute <List <TEntity> > execute, DbContext context) { try { execute.AddMessage(await ValidateManyAsync(execute.Entity)); if (execute.HasErro) { return(execute); } execute.AddMessage(await SaveAsync(execute.Entity, context)); if (!execute.HasErro) { await AfterExecuteAsync(execute.Entity); } } catch (Exception ex) { execute.AddMessage(ex, string.Format("Erro on save: {0}", GetEntityName())); } return(execute); }
public void ConstructorExecuteUserMessagesPlusUser() { var prioExecute = new Execute(SampleUser); prioExecute.AddMessage(ExecuteMessageType.Error, "Error message"); prioExecute.AddMessage(ExecuteMessageType.Warning, "Warning message"); prioExecute.AddMessage(ExecuteMessageType.Exception, "Exception message"); var user = new ExecuteUser { Key = "2".ToGuid(), Culture = "jonhy", Name = "Jonhy" }; var execute = new Execute(prioExecute, user); execute.HasErro.Should().Be(true, "There is error message"); execute.HasWarning.Should().Be(true, "There is warning message"); execute.HasException.Should().Be(true, "There is exception message"); execute.User.Should().NotBe(null, "The user was infomed"); execute.User.Should().Be(user, "The expected user is Jonhy"); execute.Messages.Should().NotBeNull("Messages never can be null"); execute.Messages.Count.Should().Be(3, "There isn't messages"); }
public async Task <bool> IsSatisfiedByAsync(TEntity entity, Execute execute) { var result = true; if (!Condition(entity)) { return(result); } var executeInternal1 = new Execute(); var executeInternal2 = new Execute(); var result1 = await Spec1.IsSatisfiedByAsync(entity, executeInternal1); var result2 = await Spec2.IsSatisfiedByAsync(entity, executeInternal2); result = result1 || result2; if (!result && execute != null) { execute.AddMessage(executeInternal1); execute.AddMessage(executeInternal2); } return(result); }
public bool IsSatisfiedBy(TEntity entity, Execute execute) { var value = Selector(entity); Regex regex = null; try { regex = new Regex(RegexExpression); } catch (Exception ex) { if (execute != null) { execute.AddMessage(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); }
protected virtual async Task <Execute> SaveAsync(List <TEntity> entitys, DbContext context) { var execute = new Execute(); foreach (var entity in entitys.Where(c => c.Action == EntityAction.Delete)) { try { var dataEntity = entity.Convert <TData>(); context.Entry <TData>(dataEntity).State = EntityState.Deleted; } catch (Exception ex) { execute.AddMessage(ex, "Error on delete info to: {0}", GetEntityName()); } } foreach (var entity in entitys.Where(c => c.Action == EntityAction.New)) { try { var dataEntity = entity.Convert <TData>(); context.Entry <TData>(dataEntity).State = EntityState.Added; } catch (Exception ex) { execute.AddMessage(ex, "Error on insert info to: {0}", GetEntityName()); } } foreach (var entity in entitys.Where(c => c.Action == EntityAction.Update)) { try { var dataEntity = entity.Convert <TData>(); context.Entry <TData>(dataEntity).State = EntityState.Modified; } catch (Exception ex) { execute.AddMessage(ex, "Error on update info to: {0}", GetEntityName()); } } if (!execute.HasErro) { try { await context.SaveChangesAsync(); } catch (Exception ex) { execute.AddMessage(ex, "Error on savechanges in: {0}", GetEntityName()); } } return(execute); }
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; })); }
public virtual async Task <Execute> ChangePasswordAsync(PasswordChangeEntity info) { var result = new Execute(); var user = await UsersBusiness.GetByKeyAsync(TicketManager.UserKey); if (user == null) { result.AddError(Resources.Authentication.InvalidUserPassword); return(result); } if (!await VerifyPasswordHashAsync(user, info.PasswordCurrent)) { result.AddError(Resources.Authentication.PasswordNotMatch); return(result); } if (info.PasswordNew != info.PasswordConfirm) { } user.Action = EntityAction.Update; user.PasswordHash = await GetPasswordHashAsync(user, info.PasswordNew); result.AddMessage(await UsersBusiness.SaveAsync(user)); return(result); }
public async Task <Execute <TConfig> > Set <TModule, TConfig>(TModule module, string key, TConfig value) { var result = new Execute <TConfig>(); var moduleName = ParseModuleName(module); var configuration = await GetFirstByFilterAsync(new ConfigsFilter { Module = moduleName, ConfigKey = key }); if (configuration == null) { configuration = new ConfigsEntity { Action = EntityAction.New, IdConfig = Guid.NewGuid(), Module = moduleName, ConfigKey = key, Value = JsonConvert.SerializeObject(value) }; } else { configuration.Action = EntityAction.Update; configuration.Value = JsonConvert.SerializeObject(value); } result.AddMessage(await SaveAsync(configuration)); if (!result.HasErro) { result.Entity = value; } return(result); }
public void AddMessageExecute() { var execute01 = new Execute(); execute01.AddMessage(ExecuteMessageType.Error, "Execute 01 error message"); execute01.AddMessage(ExecuteMessageType.Warning, "Execute 01 warning message"); var execute = new Execute(); execute.AddMessage(execute01); execute.HasErro.Should().Be(true, "There is error message"); execute.HasWarning.Should().Be(true, "There is warning message"); execute.HasException.Should().Be(false, "There isn't exception message"); execute.Messages.Count.Should().Be(2, "There are 3 messages"); }
public void ConstructorExecuteUserMessages() { var prioExecute = new Execute(SampleUser); prioExecute.AddMessage(ExecuteMessageType.Error, "Error message"); prioExecute.AddMessage(ExecuteMessageType.Warning, "Warning message"); prioExecute.AddMessage(ExecuteMessageType.Exception, "Exception message"); var execute = new Execute(prioExecute); execute.HasErro.Should().Be(true, "There is error message"); execute.HasWarning.Should().Be(true, "There is warning message"); execute.HasException.Should().Be(true, "There is exception message"); execute.User.Should().NotBe(null, "The user was infomed"); execute.Messages.Should().NotBeNull("Messages never can be null"); execute.Messages.Count.Should().Be(3, "There are 3 messages"); }
public void AddMessageMessageNullArgsNull() { var execute = new Execute(); execute.AddMessage(ExecuteMessageType.Error, null, null); execute.HasErro.Should().Be(true, "There is error message"); execute.HasWarning.Should().Be(false, "There isn't warning message"); execute.HasException.Should().Be(false, "There isn't exception message"); execute.Messages.Count.Should().Be(1, "There isn 1 message"); }
public virtual async Task <Execute <List <TEntity> > > SaveManyAsync(List <TEntity> entities, DbContext context) { var result = new Execute <List <TEntity> > { Entity = entities }; var saveResult = await SaveInternalAsync(result.Entity, context); result.AddMessage(saveResult); return(result); }
public async Task <Execute <TicketEntity> > SignUpAsync(SignUpEntity signUp) { var result = new Execute <TicketEntity>(); if (!await ValidateSignUp.IsSatisfiedByAsync(signUp, result)) { return(result); } var(person, user, provider) = await CastNewUser(signUp); using (var db = new Context.XCommonDbContext()) { using (var transaction = await db.Database.BeginTransactionAsync()) { result.AddMessage(await PeopleBusiness.SaveAsync(person, db)); result.AddMessage(await UsersBusiness.SaveAsync(user, db)); result.AddMessage(await UsersProvidersBusiness.SaveAsync(provider, db)); if (!result.HasErro) { transaction.Commit(); } } } if (!result.HasErro) { result.Entity = new TicketEntity { Culture = person.Culture, Key = person.IdPerson, Name = person.Name, Roles = await GetUserRolesAsync(user) }; } return(result); }
public async Task <Execute> SendAsync(string from, string fromName, string to, string toName, string subject, string body) { var result = new Execute(); if (ApplicationSettings.ExternalKeys == null || ApplicationSettings.ExternalKeys.MailGunKey.IsEmpty() || ApplicationSettings.ExternalKeys.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.ExternalKeys.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.ExternalKeys.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.AddMessage(ex, "Error sending email by MailGun"); } return(await Task.FromResult(result)); }
public void AddMessageTypeMessage() { var execute = new Execute(); execute.AddMessage(ExecuteMessageType.Warning, "Errors: {0} - {1}"); execute.HasErro.Should().Be(false, "There isn't error message"); execute.HasWarning.Should().Be(true, "There is warning message"); execute.HasException.Should().Be(false, "There isn't exception message"); execute.Messages.Count.Should().Be(1, "There isn 1 message"); execute.Messages[0].Message.Should().Be("Errors: {0} - {1}", "Can't format the message"); }
public void AddMessageTypeMessageArgs() { var execute = new Execute(); execute.AddMessage(ExecuteMessageType.Error, "Errors: {0} - {1}", "One", "Two"); execute.HasErro.Should().Be(true, "There is error message"); execute.HasWarning.Should().Be(false, "There isn't warning message"); execute.HasException.Should().Be(false, "There isn't exception message"); execute.Messages.Count.Should().Be(1, "There is 1 message"); execute.Messages[0].Message.Should().Be("Errors: One - Two", "Needs to format the message"); }
public async Task <Execute> SendAsync(string from, string fromName, string to, string toName, string subject, string body) { var result = new Execute(); if (ApplicationSettings.ExternalKeys == null || ApplicationSettings.ExternalKeys.SendGridKey.IsEmpty()) { result.AddMessage(ExecuteMessageType.Error, "SendGrid API Key is not defined on ApplicationSettings."); return(result); } try { var client = new SendGridClient(ApplicationSettings.ExternalKeys.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.AddMessage(ex, "Error sending email by SendGrid"); } return(result); }
public virtual async Task <Execute <TEntity> > SaveAsync(Execute <TEntity> execute, DbContext context) { var executeList = new Execute <List <TEntity> >(execute) { Entity = new List <TEntity> { execute.Entity } }; executeList = await SaveManyAsync(executeList, context); execute.Entity = executeList.Entity.FirstOrDefault(); execute.AddMessage(executeList); return(execute); }
public async Task <bool> IsSatisfiedByAsync(TEntity entity, Execute execute) { return(await Task.Factory.StartNew(() => { var result = true; if (!Condition(entity)) { return result; } var value = Selector(entity); switch (Type) { case AndIsNotEmptyType.String: result = (value as string).IsNotEmpty(); break; case AndIsNotEmptyType.Int: result = (value as int?).HasValue; break; case AndIsNotEmptyType.Decimal: result = (value as decimal?).HasValue; break; case AndIsNotEmptyType.Date: result = (value as DateTime?).HasValue; break; case AndIsNotEmptyType.Object: default: result = value != null; break; } if (!result && execute != null) { execute.AddMessage(ExecuteMessageType.Error, Message ?? "There is a empty property", MessageArgs ?? new object[] { }); } return result; })); }
public void AddMessageExceptionMessageArgs() { var execute = new Execute(); execute.AddMessage(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"); }
public bool IsSatisfiedBy(TEntity entity, Execute execute) { var result = true; if (!Condition(entity)) { return(result); } result = Selector(entity); if (!result && execute != null) { execute.AddMessage(ExecuteMessageType.Error, Message ?? "There is a invalid property", MessageArgs ?? new object[] { }); } return(result); }
public async Task <bool> IsSatisfiedByAsync(TEntity entity, Execute execute) { var result = true; var condition = await Task.Factory.StartNew(() => Condition(entity)); if (!condition) { return(result); } result = await Task.Factory.StartNew(() => Selector(entity)); if (!result && execute != null) { execute.AddMessage(ExecuteMessageType.Error, Message ?? "There is a invalid property", MessageArgs ?? new object[] { }); } return(result); }
public void ConstructorExecuteUserNullMessagesPlusUser() { Execute prioExecute = new Execute(SampleUser); prioExecute.AddMessage(ExecuteMessageType.Error, "Error message"); prioExecute.AddMessage(ExecuteMessageType.Warning, "Warning message"); prioExecute.AddMessage(ExecuteMessageType.Exception, "Exception message"); Execute execute = new Execute(prioExecute); execute.HasErro.Should().Be(true, "There is error message"); execute.HasWarning.Should().Be(true, "There is warning message"); execute.HasException.Should().Be(true, "There is exception message"); execute.User.Should().NotBe(null, "The user was infomed"); execute.User.Should().Be(SampleUser, "The expected user is Jonhy"); execute.Messages.Should().NotBeNull("Messages never can be null"); execute.Messages.Count.Should().Be(3, "There isn't messages"); }
public void AddMessageTypeMessageArgs() { Execute execute = new Execute(); execute.AddMessage(ExecuteMessageType.Error, "Errors: {0} - {1}", "One", "Two"); execute.HasErro.Should().Be(true, "There is error message"); execute.HasWarning.Should().Be(false, "There isn't warning message"); execute.HasException.Should().Be(false, "There isn't exception message"); execute.Messages.Count.Should().Be(1, "There is 1 message"); execute.Messages[0].Message.Should().Be("Errors: One - Two", "Needs to format the message"); }
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); }
public void AddMessageTypeMessage() { Execute execute = new Execute(); execute.AddMessage(ExecuteMessageType.Warning, "Errors: {0} - {1}"); execute.HasErro.Should().Be(false, "There isn't error message"); execute.HasWarning.Should().Be(true, "There is warning message"); execute.HasException.Should().Be(false, "There isn't exception message"); execute.Messages.Count.Should().Be(1, "There isn 1 message"); execute.Messages[0].Message.Should().Be("Errors: {0} - {1}", "Can't format the message"); }
public void AddMessageExceptionMessageArgs() { Execute execute = new Execute(); execute.AddMessage(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"); }
public void AddMessageExecute() { Execute execute01 = new Execute(); execute01.AddMessage(ExecuteMessageType.Error, "Execute 01 error message"); execute01.AddMessage(ExecuteMessageType.Warning, "Execute 01 warning message"); Execute execute = new Execute(); execute.AddMessage(execute01); execute.HasErro.Should().Be(true, "There is error message"); execute.HasWarning.Should().Be(true, "There is warning message"); execute.HasException.Should().Be(false, "There isn't exception message"); execute.Messages.Count.Should().Be(2, "There are 3 messages"); }
public void AddMessageMessageNullArgsNull() { Execute execute = new Execute(); execute.AddMessage(ExecuteMessageType.Error, null, null); execute.HasErro.Should().Be(true, "There is error message"); execute.HasWarning.Should().Be(false, "There isn't warning message"); execute.HasException.Should().Be(false, "There isn't exception message"); execute.Messages.Count.Should().Be(1, "There isn 1 message"); }
public virtual async Task <Execute <TicketEntity> > SignInAsync(SignInEntity signIn) { var result = new Execute <TicketEntity>(); if (!await ValidateSignIn.IsSatisfiedByAsync(signIn, result)) { return(result); } var person = await PeopleBusiness.GetFirstByFilterAsync(new PeopleFilter { Email = signIn.User }); if (person == null) { result.AddMessage(ExecuteMessageType.Error, Resources.Authentication.InvalidUserPassword); return(result); } var user = await UsersBusiness.GetByKeyAsync(person.IdPerson); if (user == null) { result.AddMessage(ExecuteMessageType.Error, Resources.Authentication.InvalidUserPassword); return(result); } if (signIn.Provider == ProviderType.Local) { if (!await VerifyPasswordHashAsync(user, signIn.Password)) { result.AddMessage(ExecuteMessageType.Error, Resources.Authentication.InvalidUserPassword); return(result); } result.Entity = new TicketEntity { Culture = person.Culture, Key = person.IdPerson, Name = person.Name, Roles = await GetUserRolesAsync(user) }; await SetTicketCustomValuesAsync(result.Entity); return(result); } var provider = await UsersProvidersBusiness.GetFirstByFilterAsync(new UsersProvidersFilter { IdUser = person.IdPerson, Provider = signIn.Provider }); if (provider == null) { result.AddMessage(ExecuteMessageType.Error, Resources.Authentication.InvalidUserPassword); return(result); } result.Entity = new TicketEntity { Culture = person.Culture, Key = person.IdPerson, Name = person.Name, Roles = await GetUserRolesAsync(user) }; await SetTicketCustomValuesAsync(result.Entity); return(result); }