/// <summary> /// Shows all available ingredients. /// </summary> /// <returns></returns> public async Task <ICollection <IngredientDTO> > GetAllIngredientsAsync() { var ingredients = this.context.Ingredients .Include(ingredient => ingredient.IngredientsCocktails) .ThenInclude(ic => ic.Cocktail) .Where(ingredient => ingredient.IsDeleted == false); var ingredientDTOs = await ingredients.Select(ingredient => mapper.MapToIngredientDTO(ingredient)).ToListAsync(); return(ingredientDTOs); }
/// <summary> /// If id exist and the cocktail is not deleted, shows the cocktail with that id, otherwise returns null. /// Also shows cocktail ingredients and bar/s where is served /// </summary> /// <param name="id">The id of the searched cocktail</param> /// <returns>Cocktail if id is valid, nothing if id is not valid, or cocktail is already deleted</returns> public async Task <CocktailDTO> GetCocktailAsync(int id) { var cocktail = await this.context.Cocktails .Include(cocktail => cocktail.CocktailBars) .ThenInclude(cb => cb.Bar) .ThenInclude(c => c.City) .Include(ingredient => ingredient.IngredientsCocktails) .ThenInclude(ic => ic.Ingredient) .Include(c => c.Creator) .FirstOrDefaultAsync(cocktail => cocktail.Id == id & cocktail.IsDeleted == false); if (cocktail == null) { return(null); } var cocktailDTO = mapper.MapToCocktailDTO(cocktail); cocktailDTO.AverageRating = this.reviewService.GetCocktailRating(id); var cocktailIngredient = cocktail.IngredientsCocktails.Select(x => x.Ingredient); var cocktailBars = cocktail.CocktailBars.Select(x => x.Bar); cocktailDTO.Ingredients = cocktailIngredient.Where(c => c.IsDeleted == false) .Select(x => ingredientMapper.MapToIngredientDTO(x)).ToList(); cocktailDTO.Bars = cocktailBars.Where(b => b.IsDeleted == false) .Select(bdto => barMapper.MapToBarDTO(bdto)).ToList(); return(cocktailDTO); }