コード例 #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();
        }
コード例 #2
0
 public ActionResult AddOrUpdateRecipe([FromBody] IngredientViewModel ingredient)
 {
     _ingredientsRepository.InsertIngredient(_mapper.Map <Ingredient>(ingredient));
     _ingredientsRepository.Save();
     return(Ok(new { status = "Ok" }));
 }