public async Task Update_Recipe() { //Arange var recipe = new UpsertRecipeModel { Name = "Salata", Access = true, Description = "Simpla si gustoasa, perfecta pentru zilele de vara", Filter = "Vegana", Ingredients = new List <string> { "ceapa", "ulei", "rosii", "varza", "castraveti" }, Type = "meal", Image = new byte[] { 1, 0, 0, 1 } }; var response = await HttpClient.PostAsJsonAsync($"api/v1/recipe", recipe); response.IsSuccessStatusCode.Should().BeTrue(); var body = await response.Content.ReadAsStringAsync(); var createdRecipeId = Extract_Guid(body); Recipes existingRecipe = null; await ExecuteDatabaseAction(async (tastyBoutiqueContext) => { existingRecipe = await tastyBoutiqueContext.Recipes .Include(r => r.Filters) .ThenInclude(r => r.Filter) .Include(r => r.Ingredients) .ThenInclude(r => r.Ingredient) .FirstOrDefaultAsync(r => r.Id == createdRecipeId); }); existingRecipe.Should().NotBeNull(); //Act recipe.Ingredients.Add("branza"); HttpContent httpContent = new StringContent(JsonSerializer.Serialize(recipe), Encoding.UTF8, "application/json-patch+json"); response = await HttpClient.PatchAsync($"api/v1/recipe/{createdRecipeId}", httpContent); //Assert response.IsSuccessStatusCode.Should().BeTrue(); await ExecuteDatabaseAction(async (tastyBoutiqueContext) => { existingRecipe = await tastyBoutiqueContext.Recipes .Include(r => r.Ingredients) .ThenInclude(r => r.Ingredient) .FirstOrDefaultAsync(r => r.Id == createdRecipeId); }); existingRecipe.Ingredients.Select(ri => ri.Ingredient.Name).Should().Contain("branza"); }
public async Task Update(Guid id, UpsertRecipeModel model) { var recipe = await _repository.GetById(id); recipe.Ingredients.Clear(); recipe.Filters.Clear(); recipe.Name = model.Name; recipe.Access = model.Access; recipe.Description = model.Description; if (model.Image != null) { recipe.Image = model.Image; } recipe.Type = model.Type; model.Ingredients = model.Ingredients.Distinct().ToList(); foreach (var ingredient in model.Ingredients) { var ing = await _ingredients.GetByName(ingredient); if (ing == null) { recipe.Ingredients.Add(new RecipesIngredients(recipe, new Ingredients(ingredient))); } else { recipe.Ingredients.Add(new RecipesIngredients(recipe, ing)); } } var fil = await _filters.GetByName(model.Filter); if (fil == null) { recipe.Filters.Add(new RecipesFilters(recipe, new Filters(model.Filter))); } else { recipe.Filters.Add(new RecipesFilters(recipe, fil)); } await _collections.SetAllByIdRecipe(recipe.Id); _repository.Update(recipe); await _repository.SaveChanges(); }
public async Task Get_Notifications() { //Arange var recipe = new UpsertRecipeModel { Name = "Salata", Access = true, Description = "Simpla si gustoasa, perfecta pentru zilele de vara", Filter = "Vegana", Ingredients = new List<string> { "ceapa", "ulei", "rosii", "varza", "castraveti" }, Type = "meal", Image = new byte[] { 1, 0, 0, 1 } }; var response = await HttpClient.PostAsJsonAsync($"api/v1/recipe", recipe); var body = await response.Content.ReadAsStringAsync(); var createdRecipeId = Extract_Guid(body); var savedRecipe = new SavedRecipes { IdUser = AuthenticatedUserId, IdRecipe = createdRecipeId }; await ExecuteDatabaseAction(async (tastyBoutiqueContext) => { await tastyBoutiqueContext.AddAsync(savedRecipe); tastyBoutiqueContext.SaveChanges(); }); recipe.Ingredients.Add("branza"); HttpContent httpContent = new StringContent(JsonSerializer.Serialize(recipe), Encoding.UTF8, "application/json-patch+json"); response = await HttpClient.PatchAsync($"api/v1/recipe/{createdRecipeId}", httpContent); //Act response = await HttpClient.GetAsync("api/v1/notifications"); response.IsSuccessStatusCode.Should().BeTrue(); var notifications = await response.Content.ReadAsAsync<IList<TotalRecipeModel>>(); //Assert notifications.Should().NotBeNull(); notifications.Should().NotBeEmpty(); notifications.Count.Should().Be(1); notifications[0].Name.Should().Be(recipe.Name); }
public async Task <TotalRecipeModel> Add(UpsertRecipeModel model) { model.IdUser = Guid.Parse(_accessor.HttpContext.User.Claims.First(c => c.Type == "IdUser").Value); var recipe = _mapper.Map <Persistance.Models.Recipes>(model); model.Ingredients = model.Ingredients.Distinct().ToList(); foreach (var ingredient in model.Ingredients) { var ing = await _ingredients.GetByName(ingredient); if (ing == null) { recipe.Ingredients.Add(new RecipesIngredients(recipe, new Ingredients(ingredient))); } else { recipe.Ingredients.Add(new RecipesIngredients(recipe, ing)); } } var fil = await _filters.GetByName(model.Filter); if (fil == null) { recipe.Filters.Add(new RecipesFilters(recipe, new Filters(model.Filter))); } else { recipe.Filters.Add(new RecipesFilters(recipe, fil)); } //recipe.Ingredients = recipe.RecipesIngredients.Select(x => x.Ingredient).ToList(); //recipe.Filters = recipe.RecipesFilters.Select(x => x.Filter).ToList(); await _repository.Add(recipe); await _repository.SaveChanges(); return(_mapper.Map <TotalRecipeModel>(recipe)); }
public async Task Post_Recipe() { //Arange var recipe = new UpsertRecipeModel { Name = "Salata", Access = true, Description = "Simpla si gustoasa, perfecta pentru zilele de vara", Filter = "Vegana", Ingredients = new List <string> { "ceapa", "ulei", "rosii", "varza", "castraveti" }, Type = "meal", Image = new byte[] { 1, 0, 0, 1 } }; var response = await HttpClient.PostAsJsonAsync($"api/v1/recipe", recipe); response.IsSuccessStatusCode.Should().BeTrue(); var body = await response.Content.ReadAsStringAsync(); var createdRecipeId = Extract_Guid(body); Recipes existingRecipe = null; await ExecuteDatabaseAction(async (tastyBoutiqueContext) => { existingRecipe = await tastyBoutiqueContext.Recipes .Include(r => r.Filters) .ThenInclude(r => r.Filter) .Include(r => r.Ingredients) .ThenInclude(r => r.Ingredient) .FirstOrDefaultAsync(r => r.Id == createdRecipeId); }); existingRecipe.Should().NotBeNull(); }
public static UpsertRecipeModel WithName(this UpsertRecipeModel model, string name) { model.Name = name; return(model); }
public static UpsertRecipeModel WithFilter(this UpsertRecipeModel model, string filter) { model.Filter = filter; return(model); }
public static UpsertRecipeModel WithIngredientsList(this UpsertRecipeModel model, List <string> ingredientsList) { model.Ingredients = ingredientsList; return(model); }
public static UpsertRecipeModel WithType(this UpsertRecipeModel model, string type) { model.Type = type; return(model); }
public static UpsertRecipeModel WithDescription(this UpsertRecipeModel model, string description) { model.Description = description; return(model); }
public async Task <IActionResult> Update([FromRoute] Guid recipeId, [FromBody] UpsertRecipeModel model) { await _recipeService.Update(recipeId, model); return(NoContent()); }
public async Task <IActionResult> Add([FromBody] UpsertRecipeModel model) { var result = await _recipeService.Add(model); return(Ok(result)); }