public override async Task <ItemDetail> SearchItem(SingleItemRequest item)
        {
            if (item.ID.EmptyOrNull() && item.Title.EmptyOrNull())
            {
                if (item.Link.EmptyOrNull())
                {
                    throw new AllowedException("ID, Title and Link cannot be empty");
                }

                item = new AliexpressQueryString().DecodeItemLink(item.Link);

                //Means there was a translation error in the URI format
                if (item.ID.EmptyOrNull() & item.Title.EmptyOrNull())
                {
                    return(null);
                }
            }

            string endpoint = SearchEndpoints.AliexpressItemUrl(item.Title, item.ID);

            var        response = "";
            ItemDetail detail   = null;

            try
            {
                var responseMessage = await http.Get(endpoint);

                response = await responseMessage.Content.ReadAsStringAsync();
            }
            catch (Exception e)
            {
            }

            try
            {
                detail = new AliexpressPageDecoder().ScrapeSingleItem(response);

                var freights = await CalculateFreight(new FreightAjaxRequest()
                {
                    ProductID    = item.ID,
                    Country      = item.ShipCountry,
                    CurrencyCode = "USD"
                });

                if (freights.Length > 0)
                {
                    var def = freights.FirstOrDefault(x => x.IsDefault == true);
                    detail.ShippingPrice = def.LocalPrice;
                    detail.ShippingType  = def.CompanyDisplayName;
                }
            }
            catch (Exception e)
            {
                await raven.CaptureNetCoreEventAsync(e);
            }

            return(detail);
        }
        public async Task <FreightAjax[]> CalculateFreight(FreightAjaxRequest model)
        {
            string endpoint = SearchEndpoints.AliexpressFreight(model.ProductID, model.CurrencyCode, model.Country);

            var response = await(await http.Get(endpoint)).Content.ReadAsStringAsync();

            try
            {
                var freights = new AliexpressPageDecoder().ScrapeFreightResults(response);

                return(freights);
            }
            catch (Exception e)
            {
                await raven.CaptureNetCoreEventAsync(e);
            }

            return(null);
        }
        public async Task <IActionResult> Add([FromBody] SingleItemRequest item)
        {
            var username = "******";

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                username = HttpContext.User.Identity.Name;
            }

            if (item.Link.EmptyOrNull() && !item.Title.EmptyOrNull() && !item.ID.EmptyOrNull())
            {
                item.Link = SearchEndpoints.AliexpressItemUrl(item.Title, item.ID);
            }

            var detail = await search.ItemSearch(item);

            if (detail == null)
            {
                return(NotFound("Aliexpress link was incorrect"));
            }

            //Get integration access tokens
            var oauth = await oauthdb.RetrieveOAuth <OAuthShopifyModel>(username);

            if (oauth == null)
            {
                return(NotFound("No dropshipping integration setup for user"));
            }

            //Create the dropship item and model
            var dropshipItem = new DropshipItem()
            {
                Dropshipping = new DropshipItemModel()
                {
                    Source   = item,
                    Username = username,
                    Rules    = DropshipListingRules.Default,
                    OAuthID  = oauth.ID
                },
                Product = new ShopifyProductModel()
                {
                    BodyHtml = detail.Description,
                    Title    = detail.Name.Replace("/", "-"), //Fix slash in name issue

                    Variants = new List <ShopifyVarant>()
                    {
                        new ShopifyVarant()
                        {
                            InventoryPolicy     = InventoryPolicy.Deny,
                            InventoryManagement = InventoryManagement.Shopify,
                            RequiresShipping    = true,
                            Taxable             = true
                        }
                    }
                }
            };

            if (detail.ImageUrls != null)
            {
                //Add images from shopify
                var images = new List <ShopifyImageType>();
                foreach (var image in detail.ImageUrls)
                {
                    images.Add(new ShopifyImageType()
                    {
                        Src = image
                    });
                }

                //Set the first image to the main dropship model image
                if (images.Count > 0)
                {
                    dropshipItem.Dropshipping.Image = images[0].Src;
                }

                dropshipItem.Product.Images = images.ToArray();
            }

            //Apply dropshipping rules
            dropshipItem.Dropshipping.Rules.ApplyRules(detail, dropshipItem.Product);

            //Add product
            var product = await shopify.AddProduct(username, dropshipItem.Product, oauth);

            dropshipItem.Dropshipping.ListingID = product.ID;

            await dbItems.InsertItem(dropshipItem.Dropshipping);

            return(Ok());
        }