Ejemplo n.º 1
0
        private static void UpdateForumsSubscriptions(
            [NotNull] System.IServiceProvider serviceProvider,
            [NotNull] IEnumerable <ForumSubscriptionRequest> requests,
            [NotNull] DeleteMessagesPredicate deleteMessagesPredicate)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException("serviceProvider");
            }
            if (requests == null)
            {
                throw new ArgumentNullException("requests");
            }
            if (deleteMessagesPredicate == null)
            {
                throw new ArgumentNullException("deleteMessagesPredicate");
            }

            using (var mgr = serviceProvider.CreateDBContext())
            {
                var forums =
                    GetAllForums(
                        mgr,
                        f => new { f.ID, f.Name, f.Descript, Subscribed = false },
                        f => new { f.ID, f.Name, f.Descript, Subscribed = true })
                    .ToDictionary(forum => forum.ID);
                var requestsArray =
                    requests
                    .Where(request => request.IsSubscribed != forums[request.ForumId].Subscribed)
                    .ToArray();
                if (!requestsArray.Any())
                {
                    return;
                }
                var forumEntryIds =
                    requestsArray
                    .Select(request => new ForumEntryIds(request.ForumId))
                    .ToArray();
                var eventBroker = serviceProvider.GetRequiredService <IEventBroker>();

                eventBroker.Fire(
                    ForumEventNames.BeforeForumEntryChanged,
                    new ForumEntryChangedEventArgs(forumEntryIds, ForumEntryChangeType.ForumSubscription));

                foreach (var request in requestsArray)
                {
                    if (request.IsSubscribed)
                    {
                        Subscribe(serviceProvider, request.ForumId);
                    }
                    else
                    {
                        var forum = forums[request.ForumId];

                        Unsubscribe(
                            serviceProvider,
                            request.ForumId,
                            deleteMessagesPredicate(forum.ID, forum.Name, forum.Descript));
                    }
                }
                serviceProvider
                .GetRequiredService <IUIShell>()
                .CreateUIAsyncOperation()
                .PostOperationCompleted(() => serviceProvider.GetRequiredService <IUIShell>().RefreshData());

                eventBroker.Fire(
                    ForumEventNames.AfterForumEntryChanged,
                    new ForumEntryChangedEventArgs(forumEntryIds, ForumEntryChangeType.ForumSubscription));
            }
        }