/// <summary> /// Can be done by sending the whole assembly /// to it instead of string but i don't know how practical that is /// </summary> /// <param name="assemblyName"></param> /// <returns></returns> public virtual async Task ImportAndCreateEvents(Assembly assemblyName) { var importEvents = assemblyName.GetTypes() .Where(typeof(INotification).IsAssignableFrom) .Select(x => x.Name).Except(_notificationsContext.Events .Select(x => x.EventName)) .Select(@event => new Event() { Id = Guid.NewGuid(), EventName = @event, NotificationTypes = JsonConvert.SerializeObject(new List <NotificationType>() { NotificationType.Action }), PropagationTypes = JsonConvert.SerializeObject(new List <PropagationType>() { PropagationType.Email, PropagationType.Application }) }).ToList(); await _notificationsContext.AddRangeAsync(importEvents); await _notificationsContext.SaveChangesAsync(); }
public virtual async Task UpdateNotificationProfile(NotificationProfileModelDto dto) { if (dto.Id == Guid.Empty) { throw new IdNullOrEmptyException(); } var entity = await _notificationsContext.NotificationProfiles.FindAsync(dto.Id); if (entity == null) { throw new NotFoundException(typeof(NotificationProfile).Name, $"{dto.Id}"); } entity.Name = dto.Name; #region NotificationUsers var notificationUsersInDb = await _notificationsContext.NotificationUsers.Where(x => x.NotificationProfileId == entity.Id) .Select(x => x.UserId).ToListAsync(); var notificationUsersToAdd = dto.Users.Except(notificationUsersInDb).ToList(); var notificationUsersToDelete = notificationUsersInDb.Except(dto.Users).ToList(); var notificationUsersToAddEntity = notificationUsersToAdd. Select(userId => new NotificationUser() { UserId = userId, NotificationProfileId = entity.Id }).ToList(); var notificationUsersToDeleteEntity = _notificationsContext.NotificationUsers.Where(x => notificationUsersToDelete.Contains(x.UserId)); _notificationsContext.NotificationUsers.RemoveRange(notificationUsersToDeleteEntity); await _notificationsContext.NotificationUsers.AddRangeAsync(notificationUsersToAddEntity); #endregion #region Events var notificationEvents = await _notificationsContext.NotificationEvents .Where(x => x.NotificationProfileId == entity.Id) .Select(x => x.EventId).ToListAsync(); var notificationEventsToAdd = dto.Events.Except(notificationEvents).ToList(); var notificationEventsToDelete = notificationEvents.Except(dto.Events).ToList(); var notificationEventsToAddEntity = notificationEventsToAdd.Select(eventId => new NotificationEvent() { EventId = eventId, NotificationProfileId = entity.Id }).ToList(); var notificationEventsToDeleteEntity = _notificationsContext.NotificationEvents.Where(x => notificationEventsToDelete.Contains(x.EventId)); _notificationsContext.NotificationEvents.RemoveRange(notificationEventsToDeleteEntity); await _notificationsContext.AddRangeAsync(notificationEventsToAddEntity); #endregion await _notificationsContext.SaveChangesAsync(); }