/// <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); }
public async Task <CityDTO> GetCityAsync(int id) { var city = await this.context.Cities .Include(c => c.Bars) .Where(c => !c.IsDeleted && c.Id == id) .FirstOrDefaultAsync(); if (city == null) { return(null); } var cityDTO = this.cityMapper.MapToCityDTO(city); cityDTO.Bars = city.Bars .Where(bar => !bar.IsDeleted) .Select(bar => barMapper.MapToBarDTO(bar)) .ToList(); return(cityDTO); }
public async Task <IList <BarDTO> > ListAllBarsAsync(int skip, int pageSize, string searchValue, string orderBy, string orderDirection) { var bars = this.context.Bars .Include(bar => bar.BarCocktails) .ThenInclude(bc => bc.Cocktail) .Include(bar => bar.City) .Where(bar => bar.IsDeleted == false); if (!string.IsNullOrEmpty(orderBy)) { if (string.IsNullOrEmpty(orderDirection) || orderDirection == "asc") { bars = bars.OrderBy(orderBy); } else { bars = bars.OrderByDescending(orderBy); } } if (!string.IsNullOrEmpty(searchValue)) { searchValue = searchValue.ToLower(); bars = bars .Where(bar => bar.Name.ToLower() .Contains(searchValue.ToLower())); } bars = bars .Skip(skip) .Take(pageSize); var barDTOs = await bars.Select(bar => barMapper.MapToBarDTO(bar)).ToListAsync(); return(barDTOs); }