Exemplo n.º 1
0
 public SantaOrdersController(
     SantaOrdersService ordersService,
     NotificationsHubHelper notificationsHub)
 {
     _santaOrdersService = ordersService;
     _notificationsHub   = notificationsHub;
 }
        public async Task <ActionResult> Create([Bind(Include = "NotificationID,TypeOfNotification,ResourceId,ActionLink,NotificationTitle,NotificationMessage,PushDateTime")] Notification notification,
                                                List <string> TechTagNames, List <string> AudienceTypeNames, string NotificationType)

        {
            if (ModelState.IsValid)
            {
                notification.PushDateTime       = DateTime.UtcNow;
                notification.TypeOfNotification = (NotificationType)Enum.Parse(typeof(NotificationType), NotificationType, true);
                db.Notifications.Add(notification);

                notification.TechTags         = new List <PrimaryTechnology>();
                notification.AudienceTypeTags = new List <AudienceType>();
                notification.SentToUsers      = new List <AppUserNotificationAction>();

                if (TechTagNames != null)
                {
                    foreach (var techTag in TechTagNames)
                    {
                        //primary technology from client.
                        var primaryTech = db.PrimaryTechnologies.FirstOrDefault(x => x.PrimaryTech == techTag);
                        if (primaryTech != null)
                        {
                            notification.TechTags.Add(primaryTech);
                        }

                        var appUsers = db.AppUsers.Where(p => p.TechnologyTags.Any(c => primaryTech.PrimaryTechnologyID == c.PrimaryTechnologyID));
                        foreach (var user in appUsers)
                        {
                            //add app users to this notification.
                            if (notification.SentToUsers.FirstOrDefault(x => x.AppUserID == user.AppUserID) == null)
                            {
                                notification.SentToUsers.Add(
                                    new AppUserNotificationAction()
                                {
                                    AppUserID = user.AppUserID,
                                    Read      = false
                                });
                            }
                        }
                    }
                }

                if (AudienceTypeNames != null)
                {
                    //add audience type names.
                    foreach (var audTypeTag  in AudienceTypeNames)
                    {
                        var audType = db.AudienceTypes.FirstOrDefault(x => x.TypeOfAudience == audTypeTag);
                        if (audType != null)
                        {
                            notification.AudienceTypeTags.Add(audType);
                        }

                        var appUsers = db.AppUsers.Where(p => p.AudienceOrg.AudienceTypeID == audType.AudienceTypeID);
                        foreach (var user in appUsers)
                        {
                            //no app user exist to send this notification.
                            if (notification.SentToUsers.FirstOrDefault(x => x.AppUserID == user.AppUserID) == null)
                            {
                                notification.SentToUsers.Add(
                                    new AppUserNotificationAction()
                                {
                                    AppUserID = user.AppUserID,
                                    Read      = false
                                });
                            }
                        }
                    }
                }
                await db.SaveChangesAsync();

                if (TechTagNames != null)
                {
                    if (AudienceTypeNames != null)
                    {
                        await NotificationsHubHelper.SendNotificationAsync(TechTagNames.Union(AudienceTypeNames).Distinct().ToList().Select(x => x.Replace(" ", "_")).ToList(), notification);
                    }
                }

                return(RedirectToAction("Index"));
            }

            return(View(notification));
        }