示例#1
0
        public async static Task AddCategoryEntry(CategoryType type, int value, string apiKey)
        {
            using (var db = new UserContextFactory().CreateDbContext()) {
                var user = await GetUser(apiKey);

                if (user == null)
                {
                    return;
                }

                var latestEntry = await db.CategoryValue.Where(x => x.UserID == user.ID && x.Category == type).OrderByDescending(x => x.Date).FirstOrDefaultAsync();

                if (latestEntry == null)
                {
                    db.CategoryValue.Add(new CategoryValue {
                        Category = type, Value = value, Delta = 0, UserID = user.ID
                    });
                }
                else
                {
                    db.CategoryValue.Add(new CategoryValue {
                        Category = type, Value = value, Delta = value - latestEntry.Value, UserID = user.ID
                    });
                }

                await db.SaveChangesAsync();
            }
        }
示例#2
0
        public static async Task AddUser(string apiKey)
        {
            using (var db = new UserContextFactory().CreateDbContext()) {
                var user = await GetUser(apiKey);

                if (user == null)
                {
                    var account = await AccountAPI.Account(apiKey);

                    if (db.User.Any(x => x.AccountName.Equals(account.Name)))
                    {
                        var existingUser = db.User.FirstOrDefault(x => x.AccountName.Equals(account.Name));
                        db.Key.Add(new Key {
                            UserID = existingUser.ID, APIKey = apiKey
                        });
                    }
                    else
                    {
                        var addedUser = db.User.Add(new User {
                            AccountName = account.Name
                        });
                        await db.SaveChangesAsync();

                        db.Key.Add(new Key {
                            UserID = addedUser.Entity.ID, APIKey = apiKey
                        });
                    }
                    await db.SaveChangesAsync();
                }
            }
        }
示例#3
0
        public static async Task AddWalletEntries(List <API.Model.Account.WalletEntry> entries, string apiKey)
        {
            using (var db = new UserContextFactory().CreateDbContext()) {
                var user = await GetUser(apiKey);

                if (user == null)
                {
                    return;
                }

                foreach (var entry in entries)
                {
                    var latestEntry = await db.Wallet.Where(x => x.UserID == user.ID && x.CurrencyID == entry.ID).OrderByDescending(x => x.Date).FirstOrDefaultAsync();

                    if (latestEntry != null)
                    {
                        db.Wallet.Add(new Model.WalletEntry {
                            UserID = user.ID, CurrencyID = entry.ID, Value = entry.Value, Delta = entry.Value - latestEntry.Value
                        });
                    }
                    else
                    {
                        db.Wallet.Add(new Model.WalletEntry {
                            UserID = user.ID, CurrencyID = entry.ID, Value = entry.Value
                        });
                    }
                }
                await db.SaveChangesAsync();
            }
        }
示例#4
0
        /// <summary>
        /// 创建数据库
        /// </summary>
        private static void BuildDB()
        {
            try
            {
                var dbFactory = new UserContextFactory();

                var db = dbFactory.CreateDbContext();
                Console.WriteLine("取得数据库实例");
                //更新数据库
                if (db.Database.GetPendingMigrations().Any())
                {
                    db.Database.Migrate();
                    Console.WriteLine("迁移已完成");
                }
                else
                {
                    Console.WriteLine("没有需要迁移的项目");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("迁移失败");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
        public async Task CreationTest()
        {
            var userContext = UserContextFactory.CreateUserContext();
            var treeManager = userContext.TreeManagerFactory.CreateTreeManager();
            var tree        = await treeManager.CreateTree("123");

            ShowFolder(tree);
        }
示例#6
0
        public static async Task <List <Mini> > GetAccountMinis(string apiKey)
        {
            using (var db = new UserContextFactory().CreateDbContext()) {
                var user = await GetUser(apiKey);

                if (user != null)
                {
                    return(await db.Mini.Where(x => x.UserID == user.ID).ToListAsync());
                }
            }
            return(new List <Mini>());
        }
示例#7
0
        internal static async Task <User> GetUser(string apiKey)
        {
            using (var db = new UserContextFactory().CreateDbContext()) {
                if (db.Key.Any(x => x.APIKey.Equals(apiKey)))
                {
                    var key = await db.Key.FirstOrDefaultAsync(x => x.APIKey.Equals(apiKey));

                    return(await db.User.FirstOrDefaultAsync(x => x.ID == key.UserID));
                }
                else
                {
                    return(null);
                }
            }
        }
示例#8
0
        public static async Task AddMinis(List <API.Model.Miscellaneous.Mini> minis, string apiKey)
        {
            using (var db = new UserContextFactory().CreateDbContext()) {
                var user = await GetUser(apiKey);

                if (user == null)
                {
                    return;
                }
                foreach (var mini in minis)
                {
                    db.Mini.Add(new Mini {
                        MiniID = mini.ID, ItemID = mini.ItemID, UserID = user.ID
                    });
                }
                await db.SaveChangesAsync();
            }
        }
示例#9
0
        public static async Task AddDyes(List <API.Model.Items.Item> dyes, string apiKey)
        {
            using (var db = new UserContextFactory().CreateDbContext()) {
                var user = await GetUser(apiKey);

                if (user == null)
                {
                    return;
                }
                foreach (var dye in dyes)
                {
                    db.Dye.Add(new Dye {
                        DyeID = dye.ID, UserID = user.ID
                    });
                }
                await db.SaveChangesAsync();
            }
        }
示例#10
0
        public static async Task AddSkins(List <API.Model.Items.Skin> skins, string apiKey)
        {
            using (var db = new UserContextFactory().CreateDbContext()) {
                var user = await GetUser(apiKey);

                if (user == null)
                {
                    return;
                }
                foreach (var skin in skins)
                {
                    db.Skin.Add(new Skin {
                        SkinID = skin.ID, UserID = user.ID
                    });
                }
                await db.SaveChangesAsync();
            }
        }
示例#11
0
        public static async Task AddAccountItems(List <Item> items, string apiKey)
        {
            using (var db = new UserContextFactory().CreateDbContext()) {
                var user = await GetUser(apiKey);

                if (user == null)
                {
                    return;
                }

                var currentItems = await db.Item.Where(x => x.UserID == user.ID).ToListAsync();

                var newItems = GetDifference(items, currentItems);
                newItems.ForEach(x => x.UserID = user.ID);

                db.RemoveRange(currentItems);
                db.AddRange(newItems);

                await db.SaveChangesAsync();
            }
        }