public async Task <List <JobSummary> > AttachedDistanceToJobSummaries(string volunteerPostCode, List <JobSummary> jobSummaries, CancellationToken cancellationToken) { if (jobSummaries.Count == 0) { return(null); } volunteerPostCode = PostcodeFormatter.FormatPostcode(volunteerPostCode); List <string> distinctPostCodes = jobSummaries.Select(d => d.PostCode).Distinct().Select(x => PostcodeFormatter.FormatPostcode(x)).ToList(); if (!distinctPostCodes.Contains(volunteerPostCode)) { distinctPostCodes.Add(volunteerPostCode); } var postcodeCoordinatesResponse = await _repository.GetLatitudeAndLongitudes(distinctPostCodes, cancellationToken); if (postcodeCoordinatesResponse == null) { return(null); } var volunteerPostcodeCoordinates = postcodeCoordinatesResponse.Where(w => w.Postcode == volunteerPostCode).FirstOrDefault(); if (volunteerPostcodeCoordinates == null) { return(null); } foreach (JobSummary jobSummary in jobSummaries) { var jobPostcodeCoordinates = postcodeCoordinatesResponse.Where(w => w.Postcode == jobSummary.PostCode).FirstOrDefault(); if (jobPostcodeCoordinates != null) { jobSummary.DistanceInMiles = _distanceCalculator.GetDistanceInMiles(volunteerPostcodeCoordinates.Latitude, volunteerPostcodeCoordinates.Longitude, jobPostcodeCoordinates.Latitude, jobPostcodeCoordinates.Longitude); } } return(jobSummaries); }
public async Task <GetDistanceBetweenPostcodesResponse> Handle(GetDistanceBetweenPostcodesRequest request, CancellationToken cancellationToken) { request.Postcode1 = PostcodeFormatter.FormatPostcode(request.Postcode1); request.Postcode2 = PostcodeFormatter.FormatPostcode(request.Postcode2); var postcodeDetails = await _repository.GetPostcodeCoordinatesAsync(new List <string>() { request.Postcode1, request.Postcode2 }); if (postcodeDetails == null) { throw new Exception($"Unable to retrieve post code details for {request.Postcode1} or {request.Postcode2}"); } if (postcodeDetails.Count() != 2) { throw new Exception($"Only expecting 2 row in collection for {request.Postcode1} and {request.Postcode2}. {postcodeDetails.Count()} rows returned"); } var postcodeDetails1 = postcodeDetails.FirstOrDefault(x => x.Postcode == request.Postcode1); var postcodeDetails2 = postcodeDetails.FirstOrDefault(x => x.Postcode == request.Postcode2); if (postcodeDetails1 == null || postcodeDetails2 == null) { throw new Exception($"Either postcode details not returned for {request.Postcode1} and {request.Postcode2}."); } var distanceInMiles = _distanceCalculator.GetDistanceInMiles( postcodeDetails1.Latitude, postcodeDetails1.Longitude, postcodeDetails2.Latitude, postcodeDetails2.Longitude ); return(new GetDistanceBetweenPostcodesResponse() { DistanceInMiles = distanceInMiles }); }
public async Task <IEnumerable <HelperWithinRadiusDTO> > GetHelpersWithinRadius(string postcode, double?overrideVolunteerRadius, CancellationToken token) { var helpers = new List <HelperWithinRadiusDTO>(); LatitudeAndLongitudeDTO comparePostcode = _repository.GetLatitudeAndLongitude(postcode); VolunteerType volunteerType = VolunteerType.Helper | VolunteerType.StreetChampion; Task <IEnumerable <CachedVolunteerDto> > cachedVolunteerDtosTask = _volunteerCache.GetCachedVolunteersAsync(volunteerType, token); await Task.WhenAll(cachedVolunteerDtosTask); IEnumerable <CachedVolunteerDto> cachedVolunteerDtos = await cachedVolunteerDtosTask; Dictionary <int, double> idsOfHelpersWithinRadius = new Dictionary <int, double>(); foreach (CachedVolunteerDto cachedVolunteerDto in cachedVolunteerDtos) { double distance = _distanceCalculator.GetDistanceInMiles(comparePostcode.Latitude, comparePostcode.Longitude, cachedVolunteerDto.Latitude, cachedVolunteerDto.Longitude); bool isWithinSupportRadius = distance <= (overrideVolunteerRadius.HasValue? overrideVolunteerRadius.Value : cachedVolunteerDto.SupportRadiusMiles); if (isWithinSupportRadius) { idsOfHelpersWithinRadius.Add(cachedVolunteerDto.UserId, distance); } } var users = await _repository.GetVolunteersByIdsAsync(idsOfHelpersWithinRadius.Keys); if (users.Any()) { helpers = users.Select(x => new HelperWithinRadiusDTO { User = x, Distance = idsOfHelpersWithinRadius[x.ID] }).ToList(); } return(helpers); }
private async Task <List <JobSummary> > AttachedDistanceToJobSummaries(string volunteerPostCode, List <PostcodeCoordinate> postcodeCoordinates, List <JobSummary> jobSummaries) { PostcodeCoordinate volunteerPostCodeDetails = postcodeCoordinates.FirstOrDefault(x => x.Postcode == volunteerPostCode); if (volunteerPostCodeDetails == null) { return(jobSummaries); } foreach (JobSummary jobSummary in jobSummaries) { PostcodeCoordinate jobPostCodeDetails = postcodeCoordinates.FirstOrDefault(x => x.Postcode == jobSummary.PostCode); if (jobPostCodeDetails != null) { jobSummary.DistanceInMiles = _distanceCalculator.GetDistanceInMiles( volunteerPostCodeDetails.Latitude, volunteerPostCodeDetails.Longitude, jobPostCodeDetails.Latitude, jobPostCodeDetails.Longitude); } } return(jobSummaries); }