public List <Beer> GetBeers(BeerFilter beerFilter) { if (beerFilter.Limit <= 0) { return(null); } var beers = _dbContext.Beers .Skip(beerFilter.Offset) .Take(beerFilter.Limit) .Where(b => (string.IsNullOrEmpty(beerFilter.BeerName) || b.Name.Contains(beerFilter.BeerName)) && (string.IsNullOrEmpty(beerFilter.BreweryName) || b.Brewery.Name.Contains(beerFilter.BreweryName)) && (beerFilter.Country == null || b.Brewery.Country == beerFilter.Country) && (beerFilter.BeerStyle == null || b.Style == beerFilter.BeerStyle) && (beerFilter.ExtractFrom == null || b.Extract >= beerFilter.ExtractFrom) && (beerFilter.ExtractTo == null || b.Extract <= beerFilter.ExtractTo) && (beerFilter.AbvFrom == null || b.Abv >= beerFilter.AbvFrom) && (beerFilter.AbvTo == null || b.Abv <= beerFilter.AbvTo)) .Include(b => b.Brewery) .ToList(); foreach (var beer in beers) { beer.Brewery.Beers.Clear(); } return(beers); }
public IActionResult Get([FromQuery] BeerFilter beerFilter) { var beers = _beerManager.GetBeers(beerFilter); if (beers != null) { return(Ok(beers)); } return(NoContent()); }
public int GetNumberOfBeers(BeerFilter beerFilter) { return(_dbContext.Beers .Where(b => (string.IsNullOrEmpty(beerFilter.BeerName) || b.Name.Contains(beerFilter.BeerName)) && (string.IsNullOrEmpty(beerFilter.BreweryName) || b.Brewery.Name.Contains(beerFilter.BreweryName)) && (beerFilter.Country == null || b.Brewery.Country == beerFilter.Country) && (beerFilter.BeerStyle == null || b.Style == beerFilter.BeerStyle) && (beerFilter.ExtractFrom == null || b.Extract >= beerFilter.ExtractFrom) && (beerFilter.ExtractTo == null || b.Extract <= beerFilter.ExtractTo) && (beerFilter.AbvFrom == null || b.Abv >= beerFilter.AbvFrom) && (beerFilter.AbvTo == null || b.Abv <= beerFilter.AbvTo)) .Count()); }
public void GetFilteredBeersShouldCallRepoGetFilteredOnce() { var repo = new Mock <IBeerRepository>(); IBeerService service = new BeerService(repo.Object); BeerFilter filter = new BeerFilter() { CurrentPage = 1, ItemsPerPage = 10, IsAscending = true, SearchField = BeerFilter.Field.Id }; service.GetFilteredBeers(filter); repo.Verify(x => x.GetFiltered(filter), Times.Once); }
public ActionResult <List <Beer> > Get([FromQuery] BeerFilter filter) { try { if (filter.CurrentPage == 0 && filter.ItemsPerPage == 0 && filter.IsAscending == false && filter.SearchField == BeerFilter.Field.Id) { return(Ok(_service.GetBeers())); } return(Ok(_service.GetFilteredBeers(filter))); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public List <Beer> GetFilteredBeers(BeerFilter filter) { return(_beerRepo.GetFiltered(filter).ToList()); }
public IEnumerable <Beer> GetFiltered(BeerFilter beerFilter) { if (beerFilter == null) { return(_ctx.Beers); } IEnumerable <Beer> beers = _ctx.Beers .Skip((beerFilter.CurrentPage - 1) * beerFilter.ItemsPerPage) .Take(beerFilter.ItemsPerPage); if (!string.IsNullOrEmpty(beerFilter.SearchString)) { string search = beerFilter.SearchString.ToLower(); beers = beers.Where(b => string.Equals(b.Name.ToLower(), search) || string.Equals(b.Brand.ToLower(), search)); } if (beerFilter.IsAscending) { switch (beerFilter.SearchField) { case BeerFilter.Field.Id: beers = beers.OrderBy(x => x.ID); break; case BeerFilter.Field.Name: beers = beers.OrderBy(x => x.Name); break; case BeerFilter.Field.Brand: beers = beers.OrderBy(x => x.Brand); break; case BeerFilter.Field.Type: beers = beers.OrderBy(x => x.Type); break; } } else { switch (beerFilter.SearchField) { case BeerFilter.Field.Id: beers = beers.OrderByDescending(x => x.ID); break; case BeerFilter.Field.Name: beers = beers.OrderByDescending(x => x.Name); break; case BeerFilter.Field.Type: beers = beers.OrderByDescending(x => x.Type); break; case BeerFilter.Field.Brand: beers = beers.OrderByDescending(x => x.Brand); break; default: break; } } return(beers); }
public IActionResult GetNumberOfBeers([FromQuery] BeerFilter beerFilter) { return(Ok(_beerManager.GetNumberOfBeers(beerFilter))); }