Exemplo n.º 1
0
        public async Task SaveChangesAsync(EmailServiceContext ctx)
        {
            var app = await ctx.FindApplicationAsync(ApplicationId);

            foreach (var transport in Transports)
            {
                var existing = await ctx.Transports
                               .Where(t => t.Id == transport.TransportId)
                               .SelectMany(t => t.Applications)
                               .FirstOrDefaultAsync(a => a.ApplicationId == ApplicationId);

                if (existing != null)
                {
                    if (transport.Remove)
                    {
                        app.Transports.Remove(existing);
                        ctx.Remove(existing);
                    }
                    else
                    {
                        existing.Priority = transport.Priority;
                    }
                }
            }

            await ctx.SaveChangesAsync();
        }
        public async Task <Template> CopyAsync(EmailServiceContext ctx)
        {
            Template copy = null;

            var template = await ctx.Templates.Include(t => t.Translations).FirstOrDefaultAsync(t => t.Id == TemplateId);

            if (template != null)
            {
                copy = new Template
                {
                    ApplicationId   = template.ApplicationId,
                    Name            = NewName,
                    Description     = Description,
                    BodyTemplate    = template.BodyTemplate,
                    SubjectTemplate = template.SubjectTemplate,
                    UseHtml         = template.UseHtml,
                    SampleData      = template.SampleData
                };
                ctx.Templates.Add(copy);

                foreach (var translation in template.Translations)
                {
                    var copyTran = new Translation {
                        TemplateId = copy.Id, Language = translation.Language, SubjectTemplate = translation.SubjectTemplate, BodyTemplate = translation.BodyTemplate
                    };
                    ctx.Translations.Add(copyTran);
                    copy.Translations.Add(copyTran);
                }

                await ctx.SaveChangesAsync();
            }

            return(copy);
        }
        public static async Task <TransportDetailsViewModel> LoadAysnc(EmailServiceContext ctx, Guid id)
        {
            var transport = await ctx.Transports
                            .Include(a => a.Applications)
                            .ThenInclude(a => a.Application)
                            .FirstOrDefaultAsync(t => t.Id == id);

            if (transport != null)
            {
                return(new TransportDetailsViewModel
                {
                    Id = transport.Id,
                    Name = transport.Name,
                    Type = transport.Type,
                    IsActive = transport.IsActive,
                    SenderAddress = transport.SenderAddress,
                    SenderName = transport.SenderName,
                    Applications = transport.Applications.Select(a => new TransportApplicationInfo
                    {
                        ApplicationId = a.ApplicationId,
                        ApplicationName = a.Application.Name,
                        IsActive = a.Application.IsActive,
                        Priority = a.Priority
                    })
                });
            }

            return(null);
        }
        public static async Task <ApplicationDetailsViewModel> LoadAsync(EmailServiceContext ctx, ICryptoServices crypto, Guid id)
        {
            var app = await ctx.Applications
                      .Include(a => a.Templates)
                      .Include(a => a.Transports)
                      .ThenInclude(t => t.Transport)
                      .FirstOrDefaultAsync(a => a.Id == id);

            if (app != null)
            {
                return(new ApplicationDetailsViewModel
                {
                    Id = app.Id,
                    Name = app.Name,
                    Description = app.Description,
                    SenderAddress = app.SenderAddress,
                    SenderName = app.SenderName,
                    PrimaryApiKey = crypto.GetApiKey(app.Id, app.PrimaryApiKey),
                    SecondaryApiKey = crypto.GetApiKey(app.Id, app.SecondaryApiKey),
                    CreatedUtc = app.CreatedUtc,
                    IsActive = app.IsActive,
                    Templates = app.Templates.Select(t => new KeyValuePair <Guid, string>(t.Id, t.Name)).ToList(),
                    Transports = app.Transports.Select(t => new KeyValuePair <Guid, string>(t.TransportId, t.Transport.Name)).ToList()
                });
            }

            return(null);
        }
Exemplo n.º 5
0
        public static async Task <TemplateDetailsViewModel> LoadAsync(EmailServiceContext _ctx, Guid id)
        {
            var source = await _ctx.Templates
                         .Include(t => t.Translations)
                         .Include(t => t.Application)
                         .FirstOrDefaultAsync(t => t.Id == id);

            if (source != null)
            {
                return(new TemplateDetailsViewModel
                {
                    Id = source.Id,
                    Name = source.Name,
                    Description = source.Description,
                    ApplicationId = source.ApplicationId,
                    ApplicationName = source.Application.Name,
                    BodyTemplate = source.BodyTemplate,
                    SubjectTemplate = source.SubjectTemplate,
                    IsActive = source.IsActive,
                    Translations = source.Translations.Select(t => new SelectListItem
                    {
                        Value = t.Language,
                        Text = t.GetCultureName()
                    })
                });
            }

            return(null);
        }
Exemplo n.º 6
0
        private EmailServiceContext GetDbContext()
        {
            var ctx = new EmailServiceContext(_contextOptions);

            ctx.Database.EnsureDeleted();
            return(ctx);
        }
Exemplo n.º 7
0
        private static void SetupDependencies(
            out IEmailQueueReceiver <AzureEmailQueueMessage> receiver,
            out IEmailQueueBlobStore blobStore,
            out EmailServiceContext context,
            out IMemoryCache cache,
            out IEmailLogWriter logWriter)
        {
            var storageOptions = Options.Create(new AzureStorageOptions
            {
                ConnectionString = Configuration.GetConnectionString("Storage")
            });

            receiver  = new StorageEmailQueue(storageOptions, LoggerFactory);
            blobStore = new AzureEmailQueueBlobStore(storageOptions, LoggerFactory);
            logWriter = new StorageEmailLog(storageOptions, LoggerFactory);

            var cacheOptions = Options.Create(new MemoryCacheOptions
            {
                CompactOnMemoryPressure = true,
                ExpirationScanFrequency = TimeSpan.FromMinutes(1)
            });

            cache = new MemoryCache(cacheOptions);

            var builder = new DbContextOptionsBuilder <EmailServiceContext>();

            builder.UseSqlServer(Configuration.GetConnectionString("SqlServer"));
            builder.UseMemoryCache(cache);
            context = new EmailServiceContext(builder.Options);
        }
Exemplo n.º 8
0
        public static Task <PrioritiseTransportsViewModel> LoadAsync(EmailServiceContext ctx, Guid id)
        {
            PrioritiseTransportsViewModel model = null;

            var app = ctx.Applications
                      .Include(a => a.Transports)
                      .ThenInclude(a => a.Transport)
                      .FirstOrDefault(a => a.Id == id);

            if (app != null)
            {
                model = new PrioritiseTransportsViewModel
                {
                    ApplicationId   = app.Id,
                    ApplicationName = app.Name
                };

                model.Transports = app.Transports.Select(t => new TransportPriorityViewModel
                {
                    TransportId = t.TransportId,
                    Priority    = t.Priority,
                    Name        = t.Transport.Name
                }).ToList();
            }

            return(Task.FromResult(model));
        }
Exemplo n.º 9
0
        public static async Task <IndexViewModel> LoadAsync(EmailServiceContext ctx, Guid?applicationId, bool showDeactivated)
        {
            var model = new IndexViewModel();
            var list  = ctx.Templates
                        .Include(t => t.Application)
                        .OrderBy(t => t.Name)
                        .AsQueryable();

            if (applicationId.HasValue)
            {
                list = list.Where(t => t.ApplicationId == applicationId.Value);
            }

            if (!showDeactivated)
            {
                list = list.Where(t => t.IsActive);
            }

            await list.ForEachAsync(t => model.Templates.Add(new TemplateIndexViewModel(t)));

            var apps = new List <SelectListItem>();
            await ctx.Applications.ForEachAsync(a => apps.Add(new SelectListItem
            {
                Value = a.Id.ToString(),
                Text  = a.Name
            }));

            model.Applications    = apps;
            model.ApplicationId   = applicationId;
            model.ShowDeactivated = showDeactivated;

            return(model);
        }
Exemplo n.º 10
0
        public EmailSenderTestFixture()
        {
            var builder = new DbContextOptionsBuilder <EmailServiceContext>();

            builder.UseInMemoryDatabase();
            Database = new EmailServiceContext(builder.Options);

            SetupEntities();
        }
Exemplo n.º 11
0
        public static async Task <IndexViewModel> LoadAsync(EmailServiceContext ctx)
        {
            var model = new IndexViewModel();
            await ctx.Transports
            .ToAsyncEnumerable()
            .ForEachAsync(t => model.Transports.Add(new TransportIndexViewModel(t)));

            return(model);
        }
Exemplo n.º 12
0
        public async Task SaveChangesAsync(EmailServiceContext ctx)
        {
            var app = await ctx.FindApplicationAsync(Id);

            if (app != null)
            {
                app.IsActive = IsActive;
                await ctx.SaveChangesAsync();
            }
        }
Exemplo n.º 13
0
        public static async Task <IndexViewModel> LoadAsync(EmailServiceContext _ctx)
        {
            var model = new IndexViewModel();
            await _ctx.Applications.ToAsyncEnumerable().ForEachAsync(a =>
            {
                model.Applications.Add(new ApplicationIndexViewModel(a));
            });

            return(model);
        }
Exemplo n.º 14
0
        public static async Task <IndexViewModel> LoadAsync(EmailServiceContext ctx)
        {
            var model = new IndexViewModel();
            var apps  = await ctx.Applications
                        .Select(a => new KeyValuePair <Guid, string>(a.Id, a.Name))
                        .ToListAsync();

            model.Applications = apps;
            return(model);
        }
Exemplo n.º 15
0
        private void SetupData(EmailServiceContext ctx)
        {
            var app = CreateApplication();

            var smtp = new Transport
            {
                Type          = TransportType.Smtp,
                PortNum       = 25,
                Hostname      = "smtp.example.com",
                Username      = "******",
                Password      = "******",
                SenderAddress = "*****@*****.**",
                SenderName    = "Example.com",
                Name          = "Example SMTP"
            };

            app.Transports.Add(new ApplicationTransport {
                Transport = smtp
            });

            var welcome = new Template
            {
                Name            = "Welcome!",
                Description     = "Sent on signup",
                UseHtml         = true,
                SubjectTemplate = "Welcome {{Name}}",
                BodyTemplate    = "Welcome {{Name}}"
            };

            welcome.Translations.Add(new Translation
            {
                Language        = "fr-FR",
                SubjectTemplate = "Bonjour {{Name}}",
                BodyTemplate    = "Bonjour {{Name}}"
            });

            var inactive = new Template
            {
                Name            = "Inactive",
                IsActive        = false,
                SubjectTemplate = "Not active",
                BodyTemplate    = "Not active"
            };

            app.Templates.Add(welcome);
            app.Templates.Add(inactive);

            ctx.Add(smtp);
            ctx.Add(app);
            ctx.SaveChanges();

            TestApp         = app;
            WelcomeTemplate = welcome;
            TestSmtp        = smtp;
        }
        public async Task <Application> SaveChangesAsync(EmailServiceContext ctx, ICryptoServices crypto)
        {
            var app = CreateDbModel();

            app.PrimaryApiKey   = crypto.GeneratePrivateKey();
            app.SecondaryApiKey = crypto.GeneratePrivateKey();
            ctx.Applications.Add(app);
            await ctx.SaveChangesAsync();

            return(app);
        }
        public async Task SaveChangesAsync(EmailServiceContext ctx)
        {
            var template = await ctx.FindTemplateWithTranslationsAsync(TemplateId);

            var translation = template?.Translations.Find(t => t.Language == Language);

            if (translation != null)
            {
                ctx.Translations.Remove(translation);
                await ctx.SaveChangesAsync();
            }
        }
        public async Task SaveChangesAsync(EmailServiceContext ctx)
        {
            var existing = await ctx.FindApplicationAsync(Id);

            if (existing != null)
            {
                existing.Name          = Name;
                existing.Description   = Description;
                existing.SenderAddress = SenderAddress;
                existing.SenderName    = SenderName;
                await ctx.SaveChangesAsync();
            }
        }
Exemplo n.º 19
0
        public async Task SaveChangesAsync(EmailServiceContext ctx)
        {
            var template = await ctx.Templates.Include(t => t.Translations).FirstOrDefaultAsync(t => t.Id == TemplateId);

            var translation = template?.Translations.FirstOrDefault(t => t.Language == Language);

            if (translation != null)
            {
                translation.SubjectTemplate = SubjectTemplate;
                translation.BodyTemplate    = BodyTemplate;

                await ctx.SaveChangesAsync();
            }
        }
Exemplo n.º 20
0
 public QueueProcessor(
     EmailServiceContext db,
     IEmailQueueReceiver <TMessage> receiver,
     IEmailQueueBlobStore blobStore,
     IEmailTransportFactory transportFactory,
     IEmailLogWriter logWriter,
     ILoggerFactory loggerFactory)
 {
     _db               = db;
     _receiver         = receiver;
     _blobStore        = blobStore;
     _transportFactory = transportFactory;
     _logWriter        = logWriter;
     _logger           = loggerFactory.CreateLogger <QueueProcessor <TMessage> >();
 }
Exemplo n.º 21
0
        public async Task SaveChangesAsync(EmailServiceContext ctx)
        {
            var app = await ctx.FindApplicationAsync(ApplicationId);

            if (app != null)
            {
                app.Transports.Add(new ApplicationTransport
                {
                    TransportId = TransportId.GetValueOrDefault(),
                    Priority    = Priority
                });

                await ctx.SaveChangesAsync();
            }
        }
Exemplo n.º 22
0
        public static async Task <EditTemplateViewModel> LoadAsync(EmailServiceContext ctx, Guid templateId)
        {
            var template = await ctx.Templates.FindAsync(templateId);

            return(new EditTemplateViewModel
            {
                Id = template.Id,
                Name = template.Name,
                Description = template.Description,
                SampleData = template.SampleData,
                BodyTemplate = template.BodyTemplate,
                SubjectTemplate = template.SubjectTemplate,
                UseHtml = template.UseHtml
            });
        }
Exemplo n.º 23
0
        public async Task SaveChangesAsync(EmailServiceContext ctx)
        {
            var template = await ctx.Templates.FindAsync(Id);

            if (template != null)
            {
                template.Name            = Name;
                template.Description     = Description;
                template.SubjectTemplate = SubjectTemplate;
                template.BodyTemplate    = BodyTemplate;
                template.SampleData      = SampleData;
                template.UseHtml         = UseHtml;

                await ctx.SaveChangesAsync();
            }
        }
        public static async Task <EditApplicationViewModel> LoadAsync(EmailServiceContext ctx, Guid id)
        {
            var application = await ctx.FindApplicationAsync(id);

            if (application != null)
            {
                return(new EditApplicationViewModel
                {
                    Id = application.Id,
                    Name = application.Name,
                    Description = application.Description,
                    SenderAddress = application.SenderAddress,
                    SenderName = application.SenderName
                });
            }

            return(null);
        }
        public async Task SaveChangesAsync(EmailServiceContext ctx, ICryptoServices crypto)
        {
            var app = await ctx.FindApplicationAsync(Id);

            if (app != null)
            {
                if (IsPrimary)
                {
                    app.PrimaryApiKey = crypto.GeneratePrivateKey();
                }
                else if (IsSecondary)
                {
                    app.SecondaryApiKey = crypto.GeneratePrivateKey();
                }

                await ctx.SaveChangesAsync();
            }
        }
        public static async Task <RemoveTranslationViewModel> LoadAsync(EmailServiceContext ctx, Guid id, string language)
        {
            var template = await ctx.FindTemplateWithTranslationsAsync(id);

            var translation = template?.Translations.Find(t => t.Language == language);

            if (translation != null)
            {
                return(new RemoveTranslationViewModel
                {
                    TemplateId = template.Id,
                    TemplateName = template.Name,
                    Language = translation.Language,
                    LanguageName = translation.GetCultureName()
                });
            }

            return(null);
        }
        public static async Task <AddTranslationViewModel> LoadAsync(EmailServiceContext ctx, Guid templateId)
        {
            var template = await ctx.Templates.Include(t => t.Translations).FirstOrDefaultAsync(t => t.Id == templateId);

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

            var langs = template.Translations.Select(t => t.Language);

            return(new AddTranslationViewModel(langs)
            {
                TemplateId = template.Id,
                TemplateName = template.Name,
                BodyTemplate = template.BodyTemplate,
                SubjectTemplate = template.SubjectTemplate
            });
        }
Exemplo n.º 28
0
        public static async Task <EditTranslationViewModel> LoadAsync(EmailServiceContext ctx, Guid templateId, string language)
        {
            var template = await ctx.Templates.Include(t => t.Translations).FirstOrDefaultAsync(t => t.Id == templateId);

            var translation = template?.Translations.FirstOrDefault(t => t.Language == language);

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

            var langs = template.Translations.Select(t => t.Language);

            return(new EditTranslationViewModel
            {
                TemplateId = template.Id,
                TemplateName = template.Name,
                Language = language,
                BodyTemplate = translation.BodyTemplate,
                SubjectTemplate = translation.SubjectTemplate
            });
        }
Exemplo n.º 29
0
 public TemplatesController(EmailServiceContext ctx)
 {
     _ctx = ctx;
 }
Exemplo n.º 30
0
 public ApplicationsController(EmailServiceContext ctx)
 {
     _ctx = ctx;
 }