Ejemplo n.º 1
0
        private SubscriptionGroupDescription UpdateSubscriptionGroup(SubscriptionGroupDescription subscriptionGroupDescription, string name, Guid uniqueKey)
        {
            using (var db = new Db.DataContext())
                using (var scope = db.CreateScope(System.Transactions.TransactionScopeOption.Suppress))
                {
                    var subscriptionGroupDb = db.SubscriptionGroup.Where(x => x.UniqueKey == uniqueKey).FirstOrDefault();
                    if (subscriptionGroupDb == null)
                    {
                        subscriptionGroupDb = new Db.SubscriptionGroup()
                        {
                            NameGroup = name,
                            UniqueKey = uniqueKey
                        };
                        db.SubscriptionGroup.Add(subscriptionGroupDb);
                        db.SaveChanges();
                    }

                    subscriptionGroupDb.NameGroup = name;
                    db.SaveChanges();

                    subscriptionGroupDescription.Id          = subscriptionGroupDb.IdGroup;
                    subscriptionGroupDescription.Name        = name;
                    subscriptionGroupDescription.IsConfirmed = true;
                    subscriptionGroupDescription.UniqueKey   = uniqueKey;
                }
            return(subscriptionGroupDescription);
        }
Ejemplo n.º 2
0
        private SubscriptionDescription UpdateSubscription(
            string name,
            Guid uniqueKey,
            SubscriptionGroupDescription subscriptionGroupDescription,
            SubscriptionDescription subscriptionDescription)
        {
            using (var db = new Db.DataContext())
                using (var scope = db.CreateScope(System.Transactions.TransactionScopeOption.Suppress))
                {
                    var rowDb = db.Subscription.Where(x => x.UniqueKey == uniqueKey).FirstOrDefault();
                    if (rowDb == null)
                    {
                        rowDb = new Db.Subscription()
                        {
                            NameSubscription = name,
                            UniqueKey        = uniqueKey,
                            IdGroup          = subscriptionGroupDescription.Id
                        };
                        db.Subscription.Add(rowDb);
                        db.SaveChanges();
                    }

                    rowDb.NameSubscription = name;
                    rowDb.IdGroup          = subscriptionGroupDescription.Id;
                    db.SaveChanges();

                    subscriptionDescription.Id                = rowDb.IdSubscription;
                    subscriptionDescription.Name              = name;
                    subscriptionDescription.IsConfirmed       = true;
                    subscriptionDescription.SubscriptionGroup = subscriptionGroupDescription;
                }
            return(subscriptionDescription);
        }
Ejemplo n.º 3
0
        private ExecutionResult <Exception> UpdateSubscribersInternal(SubscriptionDescription subscriptionDescription, ChangeType changeType, int[] userIdList)
        {
            var subscriptionInfo = _subscriptions.FirstOrDefault(x => x.Value.Item2.Id == subscriptionDescription.Id).Value.Item2;

            if (subscriptionInfo != null)
            {
                return(new ExecutionResult <Exception>(
                           false,
                           "Указанная подписка не найдена среди подтвержденных",
                           new ArgumentException("Указанная подписка не найдена среди подтвержденных", nameof(subscriptionDescription))));
            }

            try
            {
                using (var db = new Db.DataContext())
                    using (var scope = db.CreateScope())
                    {
                        if (changeType == ChangeType.Append || changeType == ChangeType.Replace)
                        {
                            var list = userIdList.Select(x => new Db.SubscriptionUser()
                            {
                                IdSubscription = subscriptionInfo.Id, IdUser = x
                            }).ToList();
                            db.SubscriptionUser.UpsertRange(list).On(x => new { x.IdUser, x.IdSubscription }).NoUpdate().Run();
                        }

                        if (changeType == ChangeType.Replace)
                        {
                            var list = db.SubscriptionUser.Where(x => x.IdSubscription == subscriptionInfo.Id).ToList().Where(x => !userIdList.Contains(x.IdUser)).ToList();
                            db.SubscriptionUser.RemoveRange(list);
                            db.SaveChanges();
                        }

                        if (changeType == ChangeType.Remove)
                        {
                            var list = db.SubscriptionUser.Where(x => x.IdSubscription == subscriptionInfo.Id).ToList().Where(x => userIdList.Contains(x.IdUser)).ToList();
                            db.SubscriptionUser.RemoveRange(list);
                            db.SaveChanges();
                        }

                        scope.Complete();
                    }
                return(new ExecutionResult <Exception>(true));
            }
            catch (Exception ex)
            {
                var msg = $"Рассылка: '{subscriptionInfo.Id}' / '{subscriptionInfo.Name}'.\r\n";
                this.RegisterEvent(Journaling.EventType.Error, "Неожиданная ошибка обновления списка подписчиков", msg, ex);
                return(new ExecutionResult <Exception>(false, "Неожиданная ошибка обновления списка подписчиков", ex));
            }
        }