bool HasAvailableIngredientsForRecipe(GameObject ingredientObj, IngredientDetail ingredient) { bool hasResoucres = true; int ingredientsAmount = 0; currentIngredients.Add(ingredientObj); RecipeWindowIngredient recipeIngredient = ingredientObj.GetComponent <RecipeWindowIngredient>(); foreach (ItemData itemData in ownerItems) { Debug.Log("inventory id: " + itemData.CollectionID + " , ingredient id: " + ingredient.ingredientID); if (itemData.CollectionID == ingredient.ingredientID) { Debug.Log("Checking for ingredient : " + ingredient.name + ", Inventory amount: " + itemData.stackSize + " Required amount : " + ingredient.amount); if (itemData.stackSize < ingredient.amount) { hasResoucres = false; } ingredientsAmount = itemData.stackSize; } } recipeIngredient.LoadIngredient(ingredient, ingredientsAmount); if (ingredientsAmount <= 0) { hasResoucres = false; } return(hasResoucres); }
public List <RecipeInfo> ConvertToListRecipeInfo(string dataString) { List <RecipeInfo> recipeList = new List <RecipeInfo>(); JToken token = JToken.Parse(dataString); JArray recipesArray = JArray.Parse(token.ToString()); Debug.Log("Recipes count:" + recipesArray.Count); for (int i = 0; i < recipesArray.Count; i++) { RecipeInfo recipeInfo = new RecipeInfo(); JToken recipeToken = recipesArray[i]; recipeInfo.recipeID = int.Parse(recipeToken["ID"].ToString()); recipeInfo.name = recipeToken["Name"].ToString(); recipeInfo.energy = int.Parse(recipeToken["Energy"].ToString()); recipeInfo.description = recipeToken["Description"].ToString(); recipeInfo.imgURL = recipeToken["Image"].ToString(); JArray ingredientsArray = (JArray)recipeToken["Recipe"]; recipeInfo.IngredientDetails = new List <IngredientDetail>(); for (int j = 0; j < ingredientsArray.Count; j++) { IngredientDetail ingredientDetail = new IngredientDetail(); JToken ingredientToken = ingredientsArray[j]; ingredientDetail.ingredientID = int.Parse(ingredientToken["ID"].ToString()); ingredientDetail.amount = int.Parse(ingredientToken["Amount"].ToString()); ingredientDetail.energy = int.Parse(ingredientToken["Energy"].ToString()); ingredientDetail.imgURL = ingredientToken["Image"].ToString(); ingredientDetail.name = ingredientToken["Name"].ToString(); recipeInfo.IngredientDetails.Add(ingredientDetail); } JArray detailsArray = JArray.Parse(recipeToken["Detail"].ToString()); recipeInfo.RecipeDetails = new List <ItemDetail>(); for (int k = 0; k < detailsArray.Count; k++) { JToken detailToken = detailsArray[k]; ItemDetail itemDetail = new ItemDetail(); itemDetail.name = detailToken["Name"].ToString(); itemDetail.value = float.Parse(detailToken["Value"].ToString()); recipeInfo.RecipeDetails.Add(itemDetail); } recipeList.Add(recipeInfo); } return(recipeList); }
public IngredientDetail GetIngredientbyId(int id) { using (var context = new CookbookContext()) { var entity = context .Ingredients .Single(e => e.ID == id && e.AuthorID == userId); var ingredientDetail = new IngredientDetail { ID = entity.ID, Name = entity.Name, Description = entity.Description, DateCreated = entity.DateCreated, DateModified = entity.DateModified, Amount = entity.Amount, Unit = entity.Unit, TemplateId = entity.TemplateId, }; var TemplateName = context.Larders .SingleOrDefault(l => l.AuthorID == userId && l.ID == entity.TemplateId); if (TemplateName != null) { ingredientDetail.TemplateName = TemplateName.Name.ToString(); } return(ingredientDetail); } }
public void AddIngredientToRecipe(Recipe recipe, string ingredientName, string denomination, double amount) { var ingred = WorkingUnit.Ingredients.GetAll().SingleOrDefault(x => string.Equals(x.Name, ingredientName, StringComparison.OrdinalIgnoreCase) && string.Equals(x.Denomination, denomination, StringComparison.OrdinalIgnoreCase)); if (ingred == null) { ingred = new Ingredient { Id = Guid.NewGuid().ToString(), Name = ingredientName, Denomination = denomination }; WorkingUnit.Ingredients.Add(ingred); } var ingDetail = new IngredientDetail() { Ingredient = ingred, Amount = amount }; var checkerDetail = recipe.Ingredients.SingleOrDefault(x => x.Ingredient == ingDetail.Ingredient); if (checkerDetail != null) { checkerDetail.Amount += ingDetail.Amount; recipe.IngIdAndAmount[checkerDetail.Ingredient.Id] += amount; } else { recipe.Ingredients.Add(ingDetail); recipe.IngIdAndAmount.Add(ingDetail.Ingredient.Id, ingDetail.Amount); } WorkingUnit.Save(); }
//Detail public GlazeDetail GetGlazeById(int id) { using (var ctx = new ApplicationDbContext()) { var ingList = new List <IngredientDetail>(); var entity = ctx .Glaze .Single(e => e.GlazeID == id && e.OwnerId == _userId); foreach (Ingredient ingredient in entity.ListOfIngredients) { var ing = new IngredientDetail(); ing.MaterialName = ingredient.Material.MaterialName; ing.Quantity = ingredient.Quantity; ingList.Add(ing); } return (new GlazeDetail { GlazeID = entity.GlazeID, GlazeName = entity.GlazeName, Description = entity.Description, MinCone = entity.MinCone, MaxCone = entity.MaxCone, MainColor = entity.MainColor, Atmosphere = entity.Atmosphere, Opacity = entity.Opacity, Surface = entity.Surface, FoodSafe = entity.FoodSafe, CreatedDate = entity.CreatedDate, ModifiedDate = entity.ModifiedDate, IngredientList = ingList, }); } }
public async Task DeleteIngredientDetailByIdsAsync_Should_Delete_IngredientDetail() { //Arrange List <IngredientDetail> mockIngredientDetailDB = GetDetails(); IngredientDetail toDelete = new IngredientDetail { RecipeId = 1, IngredientId = 2 }; _mockRepository.Setup(r => r.FirstOrDefaultAsync <IngredientDetail>(It.IsAny <Expression <Func <IngredientDetail, bool> > >())) .ReturnsAsync(() => mockIngredientDetailDB.FirstOrDefault(x => x.RecipeId == toDelete.RecipeId && x.IngredientId == toDelete.IngredientId)); _mockRepository.Setup(r => r.DeleteAsync <IngredientDetail>(It.Is <IngredientDetail>(entity => entity.RecipeId == toDelete.RecipeId && entity.IngredientId == toDelete.IngredientId))) .Callback((IngredientDetail ingredientDetail) => { mockIngredientDetailDB.Remove(ingredientDetail); }); //Act await _ingredientDetailController.DeleteIngredientDetailByIdsAsync(toDelete.RecipeId, toDelete.IngredientId); //Assert var mustBeNull = mockIngredientDetailDB.FirstOrDefault(x => x.RecipeId == toDelete.RecipeId && x.IngredientId == toDelete.IngredientId); _mockRepository.Verify(r => r.FirstOrDefaultAsync <IngredientDetail>(It.IsAny <Expression <Func <IngredientDetail, bool> > >()), Times.Once); _mockRepository.Verify(r => r.DeleteAsync <IngredientDetail>(It.Is <IngredientDetail>(entity => entity.RecipeId == toDelete.RecipeId && entity.IngredientId == toDelete.IngredientId)), Times.Once); Assert.Null(mustBeNull); }
public List <RecipeInfo> ConvertToListRecipeInfo(string dataString) { string parsedString = ParseString(dataString); List <RecipeInfo> recipeList = new List <RecipeInfo>(); JsonData recipesArray = LitJson.JsonMapper.ToObject(parsedString); for (int i = 0; i < recipesArray.Count; i++) { RecipeInfo recipeInfo = new RecipeInfo(); JsonData recipeToken = recipesArray[i]; recipeInfo.recipeID = int.Parse(recipeToken["ID"].ToString()); recipeInfo.name = recipeToken["Name"].ToString(); recipeInfo.energy = int.Parse(recipeToken["Energy"].ToString()); recipeInfo.description = recipeToken["Description"].ToString(); recipeInfo.imgURL = recipeToken["Image"].ToString(); JsonData ingredientsArray = recipeToken["Recipe"]; recipeInfo.IngredientDetails = new List <IngredientDetail>(); for (int j = 0; j < ingredientsArray.Count; j++) { IngredientDetail ingredientDetail = new IngredientDetail(); JsonData ingredientToken = ingredientsArray[j]; ingredientDetail.ingredientID = int.Parse(ingredientToken["ID"].ToString()); ingredientDetail.amount = int.Parse(ingredientToken["Amount"].ToString()); ingredientDetail.energy = int.Parse(ingredientToken["Energy"].ToString()); ingredientDetail.imgURL = ingredientToken["Image"].ToString(); ingredientDetail.name = ingredientToken["Name"].ToString(); recipeInfo.IngredientDetails.Add(ingredientDetail); } JsonData detailsArray = LitJson.JsonMapper.ToObject(recipeToken["Detail"].ToString()); recipeInfo.RecipeDetails = new List <ItemDetail>(); for (int k = 0; k < detailsArray.Count; k++) { JsonData detailToken = detailsArray[k]; ItemDetail itemDetail = new ItemDetail(); itemDetail.name = detailToken["Name"].ToString(); itemDetail.value = float.Parse(detailToken["Value"].ToString()); recipeInfo.RecipeDetails.Add(itemDetail); } recipeList.Add(recipeInfo); } return(recipeList); }
public async Task AddIngredientToRecipeAsync_Creates_New_DataBase_Entry_On_Each_Step() { //Arrange Recipe recipeToTest = new Recipe { Id = 1, Name = "Recipe 1", CategoryId = 1, Ingredients = new List <IngredientDetail>() }; string ingredientName = "Salmon"; // id = 6 double amount = 6; string measure = "fille"; //id = 5 IngredientDetail detail = new IngredientDetail { RecipeId = recipeToTest.Id, IngredientId = 6, MeasureId = 5 }; List <Recipe> mockRecipeDB = GetRecipes(); List <Measure> mockMeasureDB = GetMeasures(); List <Ingredient> mockIngredientDB = GetIngredients(); List <IngredientDetail> mockIDDB = GetIngredientDetails(); _mockRepository.Setup(r => r.AddAsync <Measure>(It.Is <Measure>(entity => entity.Name == measure))) .Callback((Measure measure) => { measure.Id = mockMeasureDB.Last().Id + 1; mockMeasureDB.Add(measure); }); _mockRepository.Setup(r => r.AddAsync <Ingredient>(It.Is <Ingredient>(entity => entity.Name == ingredientName))) .Callback((Ingredient ingredient) => { ingredient.Id = mockIngredientDB.Last().Id + 1; mockIngredientDB.Add(ingredient); }); _mockRepository.Setup(r => r.UpdateAsync <Recipe>(It.Is <Recipe>(entity => entity.Id == recipeToTest.Id && entity.Name == recipeToTest.Name && entity.CategoryId == recipeToTest.CategoryId))) .Callback(() => { detail.Id = mockIDDB.Last().Id + 1; mockIDDB.Add(detail); }); _mockRepository.Setup(r => r.FirstOrDefaultAsync <Recipe>(It.IsAny <Expression <Func <Recipe, bool> > >())) .ReturnsAsync(() => mockRecipeDB.SingleOrDefault(x => string.Equals(x.Name, recipeToTest.Name, StringComparison.OrdinalIgnoreCase) && x.CategoryId == recipeToTest.CategoryId && x.Id == recipeToTest.Id)); _mockRepository.Setup(r => r.FirstOrDefaultAsync <Ingredient>(It.IsAny <Expression <Func <Ingredient, bool> > >())) .ReturnsAsync(() => mockIngredientDB.SingleOrDefault(x => string.Equals(x.Name, ingredientName, StringComparison.OrdinalIgnoreCase))); _mockRepository.Setup(r => r.FirstOrDefaultAsync <Measure>(It.IsAny <Expression <Func <Measure, bool> > >())) .ReturnsAsync(() => mockMeasureDB.SingleOrDefault(x => string.Equals(x.Name, measure, StringComparison.OrdinalIgnoreCase))); _mockRepository.Setup(r => r.FirstOrDefaultAsync <IngredientDetail>(It.IsAny <Expression <Func <IngredientDetail, bool> > >())) .ReturnsAsync(() => mockIDDB.SingleOrDefault(x => x.RecipeId == detail.RecipeId && x.IngredientId == detail.IngredientId)); //Act await _recipeController.AddIngredientToRecipeAsync(recipeToTest, ingredientName, measure, amount); //Assert IngredientDetail retrieved = mockIDDB.SingleOrDefault(x => x.RecipeId == recipeToTest.Id && x.IngredientId == mockIngredientDB.SingleOrDefault(x => x.Name == ingredientName)?.Id); _mockRepository.Verify(r => r.FirstOrDefaultAsync <Recipe>(It.IsAny <Expression <Func <Recipe, bool> > >()), Times.Once); _mockRepository.Verify(r => r.FirstOrDefaultAsync <Measure>(It.IsAny <Expression <Func <Measure, bool> > >()), Times.Once); _mockRepository.Verify(r => r.FirstOrDefaultAsync <Ingredient>(It.IsAny <Expression <Func <Ingredient, bool> > >()), Times.Once); _mockRepository.Verify(r => r.FirstOrDefaultAsync <IngredientDetail>(It.IsAny <Expression <Func <IngredientDetail, bool> > >()), Times.Once); _mockRepository.Verify(r => r.AddAsync <Measure>(It.Is <Measure>(entity => entity.Name == measure)), Times.Once); _mockRepository.Verify(r => r.AddAsync <Ingredient>(It.Is <Ingredient>(entity => entity.Name == ingredientName)), Times.Once); _mockRepository.Verify(r => r.UpdateAsync <Recipe>(It.Is <Recipe>(entity => entity.Id == recipeToTest.Id && entity.Name == recipeToTest.Name && entity.CategoryId == recipeToTest.CategoryId)), Times.Once); Assert.NotNull(retrieved); }
public async Task DeleteIngredientDetailByIdsAsync(int recipeId, int ingredientId) { IngredientDetail retrieved = await UnitOfWork.Repository.FirstOrDefaultAsync <IngredientDetail>(x => x.RecipeId == recipeId && x.IngredientId == ingredientId); if (retrieved == null) { throw new EntryNotFoundException("This IngredientDetail doesn't exist in database."); } await UnitOfWork.Repository.DeleteAsync <IngredientDetail>(retrieved); }
//ingredientId == -1: create new ingredient public void showDetailIngredientWindow( IngredientTab ingredientTab, int ingredientId = Constant.ID_CREATE_NEW) { IngredientDetail detail = new IngredientDetail( ingredientTab, ingredientId); detail.ShowDialog(); }
public void LoadIngredient(IngredientDetail ingredientDetail, int containerAmountOfIngredient) { ingredientAmount.text = containerAmountOfIngredient + " of " + ingredientDetail.amount.ToString(); if (containerAmountOfIngredient < ingredientDetail.amount) { ingredientAmount.color = Color.red; } CloudGoods.GetItemTexture(ingredientDetail.imgURL, OnReceivedIngredientImage); }
public async Task AddIngredientToRecipeAsync(Recipe recipe, string ingredientName, double amount, int measureId) { if (recipe == null) { throw new ArgumentNullException("Recipe reference is null."); } if (ingredientName.IsNullOrEmpty()) { throw new EmptyFieldException("IngredientName is empty."); } var checkRecipe = (await UnitOfWork.Repository.FirstOrDefaultAsync <Recipe>(x => x.Name.ToLower() == recipe.Name.ToLower() && x.CategoryId == recipe.CategoryId && x.Id == recipe.Id)); if (checkRecipe == null) { throw new EntryNotFoundException($"Recipe {recipe.Name} : {recipe.Id} doesn't exist in Database."); } var ingred = (await UnitOfWork.Repository.FirstOrDefaultAsync <Ingredient>(x => x.Name.ToLower() == ingredientName.ToLower())); if (ingred == null) { ingred = new Ingredient { Name = ingredientName }; await UnitOfWork.Repository.AddAsync <Ingredient>(ingred); } var ingDetail = new IngredientDetail() { RecipeId = checkRecipe.Id, IngredientId = ingred.Id, Amount = amount, MeasureId = measureId }; var checkerDetail = (await UnitOfWork.Repository.FirstOrDefaultAsync <IngredientDetail>(x => x.RecipeId == ingDetail.RecipeId && x.IngredientId == ingDetail.IngredientId)); if (checkerDetail != null) { checkerDetail.Amount += ingDetail.Amount; await UnitOfWork.Repository.UpdateAsync <IngredientDetail>(checkerDetail); } else { checkRecipe.Ingredients.Add(ingDetail); await UnitOfWork.Repository.UpdateAsync <Recipe>(checkRecipe); } }
public async Task DeleteIngredientDetailByIdsAsync_Throws_EntryNotFoundException() { //Arrange List <IngredientDetail> mockIngredientDetailDB = GetDetails(); IngredientDetail toDelete = new IngredientDetail { RecipeId = 1, IngredientId = 4 }; // this doesn't exist _mockRepository.Setup(r => r.FirstOrDefaultAsync <IngredientDetail>(It.IsAny <Expression <Func <IngredientDetail, bool> > >())) .ReturnsAsync(() => mockIngredientDetailDB.FirstOrDefault(x => x.RecipeId == toDelete.RecipeId && x.IngredientId == toDelete.IngredientId)); //Act var caughtException = await Assert.ThrowsAsync <EntryNotFoundException>(async() => await _ingredientDetailController.DeleteIngredientDetailByIdsAsync(toDelete.RecipeId, toDelete.IngredientId)); //Assert _mockRepository.Verify(r => r.FirstOrDefaultAsync <IngredientDetail>(It.IsAny <Expression <Func <IngredientDetail, bool> > >()), Times.Once); Assert.Contains("IngredientDetail doesn't exist in database", caughtException.Message); }
public IngredientDetail GetIngredientByID(int id) { var ingredientEntity = _context.Ingredients.Find(id); if (ingredientEntity == null) { return(null); } var detail = new IngredientDetail { IngredientID = ingredientEntity.IngredientID, Name = ingredientEntity.Name, Type = ingredientEntity.Type, Price = ingredientEntity.Price }; return(detail); }
public async Task AddIngredientToRecipeAsync_Updates_IngredientDetail_Amount() { //Arrange Recipe recipeToTest = new Recipe { Id = 1, Name = "Recipe 1", CategoryId = 1 }; string ingredientName = "Sugar"; //id = 2 double amount = 200; string measure = "g"; // id = 3 IngredientDetail detail = new IngredientDetail { RecipeId = recipeToTest.Id, IngredientId = 2, MeasureId = 3 }; List <Recipe> mockRecipeDB = GetRecipes(); List <Measure> mockMeasureDB = GetMeasures(); List <Ingredient> mockIngredientDB = GetIngredients(); List <IngredientDetail> mockIDDB = GetIngredientDetails(); _mockRepository.Setup(r => r.UpdateAsync <IngredientDetail>(It.Is <IngredientDetail>(entity => entity.RecipeId == recipeToTest.Id && entity.IngredientId == 2 && entity.MeasureId == 3))) .Callback((IngredientDetail ingredientDetail) => { mockIDDB.SingleOrDefault(x => x.RecipeId == ingredientDetail.RecipeId && x.IngredientId == ingredientDetail.IngredientId).Amount += ingredientDetail.Amount; }); _mockRepository.Setup(r => r.FirstOrDefaultAsync <Recipe>(It.IsAny <Expression <Func <Recipe, bool> > >())) .ReturnsAsync(() => mockRecipeDB.SingleOrDefault(x => string.Equals(x.Name, recipeToTest.Name, StringComparison.OrdinalIgnoreCase) && x.CategoryId == recipeToTest.CategoryId && x.Id == recipeToTest.Id)); _mockRepository.Setup(r => r.FirstOrDefaultAsync <Ingredient>(It.IsAny <Expression <Func <Ingredient, bool> > >())) .ReturnsAsync(() => mockIngredientDB.SingleOrDefault(x => string.Equals(x.Name, ingredientName, StringComparison.OrdinalIgnoreCase))); _mockRepository.Setup(r => r.FirstOrDefaultAsync <Measure>(It.IsAny <Expression <Func <Measure, bool> > >())) .ReturnsAsync(() => mockMeasureDB.SingleOrDefault(x => string.Equals(x.Name, measure, StringComparison.OrdinalIgnoreCase))); _mockRepository.Setup(r => r.FirstOrDefaultAsync <IngredientDetail>(It.IsAny <Expression <Func <IngredientDetail, bool> > >())) .ReturnsAsync(() => mockIDDB.SingleOrDefault(x => x.RecipeId == detail.RecipeId && x.IngredientId == detail.IngredientId)); //Act await _recipeController.AddIngredientToRecipeAsync(recipeToTest, ingredientName, measure, amount); IngredientDetail retrieved = mockIDDB.SingleOrDefault(x => x.RecipeId == recipeToTest.Id && x.IngredientId == mockIngredientDB.SingleOrDefault(x => x.Name == ingredientName)?.Id); //Assert _mockRepository.Verify(r => r.FirstOrDefaultAsync <Recipe>(It.IsAny <Expression <Func <Recipe, bool> > >()), Times.Once); _mockRepository.Verify(r => r.FirstOrDefaultAsync <Measure>(It.IsAny <Expression <Func <Measure, bool> > >()), Times.Once); _mockRepository.Verify(r => r.FirstOrDefaultAsync <Ingredient>(It.IsAny <Expression <Func <Ingredient, bool> > >()), Times.Once); _mockRepository.Verify(r => r.FirstOrDefaultAsync <IngredientDetail>(It.IsAny <Expression <Func <IngredientDetail, bool> > >()), Times.Once); _mockRepository.Verify(r => r.UpdateAsync <IngredientDetail>(It.Is <IngredientDetail>(entity => entity.RecipeId == recipeToTest.Id && entity.IngredientId == 2 && entity.MeasureId == 3)), Times.Once); Assert.NotNull(retrieved); }
public void LoadIngredient(IngredientDetail ingredientDetail) { ingredientAmount.text = ingredientDetail.amount.ToString(); CloudGoods.GetItemTexture(ingredientDetail.imgURL, OnReceivedIngredientImage); }
public List<RecipeInfo> ConvertToListRecipeInfo(string dataString) { List<RecipeInfo> recipeList = new List<RecipeInfo>(); JToken token = JToken.Parse(dataString); JArray recipesArray = JArray.Parse(token.ToString()); Debug.Log("Recipes count:" + recipesArray.Count); for (int i = 0; i < recipesArray.Count; i++) { RecipeInfo recipeInfo = new RecipeInfo(); JToken recipeToken = recipesArray[i]; recipeInfo.recipeID = int.Parse(recipeToken["ID"].ToString()); recipeInfo.name = recipeToken["Name"].ToString(); recipeInfo.energy = int.Parse(recipeToken["Energy"].ToString()); recipeInfo.description = recipeToken["Description"].ToString(); recipeInfo.imgURL = recipeToken["Image"].ToString(); JArray ingredientsArray = (JArray)recipeToken["Recipe"]; recipeInfo.IngredientDetails = new List<IngredientDetail>(); for (int j = 0; j < ingredientsArray.Count; j++) { IngredientDetail ingredientDetail = new IngredientDetail(); JToken ingredientToken = ingredientsArray[j]; ingredientDetail.ingredientID = int.Parse(ingredientToken["ID"].ToString()); ingredientDetail.amount = int.Parse(ingredientToken["Amount"].ToString()); ingredientDetail.energy = int.Parse(ingredientToken["Energy"].ToString()); ingredientDetail.imgURL = ingredientToken["Image"].ToString(); ingredientDetail.name = ingredientToken["Name"].ToString(); recipeInfo.IngredientDetails.Add(ingredientDetail); } JArray detailsArray = JArray.Parse(recipeToken["Detail"].ToString()); recipeInfo.RecipeDetails = new List<ItemDetail>(); for (int k = 0; k < detailsArray.Count; k++) { JToken detailToken = detailsArray[k]; ItemDetail itemDetail = new ItemDetail(); itemDetail.name = detailToken["Name"].ToString(); itemDetail.value = float.Parse(detailToken["Value"].ToString()); recipeInfo.RecipeDetails.Add(itemDetail); } recipeList.Add(recipeInfo); } return recipeList; }
public List<RecipeInfo> ConvertToListRecipeInfo(string dataString) { string parsedString = ParseString(dataString); List<RecipeInfo> recipeList = new List<RecipeInfo>(); JsonData recipesArray = LitJson.JsonMapper.ToObject(parsedString); for (int i = 0; i < recipesArray.Count; i++) { RecipeInfo recipeInfo = new RecipeInfo(); JsonData recipeToken = recipesArray[i]; recipeInfo.recipeID = int.Parse(recipeToken["ID"].ToString()); recipeInfo.name = recipeToken["Name"].ToString(); recipeInfo.energy = int.Parse(recipeToken["Energy"].ToString()); recipeInfo.description = recipeToken["Description"].ToString(); recipeInfo.imgURL = recipeToken["Image"].ToString(); JsonData ingredientsArray = recipeToken["Recipe"]; recipeInfo.IngredientDetails = new List<IngredientDetail>(); for (int j = 0; j < ingredientsArray.Count; j++) { IngredientDetail ingredientDetail = new IngredientDetail(); JsonData ingredientToken = ingredientsArray[j]; ingredientDetail.ingredientID = int.Parse(ingredientToken["ID"].ToString()); ingredientDetail.amount = int.Parse(ingredientToken["Amount"].ToString()); ingredientDetail.energy = int.Parse(ingredientToken["Energy"].ToString()); ingredientDetail.imgURL = ingredientToken["Image"].ToString(); ingredientDetail.name = ingredientToken["Name"].ToString(); recipeInfo.IngredientDetails.Add(ingredientDetail); } JsonData detailsArray = LitJson.JsonMapper.ToObject(recipeToken["Detail"].ToString()); recipeInfo.RecipeDetails = new List<ItemDetail>(); for (int k = 0; k < detailsArray.Count; k++) { JsonData detailToken = detailsArray[k]; ItemDetail itemDetail = new ItemDetail(); itemDetail.name = detailToken["Name"].ToString(); itemDetail.value = float.Parse(detailToken["Value"].ToString()); recipeInfo.RecipeDetails.Add(itemDetail); } recipeList.Add(recipeInfo); } return recipeList; }
public ViewResult DeleteRecipe(string id) { Console.WriteLine("Delete Recipe ....... ID : [" + id + "]"); // Before this controller goes to RecipeLIst view, rlv model can toss to the view. rlv.Recipes = iRecipeRepo.Recipes; rlv.Categories = iCategoryRepo.Categories; rlv.ModalDetails = iModalDetailRepo.ModalDetails; rlv.RecipeModals = iRecipeModalRepo.RecipeModals; rlv.RecipeIngredients = iRecipeIngredientRepo.RecipeIngredients; rlv.IngredientDetails = iIngredientDetailRepo.IngredientDetails; rlv.RecipeReviewComments = iRecipeReviewCommentRepo.RecipeReviewComments; rlv.ReviewCommentDetails = iReviewCommentDetailRepo.ReviewCommentDetails; //Before the Neuavenue system is removed recipe, from bridge table //The system will has to indicate which ReviewComment belong to the recipe Id. // In order to remove IngredientStrings in IngredientDetail which belongs to IngredientId int recipeId = Convert.ToInt32(id); List <RecipeIngredient> recipeIngredients = rlv.RecipeIngredients.Where(ri => ri.RecipeId == recipeId).ToList <RecipeIngredient>(); foreach (RecipeIngredient recipeIngredient in recipeIngredients) { iRecipeIngredientRepo.DeleteRecipeIngredient(recipeIngredient); IngredientDetail ingredientDetail = rlv.IngredientDetails.First <IngredientDetail>( ind => ind.IngredientId == recipeIngredient.IngredientId); iIngredientDetailRepo.DeleteIngredientDetail(ingredientDetail); } //remove the data in RecipeModal which are the data (1, 2, 3 by recipeId) which has to be deleted. List <RecipeModal> recipeModals = rlv.RecipeModals.Where(rm => rm.RecipeId == recipeId).ToList <RecipeModal>(); foreach (RecipeModal recipeModal in recipeModals) { Console.WriteLine("ModalId in recipeModal: " + recipeModal.ModalId); Console.WriteLine("RecipeId in recipeModal: " + recipeModal.RecipeId); iRecipeModalRepo.DeleteRecipeModal(recipeModal); } //Remove the data in RecipeReviewComment and ReviewCommentDetail by recipeId which has to be deleted. List <RecipeReviewComment> recipeReviewComments = rlv.RecipeReviewComments.Where( rrc => rrc.RecipeId == recipeId).ToList <RecipeReviewComment>(); foreach (RecipeReviewComment recipeReviewComment in recipeReviewComments) { iRecipeReviewCommentRepo.DeleteRecipeReviewComment(recipeReviewComment); ReviewCommentDetail reviewCommentDetail = rlv.ReviewCommentDetails.First <ReviewCommentDetail>( rcd => rcd.ReviewCommentId == recipeReviewComment.ReviewCommentId); iReviewCommentDetailRepo.DeleteReviewCommentDetail(reviewCommentDetail); } //Remove Recipe by its RecipeId. Recipe DeleteRecipe = iRecipeRepo.Recipes.First <Recipe>(r => r.RecipeId.ToString() == id); iRecipeRepo.DeleteRecipe(DeleteRecipe); return(View("RecipeList", rlv)); }
//public ViewResult Insert(RecipeModel recipeModel, string[] IngredientString) public ViewResult Insert(RecipeModel recipeModel) { if (ModelState.IsValid) { recipeModel.Recipe.DateTimeUpdate = DateTime.Now; long size = 0; string fileName = Request.Form.Files.ElementAt(0).FileName.ToString(); foreach (var file in Request.Form.Files) { var filename = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"'); filename = this.ihostingEnvironment.WebRootPath + url + fileName; var urlfilename = this.ihostingEnvironment.WebRootPath + url + fileName; size += file.Length; using (FileStream fs = System.IO.File.Create(filename)) { file.CopyTo(fs); fs.Flush(); } } //The end of Upload File if (iRecipeRepo.Recipes.Where(r => r.RecipeId == recipeModel.Recipe.RecipeId).Count() == 0) { recipeModel.Recipe.FileToUpload = url + fileName; //Insert new recipe in recipe table iRecipeRepo.AddRecipe(recipeModel.Recipe); /** * It is being generated loop to create ingredientDetail and RecipeIngredient table * at the same time as much as the number of Ingredients by user */ foreach (string ingredientString in recipeModel.IngredientString) { if (string.IsNullOrEmpty(ingredientString)) { Console.WriteLine("There is a empty or null in the ingredient detail by the user input (ingredientString is null) in the recipe Id [" + recipeModel.Recipe.RecipeId + "]"); } else { IngredientDetail ingredientDetail = new IngredientDetail(); ingredientDetail.IngredientString = ingredientString; //Insert Ingredients details (IngredientString) of New Recipe in IngredientDetail Table iIngredientDetailRepo.AddIngredientDetail(ingredientDetail); // Insert Ingredients of New Recipe in RecipeIngredient Table as Bridge Table for many to many relations // Between Recipe and IngredientDetail Table (Ingredient Informations) iRecipeIngredientRepo.AddRecipeIngredient( recipeModel.Recipe.RecipeId, ingredientDetail); } } // Insert data (Model ID : 1, 2, 3 by the relationship Modal of new recipe) in RecipeModal Table as bridge table between recipe and Model (Model Id: 1, 2, 3) //to display View/Edit/Delete Button in "Data/List.cshtml" recipeModel.AllModalDetails = iModalDetailRepo.ModalDetails.ToList <ModalDetail>(); foreach (ModalDetail modalDetail in recipeModel.AllModalDetails) { iRecipeModalRepo.AllRecipeModal(recipeModel.Recipe.RecipeId, modalDetail); } return(View("../Recipe/Display", new SearchRecipeModel { RecipeCollection = iRecipeRepo.Recipes })); } else { return(View("Insert", recipeModel)); } } else { return(View("Insert", recipeModel)); } }
public ActionResult EditRecipe(RecipesListViewModel recipesListViewModel) { recipesListViewModel.Recipe.DateTimeUpdate = DateTime.Now; /** * Console.WriteLine("recipe IngredientString = " + recipesListViewModel.IngredientString); * * * Console.WriteLine("rlview recipeId = " + recipesListViewModel.Recipe.RecipeId); * Console.WriteLine("rlview RecipeTitle = " + recipesListViewModel.Recipe.RecipeTitle); * Console.WriteLine("rlview CategoryId = " + recipesListViewModel.Recipe.CategoryId); * * * Console.WriteLine("rlview Description = " + recipesListViewModel.Recipe.Description); * * Console.WriteLine("rlview Servings = " + recipesListViewModel.Recipe.Servings); * Console.WriteLine("rlview ServingsMax = " + recipesListViewModel.Recipe.ServingsMax); * Console.WriteLine("rlview Total = " + recipesListViewModel.Recipe.Total); * Console.WriteLine("rlview Yield = " + recipesListViewModel.Recipe.Yield); * Console.WriteLine("rlview CookTime = " + recipesListViewModel.Recipe.CookTime); * Console.WriteLine("rlview Prep = " + recipesListViewModel.Recipe.Prep); * Console.WriteLine("rlview DateTimeUpdate = " + recipesListViewModel.Recipe.DateTimeUpdate); * //string strName = Request.Form["pair.Value"].ToString(); * //Console.WriteLine("pair.value = " + strName); */ if (ModelState.IsValid) { recipesListViewModel.Recipe.FileToUpload = getFileUrlName(); Console.WriteLine("rlview FileToUpload = " + recipesListViewModel.Recipe.FileToUpload); //Insert new recipe in recipe table iRecipeRepo.UpdateRecipe(recipesListViewModel.Recipe); foreach (ChangedIngredientDetail chnIngredientDetail in recipesListViewModel.ChangedIngredientDetails) { //Console.WriteLine("ingredientString value of UpdateIngredientDetail in EditRecipe() = " + chnIngredientDetail.IngredientString); if (string.IsNullOrEmpty(chnIngredientDetail.IngredientString)) { Console.WriteLine("There is a empty or null in the ingredient detail by the user input (ingredientString is null) in the recipe Id [" + recipesListViewModel.Recipe.RecipeId + "]"); } else { IngredientDetail updateingredientDetail = new IngredientDetail(); updateingredientDetail.IngredientId = chnIngredientDetail.IngredientId; updateingredientDetail.IngredientString = chnIngredientDetail.IngredientString; Console.WriteLine("ingredientString value in EditRecipe() = " + updateingredientDetail.IngredientId); Console.WriteLine("ingredientString value in EditRecipe() = " + updateingredientDetail.IngredientString); iIngredientDetailRepo.UpdateIngredientDetail(updateingredientDetail); } } return(RedirectToAction("List", "Data")); } else { return(View("RecipeList", rlv)); } }