Пример #1
0
        public async Task <Sender> CreateOrUpdateSenderAsync(Sender sender)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entity = sender.SenderAddrID != null ? await ctx.SenderAddresses.SingleOrDefaultAsync(
                    s => s.EngagementAccount == sender.EngagementAccount &&
                    s.Id == new Guid(sender.SenderAddrID)) : null;

                if (entity == null)
                {
                    entity                   = new SenderAddressEntity();
                    entity.Id                = Guid.NewGuid();
                    entity.Name              = sender.SenderEmailAddress.Address;
                    entity.Domain            = sender.SenderEmailAddress.Host;
                    entity.EngagementAccount = sender.EngagementAccount;
                    entity.ForwardAddress    = sender.ForwardEmailAddress.Address;
                    entity.Properties        = JsonConvert.SerializeObject(sender.Properties);
                    entity.Created           = entity.Modified = DateTime.UtcNow;

                    ctx.SenderAddresses.Add(entity);
                }
                else
                {
                    entity.Name           = sender.SenderEmailAddress.Address;
                    entity.Domain         = sender.SenderEmailAddress.Host;
                    entity.ForwardAddress = sender.ForwardEmailAddress.Address;
                    entity.Properties     = JsonConvert.SerializeObject(sender.Properties);
                    entity.Modified       = DateTime.UtcNow;
                }

                await ctx.SaveChangesAsync();

                return(entity.ToModel());
            }
        }
Пример #2
0
        public async Task CreateOrUpdateCredentialAsync(EmailConnectorCredential credential)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.ConnectorCredentials.SingleOrDefaultAsync(
                    c => c.Provider == credential.ConnectorName &&
                    c.Id == credential.ConnectorId);

                if (entity == null)
                {
                    entity          = new ConnectorCredentialEntity();
                    entity.Provider = credential.ConnectorName;
                    entity.Id       = credential.ConnectorId;
                    entity.Data     = JsonConvert.SerializeObject(credential);
                    entity.Created  = entity.Modified = DateTime.UtcNow;
                    entity.Enabled  = true;

                    ctx.ConnectorCredentials.Add(entity);
                }
                else
                {
                    entity.Data     = JsonConvert.SerializeObject(credential);
                    entity.Modified = DateTime.UtcNow;
                    entity.Enabled  = true;
                }

                await ctx.SaveChangesAsync();
            }
        }
Пример #3
0
        public async Task <Group> CreateOrUpdateGroupAsync(Group group)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.Groups.SingleOrDefaultAsync(
                    s => s.EngagementAccount == group.EngagementAccount &&
                    s.Name == group.Name);

                if (entity == null)
                {
                    entity      = new GroupEntity();
                    entity.Name = group.Name;
                    entity.EngagementAccount = group.EngagementAccount;
                    entity.Description       = group.Description;
                    entity.Properties        = JsonConvert.SerializeObject(group.Properties);
                    entity.Created           = entity.Modified = DateTime.UtcNow;

                    ctx.Groups.Add(entity);
                }
                else
                {
                    entity.Description = group.Description;
                    entity.Properties  = JsonConvert.SerializeObject(group.Properties);
                    entity.Modified    = DateTime.UtcNow;
                }

                await ctx.SaveChangesAsync();

                return(entity.ToModel());
            }
        }
Пример #4
0
        public async Task <Domain> CreateOrUpdateDomainAsync(Domain domain)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.Domains.SingleOrDefaultAsync(
                    s => s.EngagementAccount == domain.EngagementAccount &&
                    s.Domain == domain.Value);

                if (entity == null)
                {
                    entity                   = new DomainEntity();
                    entity.Domain            = domain.Value;
                    entity.EngagementAccount = domain.EngagementAccount;
                    entity.State             = domain.State.ToString();
                    entity.Message           = domain.Message;
                    entity.Created           = entity.Modified = DateTime.UtcNow;

                    ctx.Domains.Add(entity);
                }
                else
                {
                    entity.State    = domain.State.ToString();
                    entity.Message  = domain.Message;
                    entity.Modified = DateTime.UtcNow;
                }

                await ctx.SaveChangesAsync();

                return(entity.ToModel());
            }
        }
Пример #5
0
        public async Task <Account> CreateOrUpdateAccountAsync(Account account)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.EngagementAccounts.SingleOrDefaultAsync(a => a.EngagementAccount == account.EngagementAccount);

                if (entity == null)
                {
                    entity = new EngagementAccountEntity();
                    entity.EngagementAccount = account.EngagementAccount;
                    entity.Properties        = JsonConvert.SerializeObject(account.Properties);
                    entity.Created           = entity.Modified = DateTime.UtcNow;
                    entity.SubscriptionId    = account.SubscriptionId;

                    ctx.EngagementAccounts.Add(entity);
                }
                else
                {
                    entity.Properties     = JsonConvert.SerializeObject(account.Properties);
                    entity.SubscriptionId = account.SubscriptionId;
                    entity.Modified       = DateTime.UtcNow;
                }

                await ctx.SaveChangesAsync();

                return(entity.ToModel());
            }
        }
Пример #6
0
        public async Task DeleteAccountAsync(string engagementAccount)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.EngagementAccounts.SingleOrDefaultAsync(a => a.EngagementAccount == engagementAccount);

                if (entity != null)
                {
                    ctx.EngagementAccounts.Remove(entity);
                    await ctx.SaveChangesAsync();
                }
            }
        }
Пример #7
0
        public async Task DeleteSendersAsync(string engagementAccount)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entities = await ctx.SenderAddresses.Where(s => s.EngagementAccount == engagementAccount).ToListAsync();

                if (entities != null && entities.Count > 0)
                {
                    ctx.SenderAddresses.RemoveRange(entities);
                    await ctx.SaveChangesAsync();
                }
            }
        }
Пример #8
0
        public async Task DeleteCredentialAssignmentsAsync(string engagementAccount, ConnectorIdentifier identifier)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entities = await ctx.ConnectorCredentialAssignments.Where(c => c.EngagementAccount == engagementAccount).ToListAsync();

                entities = entities.Where(e => identifier == null || (e.Provider.Equals(identifier.ConnectorName, StringComparison.OrdinalIgnoreCase) && e.Id.Equals(identifier.ConnectorId, StringComparison.OrdinalIgnoreCase))).ToList();

                if (entities != null)
                {
                    ctx.ConnectorCredentialAssignments.RemoveRange(entities);
                    await ctx.SaveChangesAsync();
                }
            }
        }
Пример #9
0
        public async Task DeleteGroupAsync(string engagementAccount, string group)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.Groups.SingleOrDefaultAsync(
                    s => s.EngagementAccount == engagementAccount &&
                    s.Name == group);

                if (entity != null)
                {
                    ctx.Groups.Remove(entity);
                    await ctx.SaveChangesAsync();
                }
            }
        }
Пример #10
0
        public async Task DeleteSenderAsync(string engagementAccount, Guid senderId)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.SenderAddresses.SingleOrDefaultAsync(
                    s => s.EngagementAccount == engagementAccount &&
                    s.Id == senderId);

                if (entity != null)
                {
                    ctx.SenderAddresses.Remove(entity);
                    await ctx.SaveChangesAsync();
                }
            }
        }
Пример #11
0
        public async Task DeleteConnectorCredentialAsync(ConnectorIdentifier identifier)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.ConnectorCredentials.SingleOrDefaultAsync(
                    c => c.Provider == identifier.ConnectorName &&
                    c.Id == identifier.ConnectorId);

                if (entity != null)
                {
                    ctx.ConnectorCredentials.Remove(entity);
                    await ctx.SaveChangesAsync();
                }
            }
        }
Пример #12
0
        public async Task DeleteTemplatesBySenderAsync(string engagementAccount, Guid senderId)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entities = await ctx.Templates.Where(
                    t => t.EngagementAccount == engagementAccount &&
                    t.SenderAddressId == senderId).ToListAsync();

                if (entities != null && entities.Count > 0)
                {
                    ctx.Templates.RemoveRange(entities);
                    await ctx.SaveChangesAsync();
                }
            }
        }
Пример #13
0
        public async Task DeleteTemplatesByDomainAsync(string engagementAccount, string domain)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entities = await ctx.Templates.Where(
                    t => t.EngagementAccount == engagementAccount &&
                    t.Domain == domain).ToListAsync();

                if (entities != null && entities.Count > 0)
                {
                    ctx.Templates.RemoveRange(entities);
                    await ctx.SaveChangesAsync();
                }
            }
        }
Пример #14
0
        public async Task DeleteTemplateAsync(string engagementAccount, string template)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.Templates.SingleOrDefaultAsync(
                    t => t.EngagementAccount == engagementAccount &&
                    t.Name == template);

                if (entity != null)
                {
                    ctx.Templates.Remove(entity);
                    await ctx.SaveChangesAsync();
                }
            }
        }
Пример #15
0
        public async Task <Template> CreateOrUpdateTemplateAsync(Template template, Sender sender)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.Templates.SingleOrDefaultAsync(
                    t => t.EngagementAccount == template.EngagementAccount &&
                    t.Name == template.Name);

                if (entity == null)
                {
                    entity      = new TemplateEntity();
                    entity.Name = template.Name;
                    entity.EngagementAccount = template.EngagementAccount;
                    entity.Domain            = sender.Domain;
                    entity.SenderAddressId   = template.SenderId;
                    entity.SenderAlias       = template.SenderAlias;
                    entity.ReplyAddress      = string.Empty;
                    entity.Subject           = template.Subject;
                    entity.MessageBody       = template.HtmlMsg;
                    entity.EnableUnSubscribe = template.EnableUnSubscribe;
                    entity.State             = template.State.ToString();
                    entity.StateMessage      = template.StateMessage;
                    entity.Created           = entity.Modified = DateTime.UtcNow;

                    ctx.Templates.Add(entity);
                }
                else
                {
                    entity.Domain            = sender.Domain;
                    entity.SenderAddressId   = template.SenderId;
                    entity.SenderAlias       = template.SenderAlias;
                    entity.ReplyAddress      = string.Empty;
                    entity.Subject           = template.Subject;
                    entity.MessageBody       = template.HtmlMsg;
                    entity.EnableUnSubscribe = template.EnableUnSubscribe;
                    entity.State             = template.State.ToString();
                    entity.StateMessage      = template.StateMessage;
                    entity.Modified          = DateTime.UtcNow;
                }

                await ctx.SaveChangesAsync();

                return(entity.ToModel());
            }
        }
Пример #16
0
        public async Task <EmailAccount> UpdateEmailAccountAsync(EmailAccount emailAccount)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var account = await ctx.EngagementAccounts.SingleOrDefaultAsync(a => a.EngagementAccount == emailAccount.EngagementAccount);

                if (account == null)
                {
                    return(null);
                }

                account.Properties = JsonConvert.SerializeObject(emailAccount.Properties);
                account.Modified   = DateTime.UtcNow;
                await ctx.SaveChangesAsync();

                return(emailAccount);
            }
        }
Пример #17
0
        public async Task CreateOrUpdateCredentialAssignmentAsync(CredentialAssignment credentialAssignment)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entities = await ctx.ConnectorCredentialAssignments.Where(
                    c => c.EngagementAccount == credentialAssignment.EngagementAccount).ToListAsync();

                var entity = entities?.SingleOrDefault(
                    e => e.Provider == credentialAssignment.Provider &&
                    e.Id == credentialAssignment.ConnectorId);

                if (entity != null)
                {
                    entity.Enabled  = credentialAssignment.Enabled;
                    entity.Active   = credentialAssignment.Active;
                    entity.Modified = DateTime.UtcNow;
                }
                else
                {
                    entity = new ConnectorCredentialAssignmentEntity();
                    entity.EngagementAccount = credentialAssignment.EngagementAccount;
                    entity.Provider          = credentialAssignment.Provider;
                    entity.Id      = credentialAssignment.ConnectorId;
                    entity.Enabled = credentialAssignment.Enabled;
                    entity.Active  = credentialAssignment.Active;
                    entity.Created = entity.Modified = DateTime.UtcNow;

                    ctx.ConnectorCredentialAssignments.Add(entity);
                }

                // Make sure at most 1 active credential
                if (credentialAssignment.Active)
                {
                    foreach (var entry in entities.Where(e => e != entity))
                    {
                        entry.Active = false;
                    }
                }

                await ctx.SaveChangesAsync();
            }
        }
Пример #18
0
        public async Task UpdateTemplateStateByDomainAsync(string account, string domain, ResourceState fromState, ResourceState toState, string message = null)
        {
            using (var ctx = new EmailServiceDbEntities(this.connectionString))
            {
                var entities = await ctx.Templates.Where(
                    t => t.EngagementAccount == account &&
                    t.Domain == domain &&
                    t.State == fromState.ToString()).ToListAsync();

                if (entities != null && entities.Count > 0)
                {
                    foreach (var entity in entities)
                    {
                        entity.State        = toState.ToString();
                        entity.StateMessage = message;
                    }

                    await ctx.SaveChangesAsync();
                }
            }
        }