public void RemoveReminder(long chatId, long reminderId)
        {
            this.chatsLock.EnterWriteLock();
            try
            {
                Chat chat;
                if (this.chats.TryGetValue(chatId, out chat))
                {
                    Reminder reminder = chat.Reminders.FirstOrDefault(r => r.ReminderId == reminderId);
                    if (reminder != null)
                    {
                        chat.Reminders.Remove(reminder);
                        using (RemindersDbContext context = new RemindersDbContext())
                        {
                            context.Reminders.Remove(reminder);
                            context.SaveChanges();
                        }
                    }

                    this.ReminderRemoved(reminder);
                }
            }
            finally
            {
                this.chatsLock.ExitWriteLock();
            }
        }
        public void StoreReminder(Reminder reminder)
        {
            this.chatsLock.EnterWriteLock();
            try
            {
                using (RemindersDbContext context = new RemindersDbContext())
                {
                    context.Reminders.Add(reminder);
                    context.SaveChanges();
                }

                Chat chat;
                if (this.chats.TryGetValue(reminder.ChatId, out chat))
                {
                    chat.Reminders.Add(reminder);
                    reminder.Chat = chat;
                }

                this.ReminderAdded(reminder);
            }
            finally
            {
                this.chatsLock.ExitWriteLock();
            }
        }
 public void SetTimeZone(long chatId, TimeSpan timeZone)
 {
     this.chatsLock.EnterUpgradeableReadLock();
     try
     {
         Chat chat;
         if (this.chats.TryGetValue(chatId, out chat))
         {
             if (chat.TimeZone != timeZone)
             {
                 this.chatsLock.EnterWriteLock();
                 try
                 {
                     TimeSpan oldTimeZone = chat.TimeZone;
                     chat.TimeZone = timeZone;
                     using (RemindersDbContext context = new RemindersDbContext())
                     {
                         Chat dbchat = context.Chats.Where((c) => c.ChatId == chatId).FirstOrDefault();
                         dbchat.TimeZone = timeZone;
                         context.SaveChanges();
                     }
                     this.TimeZoneChanged(oldTimeZone, chat);
                 }
                 finally
                 {
                     this.chatsLock.ExitWriteLock();
                 }
             }
         }
         else
         {
             this.chatsLock.EnterWriteLock();
             try
             {
                 chat = new Chat {
                     ChatId = chatId, TimeZone = timeZone, Reminders = new List <Reminder>()
                 };
                 this.chats[chatId] = chat;
                 using (RemindersDbContext context = new RemindersDbContext())
                 {
                     context.Chats.Add(chat);
                     context.SaveChanges();
                 }
             }
             finally
             {
                 this.chatsLock.ExitWriteLock();
             }
         }
     }
     finally
     {
         this.chatsLock.ExitUpgradeableReadLock();
     }
 }
        public ReminderDbRepository()
        {
            using (RemindersDbContext context = new RemindersDbContext())
            {
                context.Database.EnsureCreated();

                this.chatsLock.EnterWriteLock();
                try
                {
                    foreach (Chat chat in context.Chats.Include(chat => chat.Reminders))
                    {
                        this.chats.Add(chat.ChatId, chat);
                    }
                }
                finally
                {
                    this.chatsLock.ExitWriteLock();
                }
            }
        }