Exemplo n.º 1
0
        /// <summary>
        /// Gets pricing from the api
        /// </summary>
        /// <param name="price">Entity</param>
        /// <param name="number"></param>
        /// <param name="type"></param>
        /// <param name="colourId"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PartPriceInfo UpdatePartPricingFromApi(string number, string type, int colourId, string condition = "N", PartPriceInfo price = null)
        {
            if (number == null)
            {
                return(new PartPriceInfo());
            }

            if (price == null)
            {
                price = new PartPriceInfo();
            }

            var response = GetRequest <GetPriceGuideResponse>($"items/{type}/{number}/price?guide_type=sold&currency_code=GBP&vat=Y&country_code=UK&new_or_used={condition}&color_id={colourId}");

            var loc = "";

            // Fall back to EU
            if (response.data.avg_price == "0.0000")
            {
                response = GetRequest <GetPriceGuideResponse>($"items/{type}/{number}/price?guide_type=sold&currency_code=GBP&vat=Y&region=europe&new_or_used={condition}&color_id={colourId}");
                loc      = "(EU)";
            }

            // Fall back to world
            if (response.data.avg_price == "0.0000")
            {
                response = GetRequest <GetPriceGuideResponse>($"items/{type}/{number}/price?guide_type=sold&currency_code=GBP&vat=Y&new_or_used={condition}&color_id={colourId}");
                loc      = "(World)";
            }

            if (decimal.TryParse(response.data.avg_price, out decimal tmp))
            {
                price.AveragePrice = tmp;
            }
            else
            {
                price.AveragePrice = 0;
            }
            price.AveragePriceLocation = loc;
            price.LastUpdated          = DateTime.UtcNow;

            return(price);
        }
        public void AddPartInv(ref PartInventory inv, ref PartPriceInfo price, ref Part part)
        {
            if (part.Id == 0)
            {
                part = _ctx.Parts.Add(part);
            }
            else
            {
                int partId = part.Id;
                var local  = _ctx.Set <Part>().Local.FirstOrDefault(x => x.Id == partId);
                if (local != null)
                {
                    _ctx.Entry(local).State = EntityState.Detached;
                }
                part = _ctx.Parts.Attach(part);
            }

            inv.Part = part;

            inv = _ctx.PartInventorys.Add(inv);

            price.InventoryItem = inv;
            price = _ctx.PartPriceInfos.Add(price);

            if (inv.Location != "")
            {
                var history = new PartInventoryLocationHistory
                {
                    Location      = inv.Location,
                    Date          = DateTime.UtcNow,
                    PartInventory = inv
                };
                _ctx.PartInventoryLocationHistorys.Add(history);
            }

            _ctx.SaveChanges();
        }