Exemplo n.º 1
0
        public async Task <bool> InsertCategoriesFromFile()
        {
            var jsonCategories = await filesManager.ReadFile(CategoriesFilePath);

            var categories             = jsonCategories.FromJSON <IEnumerable <Category> >();
            var categoriesFromDatabase = await unitOfWork.CategoryRepository.GetAll();

            if (!categoriesFromDatabase.Any())
            {
                unitOfWork.CategoryRepository.AddRange(categories);
            }
            else
            {
                foreach (var categoryToInsert in categories)
                {
                    if (!categoriesFromDatabase.Any(c => c.Name.ToLower().Equals(categoryToInsert.Name.ToLower())))
                    {
                        unitOfWork.CategoryRepository.Add(categoryToInsert);
                    }
                }

                var categoriesToDelete = categoriesFromDatabase.Where(category => !categories.Any(c => c.Name.ToLower().Equals(category.Name.ToLower())));
                unitOfWork.CategoryRepository.DeleteRange(categoriesToDelete);
            }

            return(await unitOfWork.Complete());
        }
        public override async Task <StatsModel> Get(string key)
        {
            var statsModel = await base.Get(key);

            if (statsModel != null)
            {
                return(statsModel);
            }

            statsModel = (await filesManager.ReadFile("/data/stats.json"))?
                         .FromJSON <StatsModel>(JsonSettings.JsonSerializerOptions);

            this.Set(MemoryCacheKeys.STATS_KEY, statsModel, new MemoryCacheEntryOptions()
                     .SetSlidingExpiration(statsCacheSlidingExpiration));

            return(statsModel);
        }