コード例 #1
0
        public async Task <ContentResult> LegacyLookupAsync(string output, string addr, string city, string zip)
        {
            // Using a contentresult here because we want to return Xml without using the built in XML serializer so that we have complete control over the format and content of the response.
            var response = new ContentResult();

            bool useXml = false;

            if (output == "xml")
            {
                useXml = true;
            }

            if (useXml)
            {
                response.ContentType = "text/xml; charset=utf-8";
                response.Content    += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
            }
            else
            {
                response.ContentType = "text/html; charset=utf-8";
            }

            zip = zip.Trim();

            // Fail fast on invalid zip codes.
            if (!string.IsNullOrWhiteSpace(zip) && (zip.Length == 5 || zip.Length == 9 || zip.Length == 10))
            {
                ShortZip matchingZip;

                // Find a representitive zip code entry as a starting place.
                switch (zip.Length)
                {
                case 5:
                    matchingZip = await _context.ZipCodes.Where(x => x.Zip == zip).FirstOrDefaultAsync();

                    break;

                case 9:
                    matchingZip = await _context.ZipCodes.Where(x => x.Zip == zip.Substring(0, 5)).FirstOrDefaultAsync();

                    break;

                case 10:
                    zip         = zip.Replace("-", string.Empty);
                    matchingZip = await _context.ZipCodes.Where(x => x.Zip == zip.Substring(0, 5)).FirstOrDefaultAsync();

                    break;

                default:
                    matchingZip = null;
                    break;
                }

                // If no zip code if found return an invalid response.
                if (matchingZip is null)
                {
                    if (useXml)
                    {
                        response.Content += "<response loccode=\"\" localrate=\"\" rate=\"\" code=\"4\" debughint=\"Invalid ZIP\"><addressline/><rate/></response>";
                    }
                    else
                    {
                        response.Content += "LocationCode=-1 Rate=-1 ResultCode=4 debughint=Invalid ZIP";
                    }
                    response.StatusCode = 400;
                }
                else if (string.IsNullOrWhiteSpace(addr) && zip.Length == 5)
                {
                    // 5 digit ZIP only, no address provided.
                    var rate = await _context.TaxRates.Where(x => x.LocationCode == matchingZip.LocationCode).FirstOrDefaultAsync();

                    if (useXml)
                    {
                        response.Content += $"<response loccode=\"{rate.LocationCode}\" localrate=\"{rate.Local:.000}\" rate=\"{rate.Rate:.000}\" code=\"5\" xmlns=\"\"><addressline code=\"{rate.LocationCode}\" state=\"WA\" zip=\"{matchingZip.Zip}\" period=\"{Period.CurrentPeriod().PeriodLit}\" rta=\"\" ptba=\"\" cez=\"\" />{rate.ToXML()}</response>";
                    }
                    else
                    {
                        response.Content += $"LocationCode={rate.LocationCode} Rate={rate.Rate:.000} ResultCode=3";
                    }
                    response.StatusCode = 200;
                    return(response);
                }
                else
                {
                    List <AddressRange> relatedAddressRanges;

                    if (zip.Length == 9)
                    {
                        var plus4 = zip[5..];