public List <SubscriptionGroupDescription> GetSubscriptionGroups(bool onlyConfirmed) { try { var list = _subscriptionGroups.Values.ToDictionary(x => x.Id, x => x); if (!onlyConfirmed) { using (var db = new Db.DataContext()) using (var scope = db.CreateScope(System.Transactions.TransactionScopeOption.Suppress)) { var query = db.SubscriptionGroup; var dbList = query.ToList(); foreach (var rowDb in dbList) { if (list.ContainsKey(rowDb.IdGroup)) { continue; } var description = CreateUnconfirmedSubscriptionGroupDescription(rowDb); list[rowDb.IdGroup] = description; } } } return(list.Values.ToList()); } catch (Exception ex) { this.RegisterEvent(Journaling.EventType.CriticalError, "Ошибка во время получения списка групп подписок", null, ex); throw new Exception("Неожиданная ошибка во время получения списка групп подписок."); } }
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); }
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); }
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)); } }
public List <SubscriptionDescription> GetSubscriptions(bool onlyConfirmed) { try { var list = _subscriptions.Values.ToDictionary(x => x.Item2.Id, x => x.Item2); if (!onlyConfirmed) { using (var db = new Db.DataContext()) using (var scope = db.CreateScope(System.Transactions.TransactionScopeOption.Suppress)) { var query = from s in db.Subscription join sg in db.SubscriptionGroup on s.IdGroup equals sg.IdGroup select new { s, sg }; var dbList = query.ToList(); foreach (var rowDb in dbList) { if (list.ContainsKey(rowDb.s.IdSubscription)) { continue; } var subscriptionGroupDescription = _subscriptionGroups.Values.FirstOrDefault(x => x.Id == rowDb.s.IdGroup); if (subscriptionGroupDescription == null) { subscriptionGroupDescription = CreateUnconfirmedSubscriptionGroupDescription(rowDb.sg); } var description = new SubscriptionDescription { Id = rowDb.s.IdSubscription, Name = rowDb.s.NameSubscription, IsConfirmed = false, SubscriptionGroup = subscriptionGroupDescription }; list[rowDb.s.IdSubscription] = description; } } } return(list.Values.ToList()); } catch (Exception ex) { this.RegisterEvent(Journaling.EventType.CriticalError, "Ошибка во время получения списка подписок", null, ex); throw new Exception("Неожиданная ошибка во время получения списка подписок."); } }
internal ExecutionResult <Exception> ExecuteSubscription <TSubscription, TParameters>(TParameters parameters) where TSubscription : SubscriptionBase <TSubscription, TParameters> { if (!_subscriptions.TryGetValue(typeof(TSubscription), out var info)) { return(new ExecutionResult <Exception>( false, "Указанная подписка не найдена среди подтвержденных", new ArgumentException("Указанная подписка не найдена среди подтвержденных", nameof(TSubscription)))); } var subscriptionInstance = (TSubscription)info.Item1; try { var messagingContacts = new MessagingContacts(); using (var db = new Db.DataContext()) { var queryFromUsers = db.SubscriptionUser.Where(x => x.IdSubscription == subscriptionInstance.SubscriptionDescription.Id).Select(x => new { x.IdUser }); var dataFromUsers = queryFromUsers.ToList(); var userIdList = dataFromUsers.Select(x => x.IdUser).ToList(); var queryFromContacts = from smc in db.SubscriptionMessagingContact join mc in db.MessagingContact on smc.IdMessagingContact equals mc.IdMessagingContact join mcd in db.MessagingContactData on mc.IdMessagingContact equals mcd.IdMessagingContactData where smc.IdSubscription == subscriptionInstance.SubscriptionDescription.Id select new { mc.NameFull, mcd.IdMessagingContact, mcd.IdMessagingServiceType, mcd.Data }; var types = new Dictionary <int, Type>(); var getType = new Func <int, Type>(idMessagingServiceType => { if (types.TryGetValue(idMessagingServiceType, out var t)) { return(t); } t = Core.Items.ItemTypeFactory.GetClsType(idMessagingServiceType); types[idMessagingServiceType] = t; return(t); }); var dataFromContacts = queryFromContacts. ToList(). GroupBy(x => new { x.IdMessagingContact, x.NameFull }, x => new { x.Data, x.IdMessagingServiceType }). Select(x => new MessagingContact() { Id = x.Key.IdMessagingContact, NameFull = x.Key.NameFull, _data = x.GroupBy(y => y.IdMessagingServiceType, y => y.Data).ToDictionary(y => getType(y.Key), y => y.ToList()) }); messagingContacts.AddRange(dataFromContacts); } OnSendBeforeExecution(subscriptionInstance, parameters, messagingContacts); foreach (var pair in subscriptionInstance._services) { try { var messagingService = AppCore.Get <IMessagingService>(pair.Key); if (messagingService == null) { var msg = $"Рассылка: '{subscriptionInstance.SubscriptionDescription.Id}' / '{subscriptionInstance.SubscriptionDescription.Name}'.\r\n"; msg += $"Коннектор: '{pair.Key.FullName}' / '{pair.Value.GetType().FullName}'."; this.RegisterEvent(Journaling.EventType.Error, "Ошибка поиска сервиса отправки/получения сообщений для рассылки", msg); } pair.Value(messagingService, subscriptionInstance.SubscriptionDescription, parameters, messagingContacts); } catch (Exception ex) { var msg = $"Рассылка: '{subscriptionInstance.SubscriptionDescription.Id}' / '{subscriptionInstance.SubscriptionDescription.Name}'.\r\n"; msg += $"Коннектор: '{pair.Key.FullName}' / '{pair.Value.GetType().FullName}'."; this.RegisterEvent(Journaling.EventType.Error, "Неожиданная ошибка рассылки", msg, ex); } } return(new ExecutionResult <Exception>(true)); } catch (Exception ex) { var msg = $"Рассылка: '{subscriptionInstance.SubscriptionDescription.Id}' / '{subscriptionInstance.SubscriptionDescription.Name}'.\r\n"; this.RegisterEvent(Journaling.EventType.Error, "Неожиданная ошибка рассылки", msg, ex); return(new ExecutionResult <Exception>(false, "Неожиданная ошибка рассылки", ex)); } }