Exemplo n.º 1
0
        public async Task InsertIngredientsIntoDB()
        {
            var ingredientDb = await _ingredientsRepository.CheckIfExistsItems();

            if (ingredientDb)
            {
                return;
            }
            var projectRootPath   = _hostingEnvironment.ContentRootPath;
            var fullFileDirectory = Path.Combine(projectRootPath, StaticDocumentsDirectories.JsonFiles);

            if (!Directory.Exists(fullFileDirectory))
            {
                throw new ApplicationException(Errors.DirectoryDoesNotExist);
            }
            var fullFileName = Path.Combine(fullFileDirectory, "ingredients-sample-data.json");

            if (!File.Exists(fullFileName))
            {
                throw new ApplicationException(Errors.FileDoesNotExist);
            }
            var jsonString = File.ReadAllText(fullFileName);
            var jsonModel  = JsonConvert.DeserializeObject <List <JsonIngredients> >(jsonString);
            var ingredient = new Ingredients();

            foreach (var item in jsonModel)
            {
                ingredient.Id    = item.Id;
                ingredient.Name  = item.Name;
                ingredient.Price = item.Price;
                await _ingredientsRepository.Create(ingredient);
            }
            await _ingredientsRepository.Save();
        }