예제 #1
0
        public async Task CreateOrUpdateCredentialAsync(SmsConnectorCredential credential)
        {
            using (var ctx = new SmsServiceDbEntities(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.ChannelType = credential.ChannelType.ToString();
                    entity.Data        = JsonConvert.SerializeObject(credential);
                    entity.Created     = entity.Modified = DateTime.UtcNow;
                    entity.Enabled     = true;

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

                await ctx.SaveChangesAsync();
            }
        }
예제 #2
0
        public async Task <Signature> CreateOrUpdateSignatureAsync(Signature signature)
        {
            using (var ctx = new SmsServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.Signatures.SingleOrDefaultAsync(
                    s => s.EngagementAccount == signature.EngagementAccount &&
                    s.Signature == signature.Value);

                if (entity == null)
                {
                    entity                   = new SignatureEntity();
                    entity.Signature         = signature.Value;
                    entity.ChannelType       = signature.ChannelType.ToString();
                    entity.EngagementAccount = signature.EngagementAccount;
                    entity.ExtendedCode      = signature.ExtendedCode;
                    entity.State             = signature.State.ToString();
                    entity.Message           = signature.Message;
                    entity.Created           = entity.Modified = DateTime.UtcNow;

                    ctx.Signatures.Add(entity);
                }
                else
                {
                    entity.ChannelType  = signature.ChannelType.ToString();
                    entity.ExtendedCode = signature.ExtendedCode;
                    entity.State        = signature.State.ToString();
                    entity.Message      = signature.Message;
                    entity.Modified     = DateTime.UtcNow;
                }

                await ctx.SaveChangesAsync();

                return(entity.ToModel());
            }
        }
예제 #3
0
        public async Task CreateOrUpdateAgentMetadataAsync(AgentMetadata metadata)
        {
            using (var ctx = new SmsServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.ConnectorAgentMetadata.SingleOrDefaultAsync(
                    a => a.Provider == metadata.ConnectorName &&
                    a.Id == metadata.ConnectorId);

                if (entity == null)
                {
                    entity                      = new ConnectorAgentMetadataEntity();
                    entity.Provider             = metadata.ConnectorName;
                    entity.Id                   = metadata.ConnectorId;
                    entity.LastMessageSendTime  = metadata.LastMessageSendTime;
                    entity.LastReportUpdateTime = metadata.LastReportUpdateTime;
                    entity.PendingReceive       = metadata.PendingReceive;
                    entity.Modified             = DateTime.UtcNow;

                    ctx.ConnectorAgentMetadata.Add(entity);
                }
                else
                {
                    entity.LastMessageSendTime  = metadata.LastMessageSendTime;
                    entity.LastReportUpdateTime = metadata.LastReportUpdateTime;
                    entity.PendingReceive       = metadata.PendingReceive;
                    entity.Modified             = DateTime.UtcNow;
                }

                await ctx.SaveChangesAsync();
            }
        }
예제 #4
0
        public async Task <Account> CreateOrUpdateAccountAsync(Account account)
        {
            using (var ctx = new SmsServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.EngagementAccounts.SingleOrDefaultAsync(a => a.EngagementAccount == account.EngagementAccount);

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

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

                await ctx.SaveChangesAsync();

                return(entity.ToModel());
            }
        }
예제 #5
0
        public async Task DeleteTemplatesAsync(string engagementAccount)
        {
            using (var ctx = new SmsServiceDbEntities(this.connectionString))
            {
                var entities = await ctx.Templates.Where(t => t.EngagementAccount == engagementAccount).ToListAsync();

                if (entities != null && entities.Count > 0)
                {
                    ctx.Templates.RemoveRange(entities);
                    await ctx.SaveChangesAsync();
                }
            }
        }
예제 #6
0
        public async Task DeleteAccountAsync(string engagementAccount)
        {
            using (var ctx = new SmsServiceDbEntities(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 DeleteConnectorCredentialAsync(ConnectorIdentifier identifier)
        {
            using (var ctx = new SmsServiceDbEntities(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();
                }
            }
        }
예제 #8
0
        public async Task DeleteTemplateAsync(string engagementAccount, string template)
        {
            using (var ctx = new SmsServiceDbEntities(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();
                }
            }
        }
예제 #9
0
        public async Task DeleteSignatureAsync(string engagementAccount, string signature)
        {
            using (var ctx = new SmsServiceDbEntities(this.connectionString))
            {
                var entity = await ctx.Signatures.SingleOrDefaultAsync(
                    s => s.EngagementAccount == engagementAccount &&
                    s.Signature == signature);

                if (entity != null)
                {
                    ctx.Signatures.Remove(entity);
                    await ctx.SaveChangesAsync();
                }
            }
        }
예제 #10
0
        public async Task DeleteCredentialAssignmentsAsync(string engagementAccount, ConnectorIdentifier identifier)
        {
            using (var ctx = new SmsServiceDbEntities(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();
                }
            }
        }
예제 #11
0
        public async Task CreateOrUpdateCredentialAssignmentAsync(ConnectorCredentialAssignment credentialAssignment)
        {
            using (var ctx = new SmsServiceDbEntities(this.connectionString))
            {
                var entities = await ctx.ConnectorCredentialAssignments.Where(
                    c => c.EngagementAccount == credentialAssignment.EngagementAccount &&
                    c.ChannelType == credentialAssignment.ChannelType.ToString()).ToListAsync();

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

                if (entity != null)
                {
                    entity.Enabled      = credentialAssignment.Enabled;
                    entity.Active       = credentialAssignment.Active;
                    entity.ExtendedCode = credentialAssignment.ExtendedCode;
                    entity.Modified     = DateTime.UtcNow;
                }
                else
                {
                    entity = new ConnectorCredentialAssignmentEntity();
                    entity.EngagementAccount = credentialAssignment.EngagementAccount;
                    entity.ChannelType       = credentialAssignment.ChannelType.ToString();
                    entity.Provider          = credentialAssignment.ConnectorIdentifier.ConnectorName;
                    entity.Id           = credentialAssignment.ConnectorIdentifier.ConnectorId;
                    entity.Enabled      = credentialAssignment.Enabled;
                    entity.Active       = credentialAssignment.Active;
                    entity.ExtendedCode = credentialAssignment.ExtendedCode;
                    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();
            }
        }
예제 #12
0
        public async Task UpdateTemplateStateBySignatureAsync(string engagementAccount, string signature, ResourceState fromState, ResourceState toState, string message = null)
        {
            using (var ctx = new SmsServiceDbEntities(this.connectionString))
            {
                var entities = await ctx.Templates.Where(
                    t => t.EngagementAccount == engagementAccount &&
                    t.Signature == signature &&
                    t.State == fromState.ToString()).ToListAsync();

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

                    await ctx.SaveChangesAsync();
                }
            }
        }
예제 #13
0
        public async Task <Template> CreateOrUpdateTemplateAsync(Template template)
        {
            using (var ctx = new SmsServiceDbEntities(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.Signature         = template.Signature;
                    entity.Category          = template.Category.ToString();
                    entity.Body    = template.Body;
                    entity.State   = template.State.ToString();
                    entity.Message = template.Message;
                    entity.Created = entity.Modified = DateTime.UtcNow;

                    ctx.Templates.Add(entity);
                }
                else
                {
                    entity.Signature = template.Signature;
                    entity.Category  = template.Category.ToString();
                    entity.Body      = template.Body;
                    entity.State     = template.State.ToString();
                    entity.Message   = template.Message;
                    entity.Modified  = DateTime.UtcNow;
                }

                await ctx.SaveChangesAsync();

                return(entity.ToModel());
            }
        }