public void UpdateMessageStatus(int messageid)
 {
     using (NotificationEntities dc = new NotificationEntities())
     {
         var message = dc.Messages.Where(m => m.Id == messageid && m.IsRead == false).FirstOrDefault();
         message.IsRead = true;
         dc.SaveChanges();
     }
 }
 public void AddApplication(string applicationname)
 {
     using (NotificationEntities dc = new NotificationEntities())
     {
         Application application = new Application {
             Name = applicationname
         };
         dc.Applications.Add(application);
         dc.SaveChanges();
     }
 }
 public void RemoveUserConnection(string connectionid)
 {
     using (NotificationEntities dc = new NotificationEntities())
     {
         UserConnection connection = dc.UserConnections.Where(x => x.Id == connectionid).FirstOrDefault();
         if (connection != null)
         {
             dc.UserConnections.Remove(connection);
             dc.SaveChanges();
         }
     }
 }
        public void SubscribeEvent(string applicationname, string username, int[] eventids)
        {
            string message = string.Empty;

            using (NotificationEntities dc = new NotificationEntities())
            {
                dc.Database.BeginTransaction();
                //Check if application exists
                try
                {
                    Application application = dc.Applications.Where(a => a.Name == applicationname).FirstOrDefault();
                    if (application == null)
                    {
                        throw new Exception(string.Format("The application with name {0} doen not exists.", applicationname));
                    }

                    if (eventids.Count() > 0)
                    {
                        dc.EventSubscriptions.RemoveRange(dc.EventSubscriptions.Where(a => a.UserID == username && a.Event.Application.Name == applicationname).ToList());

                        foreach (var item in eventids)
                        {
                            EventSubscription subscription = new EventSubscription
                            {
                                EventID = item,
                                UserID  = username
                            };

                            dc.EventSubscriptions.Add(subscription);
                        }

                        var user = dc.Users.Where(u => u.Application.Name == applicationname && u.UserName == username).FirstOrDefault();
                        if (user == null)
                        {
                            user = new User
                            {
                                ApplicationID = application.Id,
                                UserName      = username
                            };

                            dc.Users.Add(user);
                        }
                        dc.SaveChanges();
                        dc.Database.CurrentTransaction.Commit();
                    }
                }
                catch (Exception ex)
                {
                    dc.Database.CurrentTransaction.Rollback();
                    throw new Exception(ex.Message);
                }
            }
        }
 public void UpdateMessageStatus(string username)
 {
     using (NotificationEntities dc = new NotificationEntities())
     {
         var messages = dc.Messages.Where(m => m.ReceiverUserID == username && m.IsRead == false).ToList();
         messages.ForEach(m =>
         {
             m.IsRead           = true;
             m.NotificationTime = DateTime.Now;
         });
         dc.SaveChanges();
     }
 }
        public void AddUserConnection(NotificationUserConnection connection)
        {
            using (NotificationEntities dc = new NotificationEntities())
            {
                UserConnection userconnection = new UserConnection
                {
                    Id        = connection.ConnectionId,
                    UserName  = connection.UserName,
                    UserAgent = connection.UserAgent
                };

                dc.UserConnections.Add(userconnection);
                dc.SaveChanges();
            }
        }
        public void SubscribeEvent(string applicationname, string username, int eventid)
        {
            using (NotificationEntities dc = new NotificationEntities())
            {
                Application application = dc.Applications.Where(a => a.Name == applicationname).FirstOrDefault();
                if (application == null)
                {
                    throw new Exception(string.Format("The application with name {0} doen not exists.", applicationname));
                }

                Event myevent = dc.Events.Where(a => a.Id == eventid).FirstOrDefault();

                if (myevent == null)
                {
                    throw new Exception(string.Format("The application event with name {0} doen not exists.", eventid));
                }

                if (dc.EventSubscriptions.Any(s => s.EventID == eventid && s.UserID == username))
                {
                    throw new Exception(string.Format("A mapping for EventID {0} and UserName {1} already exists.", eventid, username));
                }

                var user = dc.Users.Where(u => u.Application.Name == applicationname && u.UserName == username).FirstOrDefault();
                if (user == null)
                {
                    user = new User
                    {
                        ApplicationID = application.Id,
                        UserName      = username
                    };

                    dc.Users.Add(user);
                }

                EventSubscription subscription = new EventSubscription
                {
                    EventID = eventid,
                    UserID  = username
                };

                dc.EventSubscriptions.Add(subscription);

                dc.SaveChanges();
            }
        }
        public void AddMessage(string receiverusername, string notificationmessage, int eventid)
        {
            using (NotificationEntities dc = new NotificationEntities())
            {
                //User has subscribed for event then add message to users message notification queue.
                if (dc.EventSubscriptions.Any(s => s.UserID == receiverusername && s.EventID == eventid))
                {
                    Message message = new Message
                    {
                        ReceiverUserID      = receiverusername,
                        NotificationMessage = notificationmessage,
                        EventID             = eventid,
                        IsRead           = false,
                        NotificationTime = DateTime.Now
                    };

                    dc.Messages.Add(message);
                    dc.SaveChanges();
                }
            }
        }