public IActionResult Search([FromBody] SearchBreweryDTO searchData) { if (!ModelState.IsValid) { return(StatusCode(422, ModelState)); } var result = _breweryService.SearchBreweries(searchData); if (result.IsError) { return(StatusCode(422, result.Errors)); } return(Ok(result.SuccessResult)); }
private bool MatchesSearch(Brewery brewery, SearchBreweryDTO searchData) { if (!String.IsNullOrWhiteSpace(searchData.Name)) { if (!brewery.Name.ToLower().Contains(searchData.Name.ToLower())) { return(false); } } return(((searchData.Type != null && brewery.Type != searchData.Type) || (searchData.Country != null && brewery.Country != searchData.Country) || brewery.YearEst < searchData.YearEstMin || brewery.YearEst > searchData.YearEstMax) ? false : true); }
public ServiceResult <BreweryDTO[]> SearchBreweries(SearchBreweryDTO searchData) { var matchingBreweries = _breweryRepostiory.GetAllBy(b => MatchesSearch(b, searchData)); var mappedBreweries = _mapper.Map <BreweryDTO[]>(matchingBreweries); foreach (var brewery in mappedBreweries) { if (brewery.ImagePath != null) { var imageResult = _imageService.GetImage(brewery.ImagePath); if (!imageResult.IsError) { brewery.Image = imageResult.SuccessResult; } } } return(new ServiceResult <BreweryDTO[]>(mappedBreweries)); }