private List<Convict> FilterData(SearchParams searchParams) { using (var dataContext = new CrosoverTaskDataContex()) { IQueryable<Convict> filteredQueriable = dataContext.Convicts; if (searchParams.AgeStart.HasValue) { DateTime end = DateTime.Now.AddYears(-1 * searchParams.AgeStart.Value); filteredQueriable = filteredQueriable.Where(x => x.DateOfBirth < end); } if (searchParams.AgeEnd.HasValue) { DateTime start = DateTime.Now.AddYears(-1 * searchParams.AgeEnd.Value); filteredQueriable = filteredQueriable.Where(x => x.DateOfBirth > start); } if (searchParams.HeightStart.HasValue) { float start = searchParams.HeightStart.Value; filteredQueriable = filteredQueriable.Where(x => x.Height >= start); } if (searchParams.HeightEnd.HasValue) { float end = searchParams.HeightEnd.Value; filteredQueriable = filteredQueriable.Where(x => x.Height <= end); } if (searchParams.WeightStart.HasValue) { float start = searchParams.WeightStart.Value; filteredQueriable = filteredQueriable.Where(x => x.Weight >= start); } if (searchParams.WeightEnd.HasValue) { float end = searchParams.WeightEnd.Value; filteredQueriable = filteredQueriable.Where(x => x.Weight <= end); } if (searchParams.Name != null) { filteredQueriable = filteredQueriable.Where(x => x.FirstName.ToLower().Contains(searchParams.Name.ToLower())); } if (searchParams.Surname != null) { filteredQueriable = filteredQueriable.Where(x => x.LastName.ToLower().Contains(searchParams.Surname.ToLower())); } if (searchParams.Gender.HasValue) { filteredQueriable = filteredQueriable.Where(x => x.Gender == searchParams.Gender); } return filteredQueriable.Take(searchParams.MaxNumberOfResults).ToList(); } }
/// <summary> /// Checks the api key of the request. /// </summary> /// <param name="searchParams"></param> /// <returns></returns> public bool IsAuthorized(SearchParams searchParams) { //Idealy this would be a function of the request identity or some form of authentication/authorization //This is just to ilustrate the concept //The web service is configured to use https so no plain api keys are sent over the net string SecretKey = "70291a7ce563f15dcdd84c3091a52cc3"; if (SecretKey.Equals(searchParams.SecretKey)) { return true; } return false; }
/// <summary> /// Executes an async search in the db, and mails the results. /// A boolean responce of the request validity is imidiatly returned /// </summary> /// <param name="searchParams">The search criteria, apikey and recipient email</param> /// <returns></returns> public bool Search(SearchParams searchParams) { if (!Validator.IsAuthorized(searchParams)) { return false; } if (!Validator.IsSearchCriteriaValid(searchParams)) { return false; } var asyncSearchAndEmail = new CustomBackgroundActionExecutor<SearchParams>(); asyncSearchAndEmail.Execute(ExecuteSearchAndMail, searchParams); return true; }
private bool HasNonNullCriteria(SearchParams searchParams) { List<object> criteriaProperties = new List<object>(); criteriaProperties.Add(searchParams.AgeEnd); criteriaProperties.Add(searchParams.AgeStart); criteriaProperties.Add(searchParams.HeightEnd); criteriaProperties.Add(searchParams.HeightStart); criteriaProperties.Add(searchParams.WeightEnd); criteriaProperties.Add(searchParams.WeightStart); criteriaProperties.Add(searchParams.Name); criteriaProperties.Add(searchParams.Surname); criteriaProperties.Add(searchParams.Gender); if (!criteriaProperties.Any(o => o != null)) { return false; } return true; }
/// <summary> /// Checks if the search parameters are valid (ex. within range, max>min etc.. ) /// </summary> /// <param name="searchParams"></param> /// <returns></returns> public bool IsSearchCriteriaValid(SearchParams searchParams) { //check if there is at least one search criteria if (!HasNonNullCriteria(searchParams)) { return false; } //Check if request params are ok var context = new ValidationContext(searchParams, serviceProvider: null, items: null); var validationResults = new List<ValidationResult>(); Validator.TryValidateObject(searchParams, context, validationResults, true); if (validationResults.Any(eror => eror != null)) { return false; } return true; }
private void ExecuteSearchAndMail(SearchParams searchParams) { var filteredResults = FilterData(new SearchParams { AgeStart = 1 }); mailer.SendSearchResults(filteredResults, searchParams.RecieverEmail); }