Example #1
0
        public async ValueTask <string> ExportToJson()
        {
            var expenses = await _expensesRepo.GetCollection();

            var categories = await _categoriesRepo.GetCollection();

            var groups = await _groupsRepo.GetCollection();

            var groupsDefaultCategories = await _groupDefaultCategoriesRepo.GetCollection();

            var incomes = await _incomesRepo.GetCollection();

            var limits = await _limitsRepo.GetCollection();

            var data = new ExpensesDataSerializable
            {
                Categories = categories,
                Expenses   = expenses,
                Groups     = groups,
                GroupsDefaultCategories = groupsDefaultCategories,
                Incomes = incomes,
                Limits  = limits
            };

            var json = ExpensesDataSerializable.SerializeToJson(data);

            return(json);
        }
Example #2
0
        public async ValueTask ImportFromJson(string data, bool dataMerge)
        {
            var expenses = dataMerge ? await _expensesRepo.GetCollection() : new List <Expense>();

            var categories = dataMerge ? await _categoriesRepo.GetCollection() : new List <Category>();

            var groups = dataMerge ? await _groupsRepo.GetCollection() : new List <Group>();

            var groupsDefaultCategories = dataMerge ? await _groupDefaultCategoriesRepo.GetCollection() : new List <GroupDefaultCategory>();

            var incomes = dataMerge ? await _incomesRepo.GetCollection() : new List <Income>();

            var limits = dataMerge ? await _limitsRepo.GetCollection() : new List <Limit>();

            if (!string.IsNullOrWhiteSpace(data))
            {
                var imported = ExpensesDataSerializable.DeserializeFromJson(data);

                if (dataMerge)
                {
                    foreach (var cat in imported.Categories)
                    {
                        if (!categories.Any(c => c.Id == cat.Id))
                        {
                            categories.Add(cat);
                        }
                    }

                    foreach (var gr in imported.Groups)
                    {
                        if (!groups.Any(g => g.Id == gr.Id))
                        {
                            groups.Add(gr);
                        }
                    }

                    foreach (var defCat in imported.GroupsDefaultCategories)
                    {
                        if (!groupsDefaultCategories.Contains(defCat) && groups.Any(g => g.Id == defCat.GroupId) && categories.Any(c => c.Id == defCat.CategoryId))
                        {
                            groupsDefaultCategories.Add(defCat);
                        }
                    }

                    foreach (var exp in imported.Expenses)
                    {
                        if (!expenses.Any(e => e.Id == exp.Id) && (categories.Any(c => c.Id == exp.CategoryId) && (exp.GroupId is null || groups.Any(g => g.Id == exp.GroupId))))
                        {
                            expenses.Add(exp);
                        }
                    }

                    foreach (var inc in imported.Incomes)
                    {
                        if (!incomes.Any(i => i.Id == inc.Id))
                        {
                            incomes.Add(inc);
                        }
                    }

                    foreach (var lim in imported.Limits)
                    {
                        if (!limits.Any(i => i.Id == lim.Id))
                        {
                            limits.Add(lim);
                        }
                    }
                }
                else
                {
                    expenses.AddRange(imported.Expenses);
                    categories.AddRange(imported.Categories);
                    groups.AddRange(imported.Groups);
                    groupsDefaultCategories.AddRange(imported.GroupsDefaultCategories);
                    incomes.AddRange(imported.Incomes);
                    limits.AddRange(imported.Limits);
                }
            }

            await ClearData();

            await _expensesRepo.SetCollection(expenses);

            await _categoriesRepo.SetCollection(categories);

            await _groupsRepo.SetCollection(groups);

            await _groupDefaultCategoriesRepo.SetCollection(groupsDefaultCategories);

            await _incomesRepo.SetCollection(incomes);

            await _limitsRepo.SetCollection(limits);
        }
 internal static string SerializeToJson(ExpensesDataSerializable data) => System.Text.Json.JsonSerializer.Serialize(data);