public IHttpActionResult GetScenarioCampaignFailures([FromBody] ScenarioCampaignFailureSearchQueryModel queryModel) { if (!ModelState.IsValid || queryModel == null) { return(this.Error().InvalidParameters("One or more of the required query parameters are invalid.")); } if (queryModel.ScenarioId == Guid.Empty) { return(BadRequest("Invalid Scenario Id")); } if (queryModel.StrikeWeights?.Count() > 0) { foreach (var strikeWeight in queryModel.StrikeWeights) { strikeWeight.StrikeWeightStartDate = strikeWeight.StrikeWeightStartDate?.ToUniversalTime(); strikeWeight.StrikeWeightEndDate = strikeWeight.StrikeWeightEndDate?.ToUniversalTime(); } } var scenarioCampaignFailures = _scenarioCampaignFailureRepository.Search(queryModel); var scenarioCampaignFailureModelList = ExtendScenarioCampaignFailureModel(scenarioCampaignFailures.Items.ToList()); var searchModel = new SearchResultModel <ScenarioCampaignFailureModel>() { Items = scenarioCampaignFailureModelList.ToList(), TotalCount = scenarioCampaignFailures.TotalCount }; return(Ok(searchModel)); }
public PagedQueryResult <ScenarioCampaignFailure> Search(ScenarioCampaignFailureSearchQueryModel searchQuery) { if (searchQuery == null) { throw new ArgumentNullException(nameof(searchQuery)); } var query = _dbContext.Query <ScenarioCampaignFailureEntity>() .Where(p => p.ScenarioId == searchQuery.ScenarioId); if (searchQuery.SalesAreaGroupNames != null && searchQuery.SalesAreaGroupNames.Any()) { query = query.Where(e => searchQuery.SalesAreaGroupNames.Contains(e.SalesAreaGroup)); } if (searchQuery.ExternalCampaignIds != null && searchQuery.ExternalCampaignIds.Any()) { query = query.Where(p => searchQuery.ExternalCampaignIds.Contains(p.ExternalCampaignId)); } Expression <Func <ScenarioCampaignFailureEntity, bool> > exp = e => false; if (searchQuery.StrikeWeights?.Count() > 0) { var first = true; foreach (var strikeWeight in searchQuery.StrikeWeights) { Expression <Func <ScenarioCampaignFailureEntity, bool> > e = x => false; if (strikeWeight.StrikeWeightStartDate.HasValue) { e = scf => scf.StrikeWeightStartDate.Date > strikeWeight.StrikeWeightStartDate.Value.Date.AddDays(-1); if (strikeWeight.StrikeWeightEndDate.HasValue) { e = e.And(scf => scf.StrikeWeightEndDate.Date <= strikeWeight.StrikeWeightEndDate.Value.Date); } } else if (strikeWeight.StrikeWeightEndDate.HasValue) { e = scf => scf.StrikeWeightEndDate.Date <= strikeWeight.StrikeWeightEndDate.Value.Date; } if (first) { exp = e; first = false; } else { exp = exp.Or(e); } } query = query.Where(exp); } int totalCount = query.Count(); var scenarioCampaignFailures = query.ApplyPaging(searchQuery.Skip, searchQuery.Top) .ProjectTo <ScenarioCampaignFailure>(_mapper.ConfigurationProvider) .ToList(); return(new PagedQueryResult <ScenarioCampaignFailure>(totalCount, scenarioCampaignFailures)); }