/// <summary>
        /// Add a reader for a message.
        /// </summary>
        /// <param name="message">The message to be marked as read by a reader.</param>
        /// <param name="name">The name of the reader.</param>
        /// <returns>true, if the reader was added;
        /// false, if the message was already marked as read by the reader or is not in this store.</returns>
        public bool AddReader(IMessage message, string name)
        {
            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <Reader> readerQuery = from rdr in context.Readers
                                                  where rdr.MessageId == message.Id && rdr.Name == name
                                                  select rdr;

                if (readerQuery.Count() == 0)
                {
                    IQueryable <StoredMessage> messageQuery = from msg in context.Messages
                                                              where msg.Id == message.Id
                                                              select msg;

                    if (messageQuery.Count() != 0)
                    {
                        Reader reader = new Reader();

                        reader.MessageId = message.Id;
                        reader.Name      = name;

                        context.Readers.InsertOnSubmit(reader);
                        context.SubmitChanges();

                        return(true);
                    }
                }

                return(false);
            }
        }
        /// <summary>
        /// Returns all non-deleted messages in this store for the given channel and subchannel.
        /// </summary>
        /// <param name="channel">The channel for which messages are required.</param>
        /// <param name="subchannel">The subchannel for which messages are required.
        /// If null, messages for all subchannels are returned.</param>
        /// <returns>All messages for the given channel and subchannel.</returns>
        public IList <IMessage> GetMessages(string channel, string subchannel)
        {
            channel = channel.ToLowerInvariant();

            if (string.IsNullOrEmpty(subchannel))
            {
                using (MessageDataContext context = new MessageDataContext(connectionString))
                {
                    IQueryable <StoredMessage> query = from msg in context.Messages
                                                       where !msg.IsDeleted && msg.Channel == channel
                                                       select msg;

                    return(MakeMessages(query.ToList <StoredMessage>()));
                }
            }
            else
            {
                subchannel = subchannel.ToLowerInvariant();

                using (MessageDataContext context = new MessageDataContext(connectionString))
                {
                    IQueryable <StoredMessage> query = from msg in context.Messages
                                                       where !msg.IsDeleted && msg.Channel == channel && msg.Subchannel == subchannel
                                                       select msg;

                    return(MakeMessages(query.ToList <StoredMessage>()));
                }
            }
        }
        private void PurgeExpiredMessages()
        {
            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <string> expiredIdsQuery = from msg in context.Messages
                                                      where msg.ExpiryDate < DateTime.Now
                                                      select msg.Id;

                List <string> expiredIds = expiredIdsQuery.ToList();

                OnMessagesExpired(expiredIds);

                IQueryable <Reader> expiredReadersQuery = from rdr in context.Readers
                                                          where expiredIds.Contains(rdr.MessageId)
                                                          select rdr;

                context.Readers.DeleteAllOnSubmit(expiredReadersQuery.ToList());

                IQueryable <StoredMessage> expiredMessagesQuery = from msg in context.Messages
                                                                  where msg.ExpiryDate < DateTime.Now
                                                                  select msg;

                context.Messages.DeleteAllOnSubmit(expiredMessagesQuery.ToList());
                context.SubmitChanges();
            }
        }
        /// <summary>
        /// Adds a message to this store.
        /// </summary>
        /// <param name="message">The message to add.</param>
        /// <returns>true, if the message was added; false, if the message is already in this store.</returns>
        public bool Add(IMessage message)
        {
            if (message.ExpiryDate >= DateTime.Now)
            {
                using (MessageDataContext context = new MessageDataContext(connectionString))
                {
                    IQueryable <StoredMessage> query = from msg in context.Messages
                                                       where msg.Id == message.Id
                                                       select msg;

                    if (query.Count() == 0)
                    {
                        StoredMessage storedMessage = MakeStoredMessage(message);

                        context.Messages.InsertOnSubmit(storedMessage);
                        context.SubmitChanges();

                        OnChanged(new MessageStoreChangedEventArgs(message, MessageAction.Created));

                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else
            {
                return(AddAsDeleted(message));
            }
        }
        /// <summary>
        /// Returns all non-deleted messages in this store for the given channel and subchannel that
        /// have not been read by the given reader.
        /// </summary>
        /// <param name="channel">The channel for which messages are required.</param>
        /// <param name="subchannel">The subchannel for which messages are required.
        /// If null, messages for all subchannels are returned.</param>
        /// <param name="reader">The reader who wants the unread messages.</param>
        /// <returns>All messages for the given channel and subchannel.</returns>
        public IList <IMessage> GetAllUnreadMessages(string channel, string subchannel, string reader)
        {
            channel = channel.ToLowerInvariant();

            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <string> readersQuery = from rdr in context.Readers
                                                   where rdr.Name == reader
                                                   select rdr.MessageId;

                List <string> readIds = readersQuery.ToList();

                if (string.IsNullOrEmpty(subchannel))
                {
                    IQueryable <StoredMessage> query = from msg in context.Messages
                                                       where !msg.IsDeleted && msg.Channel == channel && !readIds.Contains(msg.Id)
                                                       select msg;

                    return(MakeMessages(query.ToList <StoredMessage>()));
                }
                else
                {
                    subchannel = subchannel.ToLowerInvariant();

                    IQueryable <StoredMessage> query = from msg in context.Messages
                                                       where !msg.IsDeleted && msg.Channel == channel && msg.Subchannel == subchannel && !readIds.Contains(msg.Id)
                                                       select msg;

                    return(MakeMessages(query.ToList <StoredMessage>()));
                }
            }
        }
 private void CreateDatabase()
 {
     using (MessageDataContext context = new MessageDataContext(connectionString))
     {
         if (!context.DatabaseExists())
         {
             context.CreateDatabase();
         }
     }
 }
        /// <summary>
        /// Returns all non-deleted messages in this store.
        /// </summary>
        /// <returns>All messages in this store.</returns>
        public IList <IMessage> GetMessages()
        {
            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <StoredMessage> query = from msg in context.Messages
                                                   where !msg.IsDeleted
                                                   select msg;

                return(MakeMessages(query.ToList <StoredMessage>()));
            }
        }
        /// <summary>
        /// Returns all readers for the given message.
        /// </summary>
        /// <param name="message">The message for which we want the readers.</param>
        /// <returns>The list of readers of the message.</returns>
        public IList <string> GetAllReaders(IMessage message)
        {
            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <string> query = from rdr in context.Readers
                                            where rdr.MessageId == message.Id
                                            select rdr.Name;

                return(query.ToList <string>());
            }
        }
        /// <summary>
        /// Returns the message with the given ID.
        /// </summary>
        /// <param name="id">The ID of the message.</param>
        /// <returns>The message with the given ID, null if no such message.</returns>
        public IMessage GetMessage(string id)
        {
            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <StoredMessage> query = from msg in context.Messages
                                                   where msg.Id == id && !msg.IsDeleted
                                                   select msg;

                return(query.Count() == 0 ? null : MakeMessage(query.First()));
            }
        }
コード例 #10
0
ファイル: Startup.cs プロジェクト: astannard/edays-challenge
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            //DB migration runner
            var ctx = new MessageDataContext();

            ctx.Database.EnsureCreated();
            ctx.CheckInitialDataSetup();
            ctx.SaveChanges();
            ctx.Dispose();
        }
        /// <summary>
        /// Get all messages for the provider with the given name.
        /// </summary>
        /// <param name="providerName">The name of the provider</param>
        /// <returns>All messages in this store for the given provdider</returns>
        public IList <IMessage> GetMessages(string providerName)
        {
            MessageProviderFactory factory = MessageProviderFactory.Instance;

            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <StoredMessage> query = from msg in context.Messages
                                                   where !msg.IsDeleted
                                                   select msg;

                return(MakeMessages(query.ToList <StoredMessage>()));
            }
        }
        /// <summary>
        /// Removes the message with the given ID from the store.
        /// </summary>
        /// <param name="id">The ID of the message to remove.</param>
        /// <returns>true, if the message with the given ID was removed;
        /// false, if no message with the given ID was in this store.</returns>
        public bool Remove(string id)
        {
            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <StoredMessage> query = from msg in context.Messages
                                                   where msg.Id == id
                                                   select msg;

                if (query.Count() > 0)
                {
                    StoredMessage   storedMessage     = query.First();
                    IMessage        message           = MakeMessage(storedMessage);
                    MessageProvider provider          = MessageProviderFactory.Instance.GetProvider(message.Provider);
                    bool            deleteImmediately = false;

                    contentManager.DeleteFiles(message.Content);

                    if (provider != null)
                    {
                        deleteImmediately = !provider.NeedsDeletionStubs;
                    }

                    if (deleteImmediately)
                    {
                        IQueryable <Reader> deletedReadersQuery = from rdr in context.Readers
                                                                  where rdr.MessageId == id
                                                                  select rdr;

                        context.Readers.DeleteAllOnSubmit(deletedReadersQuery.ToList());
                        context.Messages.DeleteOnSubmit(storedMessage);
                        context.SubmitChanges();
                    }
                    else
                    {
                        storedMessage.IsDeleted     = true;
                        storedMessage.ContentString = null;
                        storedMessage.Notification  = null;
                        storedMessage.ExpiryDate    = DateTime.Now + TimeSpan.FromDays(2);
                        context.SubmitChanges();
                    }

                    OnChanged(new MessageStoreChangedEventArgs(message, MessageAction.Deleted));

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        /// <summary>
        /// Clears the store.
        /// </summary>
        public void Clear()
        {
            canFireChangedHandler = false;

            try
            {
                ContentManager contentManager = new ContentManager(logger);

                using (MessageDataContext context = new MessageDataContext(connectionString))
                {
                    IQueryable <StoredMessage> query = from msg in context.Messages
                                                       where !msg.IsDeleted
                                                       select msg;

                    IList <IMessage> messages = MakeMessages(query.ToList <StoredMessage>());

                    foreach (IMessage message in messages)
                    {
                        contentManager.DeleteFiles(message.Content);
                    }
                }

                using (MessageDataContext context = new MessageDataContext(connectionString))
                {
                    IQueryable <Reader> expiredReadersQuery = from rdr in context.Readers
                                                              select rdr;

                    context.Readers.DeleteAllOnSubmit(expiredReadersQuery.ToList());

                    IQueryable <StoredMessage> expiredMessagesQuery = from msg in context.Messages
                                                                      select msg;

                    context.Messages.DeleteAllOnSubmit(expiredMessagesQuery.ToList());
                    context.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                if (logger != null)
                {
                    logger.WarnFormat("Cannot clear {0}: {1}", this, e.Message);
                }
            }

            canFireChangedHandler = true;
        }
        /// <summary>
        /// Mark all unread messages as newly created, firing the Changed event.
        /// </summary>
        public void MarkAllUnreadMessagesAsCreated()
        {
            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <string> readersQuery = from rdr in context.Readers
                                                   select rdr.MessageId;

                List <string> readIds = readersQuery.ToList();

                IQueryable <StoredMessage> query = from msg in context.Messages
                                                   where !msg.IsDeleted && !readIds.Contains(msg.Id)
                                                   select msg;

                foreach (IMessage message in MakeMessages(query.ToList <StoredMessage>()))
                {
                    OnChanged(new MessageStoreChangedEventArgs(message, MessageAction.Created));
                }
            }
        }
        /// <summary>
        /// Send all pending messages.
        /// </summary>
        public void SendAllMessages()
        {
            MessageProviderFactory factory = MessageProviderFactory.Instance;

            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <StoredMessage> query = from msg in context.Messages
                                                   where !msg.IsDeleted
                                                   select msg;

                IList <IMessage> messages = MakeMessages(query.ToList <StoredMessage>());

                foreach (IMessage message in messages)
                {
                    MessageProvider provider = factory.GetProvider(message.Provider);

                    if (provider != null)
                    {
                        provider.Send(message);
                    }
                }
            }
        }
        /// <summary>
        /// Updates a message, provided it exists in the store.
        /// </summary>
        /// <param name="message">The message to update.</param>
        /// <returns>Wther the message was updated.</returns>
        public bool Update(IMessage message)
        {
            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <StoredMessage> query = from msg in context.Messages
                                                   where msg.Id == message.Id
                                                   select msg;

                if (query.Count() == 1)
                {
                    StoredMessage storedMessage = query.First <StoredMessage>();

                    UpdateStoredMessage(message, ref storedMessage);
                    context.SubmitChanges();

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        /// <summary>
        /// Adds a message to this store and marks it as deleted. Changed events will <i>not</i> be fired.
        /// </summary>
        /// <param name="message">The message to add.</param>
        /// <returns>true, if the message was added; false, if the message is already in this store.</returns>
        private bool AddAsDeleted(IMessage message)
        {
            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <StoredMessage> query = from msg in context.Messages
                                                   where msg.Id == message.Id
                                                   select msg;

                if (query.Count() == 0)
                {
                    StoredMessage storedMessage = MakeStoredMessage(message);

                    storedMessage.IsDeleted = true;
                    context.Messages.InsertOnSubmit(storedMessage);
                    context.SubmitChanges();

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        /// <summary>
        /// Logs all messages in this store.
        /// </summary>
        public void LogAllMessages()
        {
            logger.InfoFormat("{0}:", name);

            using (MessageDataContext context = new MessageDataContext(connectionString))
            {
                IQueryable <StoredMessage> query = from msg in context.Messages
                                                   where !msg.IsDeleted
                                                   select msg;

                foreach (StoredMessage message in query.ToList <StoredMessage>())
                {
                    logger.InfoFormat("  {0} | {1} | {2} | {3} | {4} | {5} | {6} | {7}",
                                      message.Id,
                                      message.Channel,
                                      message.Subchannel,
                                      CollapseNewLines(message.ContentString),
                                      CollapseNewLines(message.Notification),
                                      message.CreatedDate,
                                      message.ExpiryDate,
                                      message.IsDeleted ? "<Deleted" : "");
                }
            }
        }
コード例 #19
0
 public MessageController(MessageDataContext context)
 {
     contextThis = context;
 }
コード例 #20
0
 public MessageRepository(IOptions <PersistenceOptions> settings)
 {
     _context = new MessageDataContext(settings);
 }
コード例 #21
0
 public MessageDataRepository(MessageDataContext context)
 {
     _context = context;
 }