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