public static void Save(this TelegramBotGroup u, TelegramBotDbContext db)
 {
     if (u.ID == null || !ExistsInDb(u, db))
     {
         //need to insert
         //db.ExecuteNonQuery(
         //    "insert into chatgroup (GroupId, Name, UserName, MemberCount) VALUES (@GroupId, @Name, @UserName, @MemberCount)",
         //    u);
         //u.ID =
         //    db.Connection.Query<int>(
         //        $"SELECT ID FROM chatgroup WHERE GroupId = @GroupId", u)
         //        .First();
         db.Groups.Add(u);
         db.SaveChanges();
         u.ID = db.Groups.FirstOrDefault(c => c.GroupId == u.GroupId).ID;
     }
     else
     {
         db.Groups.Update(u);
         db.SaveChanges();
         //db.ExecuteNonQuery(
         //    "UPDATE chatgroup SET GroupId = @GroupId, Name = @Name, UserName = @UserName, MemberCount = @MemberCount WHERE ID = @ID",
         //    u);
     }
 }
        public virtual void SeedDb(TelegramBotDbContext db, params int[] admins)
        {
            if (!db.Users.AsNoTracking().ToList().Any(c => c.UserId == LoadedSetting.TelegramDefaultAdminUserId))
            {
                using (var tx = db.Database.BeginTransaction())
                {
                    try
                    {
                        foreach (var u in admins)
                        {
                            db.Users.Add(new TelegramBotUser()
                            {
                                IsBotAdmin = true, UserId = u, FirstSeen = DateTime.UtcNow
                            });
                        }
                        db.Users.Add(new TelegramBotUser()
                        {
                            IsBotAdmin = true, UserId = LoadedSetting.TelegramDefaultAdminUserId, FirstSeen = DateTime.UtcNow
                        });


                        db.SaveChanges();
                        tx.Commit();
                    }
                    catch (Exception ex)
                    {
                        Log.Write($"Saving admin error: {ex}", LogLevel.Error, null, "error.log");
                    }
                }
            }
        }
 public static void Save(this TelegramBotSetting set, TelegramBotDbContext db)
 {
     if (set.ID == null || !ExistsInDb(set, db))
     {
         //need to insert
         //db.ExecuteNonQuery(
         //    "insert into settings (Alias, TelegramBotAPIKey, TelegramDefaultAdminUserId) VALUES (@Alias, @TelegramBotAPIKey, @TelegramDefaultAdminUserId)",
         //    set);
         //set.ID = db.Connection.Query<int>("SELECT ID FROM Settings WHERE Alias = @Alias", set).First();
         db.Settings.Add(set);
         db.SaveChanges();
         set.ID = db.Settings.FirstOrDefault(c => c.Alias == set.Alias).ID;
     }
     else
     {
         //db.ExecuteNonQuery(
         //    "UPDATE settings SET Alias = @Alias, TelegramBotAPIKey = @TelegramBotAPIKey, TelegramDefaultAdminUserId = @TelegramDefaultAdminUserId WHERE ID = @ID",
         //    set);
         db.Settings.Update(set);
         db.SaveChanges();
     }
 }
        public static void Save(this TelegramBotUser u, TelegramBotDbContext db)
        {
            if (u.ID == null || !ExistsInDb(u, db))
            {
                using (var tx = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Users.Add(u);
                        db.SaveChanges();
                        u.ID = db.Users.FirstOrDefault(c => c.UserId == u.UserId).ID;
                    }
                    catch { }
                }

                //db.Database.ExecuteSqlCommand(
                //    "insert into users (Name, UserId, UserName, FirstSeen, LastHeard, Points, Location, Debt, LastState, Greeting, Grounded, GroundedBy, IsBotAdmin, LinkingKey, Description) VALUES (@Name, @UserId, @UserName, @FirstSeen, @LastHeard, @Points, @Location, @Debt, @LastState, @Greeting, @Grounded, @GroundedBy, @IsBotAdmin, @LinkingKey, @Description)",
                //    u);
                //u.ID =
                //    db.Database.Query<int>(
                //        $"SELECT ID FROM Users WHERE UserId = @UserId", u)
                //        .First();
            }
            else
            {
                //using(var tx = db.Database.BeginTransaction())
                //{
                //    try
                //    {
                //        db.Users.Update(u);
                //        db.SaveChanges();
                //        tx.Commit();
                //    }
                //    catch { }
                //}

                //db.ExecuteNonQuery(
                //    "UPDATE users SET Name = @Name, UserId = @UserId, UserName = @UserName, FirstSeen = @FirstSeen, LastHeard = @LastHeard, Points = @Points, Location = @Location, Debt = @Debt, LastState = @LastState, Greeting = @Greeting, Grounded = @Grounded, GroundedBy = @GroundedBy, IsBotAdmin = @IsBotAdmin, LinkingKey = @LinkingKey, Description = @Description WHERE ID = @ID",
                //    u);
            }
        }
 public static void RemoveFromDb(this TelegramBotUser user, TelegramBotDbContext db)
 {
     db.Users.Remove(user);
     db.SaveChanges();
 }
 public static void RemoveFromDb(this TelegramBotSetting set, TelegramBotDbContext db)
 {
     db.Settings.Remove(set);
     db.SaveChanges();
 }
 public static void RemoveFromDb(this TelegramBotGroup group, TelegramBotDbContext db)
 {
     db.Groups.Remove(group);
     db.SaveChanges();
 }