示例#1
0
        protected async static Task UpdateSubscriberAsync(Subscriber subscriber, IGovDeliveryApiService service, IGovDeliveryContext ctx)
        {
            var subscriberInfoTask       = service.ReadSubscriberAsync(subscriber.Email);
            var subscriberTopicsTask     = service.ListSubscriberTopicsAsync(subscriber.Email);
            var subscriberCategoriesTask = service.ListSubscriberCategoriesAsync(subscriber.Email);

            await Task.WhenAll(new List <Task> {
                subscriberInfoTask, subscriberTopicsTask, subscriberCategoriesTask
            });

            var subscriberInfo = subscriberInfoTask.Result.Data;

            // Update detailed subscriber info
            subscriber.BulletinFrequency = (BulletinFrequency)subscriberInfo.DigestFor.Value;
            subscriber.GovDeliveryId     = subscriberInfo.Id.Value;
            subscriber.Phone             = subscriberInfo.Phone;
            subscriber.SendSubscriberUpdateNotifications = subscriberInfo.SendSubscriberUpdateNotifications.Value;

            // Update Category Subscriptions
            var subscriberCategories = subscriber
                                       .CategorySubscriptions.Select(esc => esc.Category)
                                       .ToList();

            var subscriberCategoryInfo = subscriberCategoriesTask.Result.Data.Items;

            // existing category subscriptions - do nothing.

            var newCategorySubscriptions = subscriberCategoryInfo
                                           .Where(sci => !subscriberCategories.Any(sc => sc.Code == sci.CategoryCode));

            foreach (var nCS in newCategorySubscriptions)
            {
                var cat = ctx.Categories.First(c => c.Code == nCS.CategoryCode);
                ctx.Add(new CategorySubscription
                {
                    CategoryId   = cat.Id,
                    Category     = cat,
                    SubscriberId = subscriber.Id,
                    Subscriber   = subscriber
                });
            }

            var deleteableCategorySubscriptions = subscriberCategories
                                                  .Where(sc => !subscriberCategoryInfo.Any(sci => sci.CategoryCode == sc.Code))
                                                  .ToList();

            foreach (var dSC in deleteableCategorySubscriptions)
            {
                var categorySub = ctx.CategorySubscriptions.First(cs => cs.Category.Code == dSC.Code);
                ctx.Remove(categorySub);
            }

            ctx.SaveChanges();

            // New Topic subscriptions
            var subscriberTopics = subscriber
                                   .TopicSubscriptions.Select(est => est.Topic)
                                   .ToList();

            var subscriberTopicInfo = subscriberTopicsTask.Result.Data.Items;

            var newTopicSubscriptions = subscriberTopicInfo
                                        .Where(sti => !subscriberTopics.Any(st => st.Code == sti.TopicCode))
                                        .ToList();

            foreach (var nTS in newTopicSubscriptions)
            {
                var topic = ctx.Topics.First(t => t.Code == nTS.TopicCode);
                ctx.Add(new TopicSubscription
                {
                    Subscriber   = subscriber,
                    SubscriberId = subscriber.Id,
                    Topic        = topic,
                    TopicId      = topic.Id,
                });
            }

            // deleteable topic subscriptions
            var deleteableTopicSubscriptions = subscriberTopics
                                               .Where(st => !subscriberTopicInfo.Any(sti => sti.TopicCode == st.Code))
                                               .ToList();

            foreach (var dts in deleteableTopicSubscriptions)
            {
                var topicSub = ctx.TopicSubscriptions.First(ts => ts.TopicId == dts.Id);
                ctx.Remove(topicSub);
            }

            ctx.SaveChanges();
        }
        protected async static Task UpdateSingleSubscriberAsync <T>(
            Guid subscriberId,
            IGovDeliveryApiService service,
            IGovDeliveryContextFactory <T> factory,
            Action <string> loggingDelegte
            )
            where T : AbstractGovDeliveryContext
        {
            var ctx        = factory.CreateDbContext();
            var subscriber = ctx.Subscribers.First(s => s.Id == subscriberId);

            if (subscriber == null || service == null || ctx == null)
            {
                return;
            }

            var subscriberInfoTask       = service.ReadSubscriberAsync(subscriber.Email);
            var subscriberTopicsTask     = service.ListSubscriberTopicsAsync(subscriber.Email);
            var subscriberCategoriesTask = service.ListSubscriberCategoriesAsync(subscriber.Email);

            await Task.WhenAll(subscriberInfoTask, subscriberTopicsTask, subscriberCategoriesTask);

            var subscriberInfo = (await subscriberInfoTask).Data;

            // Update detailed subscriber info
            subscriber.BulletinFrequency = (BulletinFrequency)subscriberInfo.DigestFor.Value;
            subscriber.GovDeliveryId     = subscriberInfo.Id.Value;
            subscriber.Phone             = subscriberInfo.Phone;

            if (subscriberInfo.SendSubscriberUpdateNotifications != null)
            {
                subscriber.SendSubscriberUpdateNotifications = subscriberInfo.SendSubscriberUpdateNotifications.Value;
            }

            try { await ctx.SaveChangesAsync(); }
            catch (Exception e) { loggingDelegte?.Invoke($@"{e.Message} {e.TargetSite}"); }

            // Update Category Subscriptions

            var subscriberCategories = ctx.CategorySubscriptions
                                       .Where(sc => sc.SubscriberId == subscriber.Id)
                                       .Select(sc => sc.Category)
                                       .ToList();

            var subscriberCategoryInfo = (await subscriberCategoriesTask).Data.Items;

            // existing category subscriptions - do nothing.

            var newCategorySubscriptions = subscriberCategoryInfo
                                           .Where(sci => !subscriberCategories.Any(sc => sc.Code == sci.CategoryCode));

            foreach (var nCS in newCategorySubscriptions)
            {
                var cat = ctx.Categories.First(c => c.Code == nCS.CategoryCode);
                ctx.CategorySubscriptions.Add(new CategorySubscription
                {
                    CategoryId   = cat.Id,
                    Category     = cat,
                    SubscriberId = subscriber.Id,
                    Subscriber   = subscriber
                });
            }

            var deleteableCategorySubscriptions = subscriberCategories
                                                  .Where(sc => !subscriberCategoryInfo.Any(sci => sci.CategoryCode == sc.Code))
                                                  .ToList();

            foreach (var dSC in deleteableCategorySubscriptions)
            {
                var categorySub = ctx.CategorySubscriptions.First(cs => cs.Category.Code == dSC.Code);
                ctx.CategorySubscriptions.Remove(categorySub);
            }

            await ctx.SaveChangesAsync();

            // Find new Topic subscriptions

            var subscriberTopics = ctx.TopicSubscriptions
                                   .Where(ts => ts.SubscriberId == subscriber.Id)
                                   .Select(ts => ts.Topic)
                                   .ToList();

            var subscriberTopicInfo = (await subscriberTopicsTask).Data.Items;

            var newTopicSubscriptions = subscriberTopicInfo
                                        .Where(sti => !subscriberTopics.Any(st => st.Code == sti.TopicCode))
                                        .ToList();

            foreach (var nTS in newTopicSubscriptions)
            {
                var topic = ctx.Topics.First(t => t.Code == nTS.TopicCode);
                ctx.TopicSubscriptions.Add(new TopicSubscription
                {
                    Subscriber   = subscriber,
                    SubscriberId = subscriber.Id,
                    Topic        = topic,
                    TopicId      = topic.Id,
                });
            }

            try
            {
                await ctx.SaveChangesAsync();
            }
            catch (Exception e)
            {
                loggingDelegte?.Invoke($@"{e.Message} {e.TargetSite}");
            }

            // deleteable topic subscriptions
            var deleteableTopicSubscriptions = subscriberTopics
                                               .Where(st => !subscriberTopicInfo.Any(sti => sti.TopicCode == st.Code))
                                               .ToList();

            foreach (var dts in deleteableTopicSubscriptions)
            {
                var topicSub = ctx.TopicSubscriptions.First(ts => ts.TopicId == dts.Id);
                ctx.TopicSubscriptions.Remove(topicSub);
            }

            try
            {
                await ctx.SaveChangesAsync();
            }
            catch (Exception e)
            {
                loggingDelegte?.Invoke($@"{e.Message} {e.TargetSite}");
            }
        }