public async Task <GetNearbyPostcodesResponse> Handle(GetNearbyPostcodesRequest request, CancellationToken cancellationToken) { string postcode = PostcodeFormatter.FormatPostcode(request.Postcode); // get nearest postcodes IReadOnlyList <NearestPostcodeDto> nearestPostcodeDtos = await _nearestPostcodeGetter.GetNearestPostcodesAsync(postcode, request.RadiusInMetres, request.MaxNumberOfResults); IEnumerable <string> nearestPostcodes = nearestPostcodeDtos.Select(x => x.Postcode).ToList(); // get postcodes IEnumerable <PostcodeDto> postcodeDtos = await _postcodeAndAddressGetter.GetPostcodesAsync(nearestPostcodes, cancellationToken); // create response GetNearbyPostcodesResponse getNearbyPostcodesResponse = new GetNearbyPostcodesResponse(); IEnumerable <GetNearbyPostCodeResponse> getNearbyPostCodeResponses = _mapper.Map <IEnumerable <PostcodeDto>, IEnumerable <GetNearbyPostCodeResponse> >(postcodeDtos); getNearbyPostcodesResponse.Postcodes = (from getNearbyPostCodeResponse in getNearbyPostCodeResponses join nearestPostcodeDto in nearestPostcodeDtos on getNearbyPostCodeResponse.Postcode equals nearestPostcodeDto.Postcode select new GetNearbyPostCodeResponse { Postcode = getNearbyPostCodeResponse.Postcode, AddressDetails = _addressDetailsSorter.OrderAddressDetailsResponse(getNearbyPostCodeResponse.AddressDetails), DistanceInMetres = nearestPostcodeDto.DistanceInMetres, FriendlyName = getNearbyPostCodeResponse.FriendlyName }) .OrderBy(x => x.DistanceInMetres) .ToList(); return(getNearbyPostcodesResponse); }
public async Task <GetNearbyPostcodesWithoutAddressesResponse> Handle(GetNearbyPostcodesWithoutAddressesRequest request, CancellationToken cancellationToken) { request.Postcode = PostcodeFormatter.FormatPostcode(request.Postcode); IEnumerable <NearestPostcodeDto> nearestPostcodeDtos = await _nearestPostcodeGetter.GetNearestPostcodesAsync(request.Postcode, request.RadiusInMetres, request.MaxNumberOfResults); IEnumerable <NearestPostcodeWithoutAddress> nearestPostcodes = _mapper.Map <IEnumerable <NearestPostcodeDto>, IEnumerable <NearestPostcodeWithoutAddress> >(nearestPostcodeDtos); GetNearbyPostcodesWithoutAddressesResponse getNearbyPostcodesWithoutAddressesResponse = new GetNearbyPostcodesWithoutAddressesResponse() { NearestPostcodes = nearestPostcodes.ToList() }; return(getNearbyPostcodesWithoutAddressesResponse); }