public async Task AddWebSource() { await InitializeAsync(); using (var service = this.GetService()) { var recipe = await DbContext.Recipes.AsNoTracking().FirstAsync(); var newWebSource = new WebSourceCreationViewModel() { Name = "chefkoch.de", SourceUrl = "http://www.websource.com" }; var result = await service.AddWebSourceAsync(recipe.Id, newWebSource); Assert.NotNull(result); Assert.Equal(TestData.Sources.Count + 1, DbContext.RecipeBaseSources.Count()); var recipeAfterChange = await this.DbContext.Recipes.Include(x => x.Source).ThenInclude(x => x.Source).SingleAsync(x => x.Id.Equals(recipe.Id)); Assert.NotNull(recipeAfterChange); Assert.NotNull(recipeAfterChange.Source); var recipeSource = recipeAfterChange.Source.Source as RecipeUrlSource; Assert.NotNull(recipeSource); Assert.Equal(recipeSource.Id, result.Id); Assert.Equal(recipeSource.Url, result.SourceUrl); } }
public void Convert_WebSourceCreationViewModel_To_RecipeUrlSource() { Initalize(); var input = new WebSourceCreationViewModel() { Name = "My Source", SourceUrl = "http://www.test.de/source" }; var output = Mapper.Map <RecipeUrlSource>(input); Assert.IsType <RecipeUrlSource>(output); Assert.Equal(input.Name, output.Name); Assert.Equal(input.SourceUrl, output.Url); Assert.Equal(0, output.Id); Assert.NotNull(output.RecipeSourceRecipes); Assert.Empty(output.RecipeSourceRecipes); }
public async Task <WebSourceViewModel> AddWebSourceAsync(int recipeId, WebSourceCreationViewModel source) { WebSourceViewModel result = null; try { if (source == null) { throw new ArgumentNullException(nameof(source)); } result = await this._webSourceAdapter.AddAsync(new WebSourceCreationViewModel() { Name = source.Name, SourceUrl = source.SourceUrl }); var dbsource = await this._rep.WebSources.FindAsync(result.Id, x => x.RecipeSourceRecipes); var recipe = await this._recipeRep.FindAsync(recipeId, x => x.Source); if (recipe == null) { throw new DataObjectNotFoundException($"No Recipe with id '{recipeId}' found"); } recipe.Source = new RecipeSourceRecipe() { RecipeId = recipeId, SourceId = dbsource.Id }; await this._recipeRep.SaveChangesAsync(); } catch (Exception e) { var message = "An error on creating a web source"; _log.LogError(new EventId(), e, message); throw new Exception(message); } return(result); }
public async Task <IActionResult> AddWebSourceAsync(int recipeId, [FromBody] WebSourceCreationViewModel model) { var result = await _service.AddWebSourceAsync(recipeId, model); return(CreatedAtRoute("GetSourceById", new { sourceId = result.Id }, result)); }
/// <summary> /// Overrides a recipes source with the data of a web source /// </summary> /// <param name="recipeId">Id of owning recipe</param> /// <param name="model">New source data</param> /// <returns></returns> public async Task <WebSourceViewModel> CreateWebSourceForRecipeAsync(int recipeId, WebSourceCreationViewModel model) { return(await this.Client.PostAsJsonReturnAsync <WebSourceCreationViewModel, WebSourceViewModel>(model, $"Recipes/{recipeId}/WebSource")); }