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 <GetPostcodeResponse> Handle(GetPostcodeRequest request, CancellationToken cancellationToken) { request.Postcode = PostcodeFormatter.FormatPostcode(request.Postcode); PostcodeDto postcodeDto = await _postcodeAndAddressGetter.GetPostcodeAsync(request.Postcode, cancellationToken); GetPostcodeResponse getNearbyGetPostcodesResponse = _mapper.Map <PostcodeDto, GetPostcodeResponse>(postcodeDto); getNearbyGetPostcodesResponse.AddressDetails = _addressDetailsSorter.OrderAddressDetailsResponse(getNearbyGetPostcodesResponse.AddressDetails); return(getNearbyGetPostcodesResponse); }