Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. public override async Task<bool> UpdateAsync(NotificationSettingModel model)
  2. {
  3. //// 1 2
  4. /// 1 2 null
  5. /// [] []
  6. /// [] [] []
  7. var entity = await _repository.GetAsync((long)model.Id);
  8. if (entity == null)
  9. {
  10. throw new ArgumentNullException(nameof(entity));
  11. }
  12.  
  13. entity.NotificationEnabled = model.NotificationEnabled;
  14. entity.EmailNotificationEnabled = model.EmailNotificationEnabled;
  15. entity.PushNotificationEnabled = model.PushNotificationEnabled;
  16.  
  17. entity.SubscriptionItems = model.SelectedSettingsItems.Where(b => b.Id != 0)
  18. .Select(_ => new NotificationSettingsItem
  19. {
  20. Id = (long)_.Id,
  21. NotificationSetting = entity,
  22. ProjectId = _.TargetType == NotificationSettingType.Project ? _.ProjectId : default(long?),
  23. PersonId = _.TargetType == NotificationSettingType.Person ? _.UserId : string.Empty
  24. }).ToList();
  25.  
  26. entity.BranchSubscriptions = model.SelectedBranches.Where(b => b.Id != 0)
  27. .Select(_ => new NotificationBranchSubscription { Id = (long)_.Id, ProjectId = _.ProjectId, Name = _.Name })
  28. .ToList();
  29.  
  30. //Deleted -> Pass to the Update and delete from DB manually
  31. var deletedId = entity.SubscriptionItems
  32. .Where(c => !model.SelectedSettingsItems.Select(ssi => ssi.Id).Contains(c.Id));
  33. // Added (List of notificationSettingItems )-> Pass to the Update method in repo and save them to the db, add them to the entity and save the changes
  34. var addedEntities = entity.SubscriptionItems
  35. .Where(c => !model.SelectedSettingsItems.Select(ssi => ssi.Id).Contains(c.Id));
  36.  
  37. _repository.Update(entity,deltedIds, addedEntities);
  38.  
  39. return true;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement