private Backpack ExecUpdate(string aPlayerSteamId, ItemStack[] aToolbar, ItemStack[] aBag)
        {
            using (var DB = new BackpackContext())
            {
                var Backpack = DB.Backpacks
                               .OrderByDescending(B => B.Timestamp)
                               .FirstOrDefault(B => B.Id == aPlayerSteamId);
                var IsNewBackpack  = Backpack == null || (DateTime.Now - Backpack.Timestamp).TotalMinutes >= 1;
                var ToolbarContent = JsonConvert.SerializeObject(aToolbar);
                var BagContent     = JsonConvert.SerializeObject(aBag);

                if (Backpack != null)
                {
                    if (IsEqual(JsonConvert.DeserializeObject <ItemStack[]>(Backpack.ToolbarContent), aToolbar) &&
                        IsEqual(JsonConvert.DeserializeObject <ItemStack[]>(Backpack.BagContent), aBag))
                    {
                        return(null);
                    }
                }

                if (IsNewBackpack)
                {
                    Backpack = new Backpack()
                    {
                        Id             = aPlayerSteamId,
                        Timestamp      = DateTime.Now,
                        ToolbarContent = ToolbarContent,
                        BagContent     = BagContent
                    };
                    DB.Backpacks.Add(Backpack);
                }

                DB.SaveChanges();

                Backpack      = DB.Backpacks.FirstOrDefault(B => B.Id == aPlayerSteamId && B.Timestamp == DateTime.MinValue);
                IsNewBackpack = Backpack == null;
                if (IsNewBackpack)
                {
                    Backpack = new Backpack()
                    {
                        Id        = aPlayerSteamId,
                        Timestamp = DateTime.MinValue,
                    }
                }
                ;

                Backpack.ToolbarContent = ToolbarContent;
                Backpack.BagContent     = BagContent;
                if (IsNewBackpack)
                {
                    DB.Backpacks.Add(Backpack);
                }

                var count = DB.SaveChanges();

                return(Backpack);
            }
        }
 public void CreateAndUpdateDatabase()
 {
     using (var DB = new BackpackContext())
     {
         DB.Database.Migrate();
         DB.Database.EnsureCreated();
         DB.Database.ExecuteSqlCommand("PRAGMA journal_mode=WAL;");
     }
 }
 public void DeleteOldBackpacks(int aDays)
 {
     using (var DB = new BackpackContext())
     {
         DB.Backpacks
         .Where(B => B.Timestamp != DateTime.MinValue && (DateTime.Now - B.Timestamp).TotalDays > aDays)
         .ToList()
         .ForEach(B => DB.Backpacks.Remove(B));
         DB.SaveChanges();
         DB.Database.ExecuteSqlCommand("VACUUM;");
     }
 }
 public BackpacksController(BackpackContext context)
 {
     _db             = context;
     BackpackManager = Program.GetManager <BackpackManager>();
 }