public async Task <IEnumerable <CocktailResource> > GetByIng(int IngIds) { var cocktails = await _cocktailService.ListByIngredientAsync(IngIds); List <CocktailResource> resources = new List <CocktailResource>(); foreach (Domain.Models.Cocktail cocktail in cocktails) { CocktailResource resource = new CocktailResource() { Id = cocktail.Id, Name = cocktail.Name, Alcoholic = cocktail.Alcoholic, Category = new CategoryResource() { Id = cocktail.Category.Id, Name = cocktail.Category.Name }, Glass = cocktail.Glass, Tags = cocktail.Tags, Instructions = cocktail.Instructions, Thumb = cocktail.Thumb, Ingredients = new List <IngredientResource>() { } }; bool containsIngredient = false; foreach (CocktailIngredient ingredient in cocktail.IngredientsTo) { if (ingredient.IngredientId == IngIds) { containsIngredient = true; } resource.Ingredients.Add(new IngredientResource() { Id = ingredient.Ingredient.Id, Name = ingredient.Ingredient.Name }); } if (containsIngredient) { resources.Add(resource); } } return(resources); }
public async Task <IEnumerable <CocktailResource> > Get(int id) { var cocktails = await _cocktailService.IdAsync(id); List <CocktailResource> resources = new List <CocktailResource>(); foreach (Domain.Models.Cocktail cocktail in cocktails) { CocktailResource resource = new CocktailResource() { Id = cocktail.Id, Name = cocktail.Name, Alcoholic = cocktail.Alcoholic, Category = new CategoryResource() { Id = cocktail.Category.Id, Name = cocktail.Category.Name }, Glass = cocktail.Glass, Tags = cocktail.Tags, Instructions = cocktail.Instructions, Thumb = cocktail.Thumb, Ingredients = new List <IngredientResource>() { } }; foreach (CocktailIngredient ingredient in cocktail.IngredientsTo) { resource.Ingredients.Add(new IngredientResource() { Id = ingredient.Ingredient.Id, Name = ingredient.Ingredient.Name }); } resources.Add(resource); } return(resources); //var resources = _mapper.Map<IEnumerable<Domain.Models.Cocktail>, IEnumerable<CocktailResource>>(cocktails); }