public SentRecipe GetRecipeById(int id) { var recipe = _repo.GetRecipeById(id); System.Console.WriteLine("recipe by id: " + recipe.RecipeName); return(SentRecipe.GetFromRecipe(recipe)); }
public async Task <ActionResult <SentRecipe> > savePrepare([FromBody] HistoryModel historyModel) { // System.Console.WriteLine(body); System.Console.WriteLine("id: " + historyModel.recipeId + " sub: " + historyModel.sub); SentRecipe recipe = await this.iLogicKitchen.SaveRecipePrepare(historyModel); return(recipe); }
public async Task <ActionResult <SentRecipe> > saveRecipe([FromBody] SentRecipe sentRecipe) { var token = ControllerHelper.GetTokenFromRequest(this.Request); var userDictionary = await _auth.GetUserAuth0Dictionary(token); SentRecipe recipe = await this.iLogicKitchen.saveRecipe(sentRecipe, userDictionary["sub"]); return(recipe); }
public async Task <SentRecipe> SaveRecipePrepare(HistoryModel historyModel) { Recipe recipe = await _repo.AddNewPrepare(historyModel.recipeId, historyModel.sub); if (recipe == null) { return(null); } return(SentRecipe.GetFromRecipe(recipe)); }
public async Task <SentRecipe> saveRecipe(SentRecipe sentRecipe, string sub) { Recipe dbRecipe = _repo.GetRecipeById(sentRecipe.RecipeId); if (dbRecipe == null) { dbRecipe = await _repo.SaveNewRecipe(sub); } Recipe otherRecipe = await _mapSentIntoRecipe(dbRecipe, sentRecipe); return(SentRecipe.GetFromRecipe(otherRecipe)); }
public void TestGetFromRecipe() { var recipe = new Recipe() { RecipeId = 3, RecipeAuthor = "Anis", RecipeDescription = "svsvsvs", RecipeIngredients = new List <RecipeIngredient>(), RecipeImage = "Some Image", RecipeName = "Cheese", RecipeTags = new List <RecipeTag>(), RecipeAuthors = new List <RecipeAuthor>(), Reviews = new List <Review>() }; var sentRecipe = SentRecipe.GetFromRecipe(recipe); Assert.Equal(recipe.RecipeAuthor, sentRecipe.RecipeAuthor); }
public void TestMapMany() { var recipes = new List <Recipe>(); recipes.Add(new Recipe() { RecipeId = 1, RecipeAuthor = "Anis" }); recipes.Add(new Recipe() { RecipeId = 2, RecipeAuthor = "Nour" }); recipes.Add(new Recipe() { RecipeId = 3, RecipeAuthor = "Beau" }); var sentRecipe = SentRecipe.MapMany(recipes); Assert.Equal(sentRecipe.Count, recipes.Count); }
public void TestSendRecipe() { var recipeSent = new SentRecipe() { RecipeId = 4, RecipeName = "Cheese", RecipeDescription = "Something", RecipeImage = "Some Image", RecipeAuthor = "Anis", DateCreated = DateTime.Today, NumTimesPrepared = 5, DateLastPrepared = DateTime.Today, ingredients = new List <Ingredient>(), tags = new List <Tag>(), Steps = new List <Step>(), Reviews = new List <Review>(), }; var expected = "Anis"; var result = recipeSent.RecipeAuthor; Assert.Equal(expected, result); }
private async Task <Recipe> _mapSentIntoRecipe(Recipe dbRecipe, SentRecipe sentRecipe) { dbRecipe.RecipeDescription = sentRecipe.RecipeDescription; dbRecipe.RecipeName = sentRecipe.RecipeName; dbRecipe.RecipeImage = sentRecipe.RecipeImage; var sentTags = sentRecipe.tags; List <Tag> dbTags = await _repo.GetAllTags(); List <RecipeTag> dbRecipeTags = await _repo.GetAllRecipeTags(); sentTags.ToList().ForEach(tag => { var savedTag = dbTags.Where(t => t.TagId == tag.TagId).FirstOrDefault(); if (savedTag == null) { System.Console.WriteLine("tag not found: " + tag.TagName); Tag dbTag = _repo.saveNewTag(tag); _createRecipeTag(dbRecipe, dbTag); } else { System.Console.WriteLine("tag found: " + tag.TagName); var savedRecipeTag = dbRecipeTags.Where(rt => rt.TagId == savedTag.TagId && rt.RecipeId == dbRecipe.RecipeId).FirstOrDefault(); if (savedRecipeTag == null) { System.Console.WriteLine("added to recipe"); _createRecipeTag(dbRecipe, savedTag); } } }); dbRecipe.RecipeTags.ToList().ForEach(rt => { if (!sentRecipe.tags.Any(t => t.TagName == rt.Tag.TagName && t.TagDescription == rt.Tag.TagDescription)) { System.Console.WriteLine("we gonna try not to remove: " + rt.Tag.TagName); _repo.RemoveRecipeTag(rt); } }); await _repo.SaveChanges(); var sentIngredients = sentRecipe.ingredients; List <Ingredient> dbIngredients = await _repo.GetAllIngredients(); List <RecipeIngredient> dbRecipeIng = await _repo.GetAllRecipeIngredients(); sentIngredients.ToList().ForEach(ingredient => { var savedIngredient = dbIngredients.Where(t => t.IngredientId == ingredient.IngredientId).FirstOrDefault(); if (savedIngredient == null) { System.Console.WriteLine("ingredient not found: " + ingredient.IngredientName); Ingredient dbIngredient = _repo.saveNewIngredient(ingredient); _createRecipeIngredient(dbRecipe, dbIngredient); } else { System.Console.WriteLine("ingredient found: " + ingredient.IngredientName); var savedRecipeIngredient = dbRecipeIng.Where(ri => ri.IngredientId == savedIngredient.IngredientId && ri.RecipeId == dbRecipe.RecipeId).FirstOrDefault(); if (savedRecipeIngredient == null) { System.Console.WriteLine("added to recipe"); _createRecipeIngredient(dbRecipe, savedIngredient); } } }); dbRecipe.RecipeIngredients.ToList().ForEach(rt => { if (!sentRecipe.ingredients.Any(t => t.IngredientName == rt.Ingredient.IngredientName && t.IngredientDescription == rt.Ingredient.IngredientDescription)) { System.Console.WriteLine("we gonna try not to remove: " + rt.Ingredient.IngredientName); _repo.RemoveRecipeIngredient(rt); } }); await _repo.SaveChanges(); // List<Step> dbSteps = await _repo.GetAllSteps(); var dboldRecipeSteps = dbRecipe.Steps.ToList(); var newSteps = sentRecipe.Steps.ToList(); List <Step> savedSteps = new List <Step>(); for (int i = 0; i < newSteps.Count; i++) { var dbStep = dboldRecipeSteps.Where(s => s.RecipeId == newSteps[i].RecipeId && s.RecipeStepNo == newSteps[i].RecipeStepNo && s.StepDescription == newSteps[i].StepDescription); if (dbStep == null) { System.Console.WriteLine("step not found: " + newSteps[i].RecipeStepNo + " " + newSteps[i].StepDescription); Step savedStep = _repo.SaveNewStep(newSteps[i]); savedSteps.Add(savedStep); } else { System.Console.WriteLine("step found: " + newSteps[i].RecipeStepNo + " " + newSteps[i].StepDescription); savedSteps.Add(newSteps[i]); } } dboldRecipeSteps.ForEach(step => { if (!savedSteps.Any(s => s.StepId == step.StepId)) { System.Console.WriteLine("old step deleted: " + step.RecipeStepNo + " " + step.StepDescription); _repo.DeleteStep(step); } }); dbRecipe.Steps = savedSteps; await _repo.SaveChanges(); return(dbRecipe); }
public async Task <ICollection <SentRecipe> > getAllSentRecipe() { ICollection <Recipe> rs = _repo.GetAllRecipes(); return(SentRecipe.MapMany(rs)); }
public async Task TestRecipeSaving() { // var tag = new Recipe() { TagId = 123, TagName = "Cheese" }; var sentrecipe = new SentRecipe(); var date = DateTime.Now; sentrecipe.DateCreated = date; sentrecipe.DateLastPrepared = date; Tag t = new Tag() { TagName = "tag name 2" }; Ingredient i = new Ingredient() { IngredientName = "ing name 2" }; Step s = new Step() { StepDescription = "new description", RecipeStepNo = 1 }; sentrecipe.ingredients = new List <Ingredient>() { new Ingredient() { IngredientName = "ingredient name" } }; sentrecipe.tags = new List <Tag>() { new Tag() { TagName = "tag name" } }; Step s1 = new Step() { StepDescription = "step description" }; sentrecipe.Steps = new List <Step>() { s1 }; User user = new User() { Auth0 = "authcode", Firstname = "fname", Lastname = "lname", DateCreated = date, Email = "email", ImageUrl = "picture" }; AuthModel model = new AuthModel() { Sub = "authcode", FirstName = "fname", LastName = "lname", Email = "email", ProfileImage = "picture", Username = "******" }; Dictionary <string, string> userDictionary = new Dictionary <string, string>(); userDictionary["email"] = "email"; userDictionary["picture"] = "picture"; userDictionary["sub"] = "authcode"; bool result; Recipe recipe = null; using (var context = new InTheKitchenDBContext(testOptions)) { context.Database.EnsureDeletedAsync(); context.Database.EnsureCreatedAsync(); var repo = new KitchenRepository(context); var msr = new KitchenLogic(context, repo); var reviewlogic = new ReviewStepTagLogic(context, repo); var userlogic = new UserLogic(context, repo); userlogic.UpdateUser(model, userDictionary); userlogic.UpdateUser(model, userDictionary); var sr = await msr.saveRecipe(sentrecipe, model.Sub); sentrecipe.tags.Add(t); sentrecipe.ingredients.Add(i); sentrecipe.Steps.Add(s); sentrecipe.Steps.Remove(s1); sr = await msr.saveRecipe(sentrecipe, model.Sub); recipe = repo.GetRecipeById(sr.RecipeId); repo.UpdateUserAuth0Data(user); var rev = new Review() { Recipe = recipe, ReviewDate = date, ReviewDescription = "review description", RecipeId = recipe.RecipeId, }; reviewlogic.addReview(userDictionary["sub"], rev); HistoryModel hmodel = new HistoryModel() { recipeId = recipe.RecipeId, sub = model.Sub }; msr.SaveRecipePrepare(hmodel); } Assert.Equal(recipe.RecipeTags.ToArray()[0].Tag.TagName, "tag name"); }