Beispiel #1
0
        public IDatabaseError SaveMessageToStash(IStashMessage stashMessage)
        {
            if (!stashMessage.IsEncrypt)
            {
                throw new ArgumentException("An unencrypted message cannot be stored in a stash");
            }

            if (!stashMessage.IsDownloaded)
            {
                throw new ArgumentException("An undownloaded message cannot be stored in a stash");
            }

            StashMessageModel messageModel = ((IStashMessageDatabaseModelConverter)stashMessage).ToStashMessageModel();

            using (StashMessagesContext db = new StashMessagesContext())
            {
                if (!CheckStashLimit(stashMessage.ChatId, db))
                {
                    return(new StashFullError());
                }

                db.StashMessages.Add(messageModel);
                db.SaveChanges();
            }

            return(new NullError());
        }
Beispiel #2
0
        private bool CheckStashLimit(long chatId, StashMessagesContext db)
        {
            int count = db.StashMessages
                        .Where(message => message.ChatId == chatId)
                        .Count();

            return(count < StashSettings.StashMessageLimit);
        }
Beispiel #3
0
        public bool IsStashExist(long chatId)
        {
            using (StashMessagesContext db = new StashMessagesContext())
            {
                StashMessageModel messageModel = db.StashMessages
                                                 .Where(message => message.ChatId == chatId)
                                                 .FirstOrDefault();

                return(messageModel != null);
            }
        }
Beispiel #4
0
        public void ClearStash(long chatId)
        {
            using (StashMessagesContext db = new StashMessagesContext())
            {
                IQueryable <StashMessageModel> messageModels = db.StashMessages
                                                               .Where(message => message.ChatId == chatId);

                foreach (StashMessageModel messageModel in messageModels)
                {
                    db.Remove(messageModel);
                }

                db.SaveChanges();
            }
        }
Beispiel #5
0
        public void DeleteStashMessage(long chatId, long databaseMessageId)
        {
            using (StashMessagesContext db = new StashMessagesContext())
            {
                StashMessageModel messageModel = db.StashMessages
                                                 .Where(message => (message.ChatId == chatId) && (message.Id == databaseMessageId))
                                                 .FirstOrDefault();

                if (messageModel != null)
                {
                    db.Remove(messageModel);
                    db.SaveChanges();
                }
            }
        }
Beispiel #6
0
        public void SaveMessageToStash(IStashMessage stashMessage)
        {
            if (!stashMessage.IsEncrypt)
            {
                throw new ArgumentException("An unencrypted message cannot be stored in a stash");
            }

            if (!stashMessage.IsDownloaded)
            {
                throw new ArgumentException("An undownloaded message cannot be stored in a stash");
            }

            StashMessageModel messageModel = ((IStashMessageDatabaseModelConverter)stashMessage).ToStashMessageModel();

            using (StashMessagesContext db = new StashMessagesContext())
            {
                db.StashMessages.Add(messageModel);
                db.SaveChanges();
            }
        }
Beispiel #7
0
        public ICollection <IStashMessage> GetMessagesFromStash(long chatId)
        {
            List <IStashMessage> stashMessages = new List <IStashMessage>();

            if (!IsStashExist(chatId))
            {
                return(stashMessages);
            }

            using (StashMessagesContext db = new StashMessagesContext())
            {
                IQueryable <StashMessageModel> stashMessageModel = db.StashMessages
                                                                   .Where(message => message.ChatId == chatId);

                foreach (StashMessageModel messageModel in stashMessageModel)
                {
                    IStashMessage stashMessage = CreateStashMessage(null);
                    ((IStashMessageDatabaseModelConverter)stashMessage).FromStashMessageModel(messageModel);
                    stashMessages.Add(stashMessage);
                }
            }

            return(stashMessages);
        }