public async Task <IActionResult> GetAvailability([FromRoute] string storeId, [FromRoute] string productNumber)
        {
            var client = new HttpClient();

            string apiResult = null;

            try
            {
                apiResult = await client.GetStringAsync(string.Format(AvailabilityUrl, productNumber));
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
                return(BadRequest("IKEA-API call failed."));
            }

            if (string.IsNullOrWhiteSpace(apiResult))
            {
                return(BadRequest("IKEA-API call failed."));
            }

            var xmlSerializer = new XmlSerializer(typeof(Ikearest));

            xmlSerializer.UnknownAttribute += (s, e) =>
            {
                Console.Error.WriteLine("Warning: Deserializing "
                                        + e.ObjectBeingDeserialized
                                        + ": Unknown attribute "
                                        + e.Attr.Name);
            };
            xmlSerializer.UnknownElement += (s, e) =>
            {
                Console.Error.WriteLine("Warning: Deserializing "
                                        + e.ObjectBeingDeserialized
                                        + ": Unknown element "
                                        + e.Element.Name);
            };
            xmlSerializer.UnknownNode += (s, e) =>
            {
                Console.Error.WriteLine("Warning: Deserializing "
                                        + e.ObjectBeingDeserialized
                                        + ": Unknown node "
                                        + e.Name);
            };

            Ikearest ikeaRest = null;

            try
            {
                var deserializeResult = xmlSerializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(apiResult)));
                ikeaRest = (Ikearest)deserializeResult;

                var ikeaStore = ikeaRest.Availability.LocalStore.FirstOrDefault(x => x.BuCode == storeId);
                if (ikeaStore == null)
                {
                    return(BadRequest("Product not sold in store"));
                }

                var ikeaStock = new IkeaStock
                {
                    StoreId = ikeaStore.BuCode,
                    InStockProbabilityCode = ikeaStore.Stock.InStockProbabilityCode,
                    IsInStoreRange         = String.Equals(ikeaStore.Stock.IsInStoreRange, Boolean.TrueString, StringComparison.InvariantCultureIgnoreCase),
                    IsMultiProduct         = String.Equals(ikeaStore.Stock.IsMultiProduct, Boolean.TrueString, StringComparison.InvariantCultureIgnoreCase),
                    IsSoldInStore          = String.Equals(ikeaStore.Stock.IsSoldInStore, Boolean.TrueString, StringComparison.InvariantCultureIgnoreCase),
                    PartNumber             = ikeaStore.Stock.PartNumber
                };

                if (int.TryParse(ikeaStore.Stock.AvailableStock, out var availableStock))
                {
                    ikeaStock.AvailableStock = availableStock;
                }
                else
                {
                    ikeaStock.AvailableStock = -1;
                }

                foreach (var item in ikeaStore.Stock.FindItList.FindIt)
                {
                    var stockProduct = new IkeaStockProduct
                    {
                        PartNumber = item.PartNumber,
                        Box        = item.Box,
                        Shelf      = item.Shelf,
                        Type       = item.Type
                    };
                    if (int.TryParse(item.Quantity, out var quantity))
                    {
                        stockProduct.Quantity = quantity;
                    }
                    else
                    {
                        stockProduct.Quantity = -1;
                    }
                    ikeaStock.Products.Add(stockProduct);
                }

                return(Json(ikeaStock));
            }
            catch
            {
                return(BadRequest("API-Result cannot be deserialized."));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetByProductNumber(string number)
        {
            var productNumber = number.PadLeft(8, '0');

            var client = new HttpClient();

            var apiResult = await client.GetStringAsync(string.Format(GermanProductUrl, productNumber));

            if (string.IsNullOrWhiteSpace(apiResult))
            {
                return(BadRequest("IKEA-API call failed."));
            }

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Ikearest));

            xmlSerializer.UnknownAttribute += (s, e) =>
            {
                Console.Error.WriteLine("Warning: Deserializing "
                                        + e.ObjectBeingDeserialized
                                        + ": Unknown attribute "
                                        + e.Attr.Name);
            };
            xmlSerializer.UnknownElement += (s, e) =>
            {
                Console.Error.WriteLine("Warning: Deserializing "
                                        + e.ObjectBeingDeserialized
                                        + ": Unknown element "
                                        + e.Element.Name);
            };
            xmlSerializer.UnknownNode += (s, e) =>
            {
                Console.Error.WriteLine("Warning: Deserializing "
                                        + e.ObjectBeingDeserialized
                                        + ": Unknown node "
                                        + e.Name);
            };

            Ikearest ikeaRest = null;

            try
            {
                var deserializeResult = xmlSerializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(apiResult)));
                ikeaRest = (Ikearest)deserializeResult;
            }
            catch
            {
                return(BadRequest("API-Result cannot be deserialized."));
            }

            if (ikeaRest != null)
            {
                var ikeaProduct = ikeaRest.Products.Product.FirstOrDefault();
                if (ikeaProduct == null)
                {
                    return(BadRequest("Product not found."));
                }

                var product = new IkeaProduct();

                product.ParentUrl               = string.Format(IkeaBaseUrl, ikeaProduct.URL);
                product.ParentPartNumber        = ikeaProduct.PartNumber;
                product.ParentDisplayName       = ikeaProduct.Name;
                product.ParentDisplayNameSweden = ikeaProduct.Nameswe;

                var ikeaProductItem = ikeaProduct.Items.Item;
                product.Url               = string.Format(IkeaBaseUrl, ikeaProductItem.URL);
                product.PartNumber        = ikeaProductItem.PartNumber;
                product.DisplayName       = ikeaProductItem.Name;
                product.DisplayNameSweden = ikeaProductItem.Nameswe;
                product.CareInstructions  = ikeaProductItem.CareInst;
                product.CustomerBenefit   = ikeaProductItem.CustBenefit;
                product.CustomerMaterials = ikeaProductItem.CustMaterials;
                product.Designer          = ikeaProductItem.Designer;
                product.Environment       = ikeaProductItem.Environment;
                product.Facts             = ikeaProductItem.Facts;
                product.GoodToKnow        = ikeaProductItem.GoodToKnow;
                product.IsBti             = string.Equals(ikeaProductItem.Bti, bool.TrueString, StringComparison.InvariantCultureIgnoreCase);
                product.IsNew             = string.Equals(ikeaProductItem.New, bool.TrueString, StringComparison.InvariantCultureIgnoreCase);
                product.Measure           = ikeaProductItem.Measure;
                if (int.TryParse(ikeaProductItem.Nopackages, out var numberOfPackages))
                {
                    product.NumberOfPackages = numberOfPackages;
                }
                product.RequireAssembly = string.Equals(ikeaProductItem.ReqAssembly, bool.TrueString, StringComparison.InvariantCultureIgnoreCase);
                product.Type            = ikeaProductItem.Type;

                if (decimal.TryParse(ikeaProductItem.Prices.Normal.PriceNormal.Unformatted, out var normalPrice))
                {
                    product.NormalPrice = normalPrice;
                }
                if (decimal.TryParse(ikeaProductItem.Prices.Second.PriceNormal.Unformatted, out var secondPrice))
                {
                    product.SecondPrice = secondPrice;
                }
                if (decimal.TryParse(ikeaProductItem.Prices.Familynormal.PriceNormal.Unformatted, out var familyPrice))
                {
                    product.FamilyNormalPrice = familyPrice;
                }

                foreach (var item in ikeaProductItem.SubItems.Article)
                {
                    if (int.TryParse(item.Quantity, out var quantity))
                    {
                        product.SubProducts.Add(new IkeaSubProduct(item.PartNumber, quantity));
                    }
                }

                foreach (var item in ikeaProductItem.Images.Normal.Image)
                {
                    product.SmallImageUrls.Add(item.Text);
                }
                foreach (var item in ikeaProductItem.Images.Zoom.Image)
                {
                    product.LargeImageUrls.Add(item.Text);
                }

                return(Json(product));
            }

            return(BadRequest());
        }