Exemplo n.º 1
0
    public async Task <IHttpActionResult> GetProductsAsync([FromUri(Name = "from")] string originPostcode,
                                                           [FromUri(Name = "to")] string destinationPostcode,
                                                           [FromUri(Name = "weight")] decimal?weight,
                                                           [FromUri(Name = "item-category")] string itemCategory = "Merchandise",
                                                           [FromUri(Name = "country")] string destinationCountry = "MY",
                                                           [FromUri(Name = "length")] decimal?length             = null,
                                                           [FromUri(Name = "width")] decimal?width   = null,
                                                           [FromUri(Name = "height")] decimal?height = null)
    {
        var model = new SuggestProductModel
        {
            OriginPostcode      = originPostcode,
            Country             = destinationCountry,
            DestinationPostcode = destinationPostcode,
            Height = height,
            Length = length,
            Weight = weight,
            Width  = width
        };

        var request = new QuotationRequest
        {
            SenderPostcode   = originPostcode,
            ReceiverCountry  = destinationCountry,
            ReceiverPostcode = destinationPostcode,
            ItemCategory     = itemCategory,
            Height           = height,
            Length           = length,
            Weight           = weight,
            Width            = width
        };

        var snb      = ObjectBuilder.GetObject <ISnbService>();
        var products = (await snb.GetProductAsync(model, new CodedValueAddedServicesRule.CodedValuedAddedServicesRule())).ToList();
        var tasks    = from p in products
                       select snb.CalculatePublishedRateAsync(request, p, Array.Empty <ValueAddedService>());

        var prices = (await Task.WhenAll(tasks)).ToList();

        for (int i = 0; i < products.Count; i++)
        {
            products[i].TotalCost = prices[i].Total;
        }

        return(Ok(products));
    }
Exemplo n.º 2
0
        public async Task <bool> Validate(SuggestProductModel model, Product product, ValueAddedService vas)
        {
            var result = true;

            if (vas.Code == "V11")
            {
                var query = $@"{{
   ""query"": {{
      ""bool"": {{
         ""must"": [
            {{
               ""range"": {{
                  ""PostcodeFrom"": {{
                     ""lte"": ""{model.OriginPostcode}""
                  }}
               }}
            }},
            {{
                ""range"": {{
                   ""PostcodeTo"": {{
                      ""gte"": ""{model.OriginPostcode}""
                   }}
                }}
            }}
         ]
      }}
   }}
}}";
                var repos = ObjectBuilder.GetObject <IReadonlyRepository <PosLajuBranch> >();
                var pr    = await Policy.Handle <HttpRequestException>()
                            .WaitAndRetryAsync(5, x => TimeSpan.FromMilliseconds(Math.Pow(2, x) * 500))
                            .ExecuteAndCaptureAsync(async() => await repos.GetCountAsync(query, "from=0&size=0"));

                if (null != pr.FinalException)
                {
                    return(false);
                }
                result = pr.Result > 0;
            }

            // TODO : insurance - which one

            return(result);
        }