public void RunLocations()
        {
            var dataLocations = _locationRepository.RetreiveLocations();

            var shopifyLocations
                = _productApi
                  .RetrieveLocations()
                  .DeserializeFromJson <LocationList>();

            foreach (var shopifyLoc in shopifyLocations.locations)
            {
                var dataLocation = dataLocations.FindByShopifyId(shopifyLoc);

                using (var transaction = _locationRepository.BeginTransaction())
                {
                    if (dataLocation == null)
                    {
                        var newDataLocation = new ShopifyLocation
                        {
                            ShopifyLocationId   = shopifyLoc.id,
                            ShopifyLocationName = shopifyLoc.name,
                            ShopifyActive       = shopifyLoc.active,
                            DateCreated         = DateTime.UtcNow,
                            LastUpdated         = DateTime.UtcNow,
                        };

                        _locationRepository.InsertLocation(newDataLocation);
                    }
                    else
                    {
                        dataLocation.LastUpdated         = DateTime.UtcNow;
                        dataLocation.ShopifyLocationName = shopifyLoc.name;
                        dataLocation.ShopifyActive       = shopifyLoc.active;
                    }
                    _locationRepository.SaveChanges();
                    _shopifyJsonService.Upsert(ShopifyJsonType.Location, shopifyLoc.id, shopifyLoc.SerializeToJson());

                    transaction.Commit();
                }
            }
        }
Example #2
0
        public void UpsertInventory(
            ShopifyVariant variant,
            InventoryItem shopifyItem,
            List <InventoryLevel> shopifyLevels)
        {
            var existingLevels =
                _inventoryRepository.RetrieveInventory(variant.ShopifyInventoryItemId);

            var locations = _inventoryRepository.RetreiveLocations();

            foreach (var shopifyLevel in shopifyLevels)
            {
                var existingLevel =
                    existingLevels.FirstOrDefault(x => x.ShopifyLocationId == shopifyLevel.location_id);

                var location = locations.First(x => x.ShopifyLocationId == shopifyLevel.location_id);

                if (existingLevel == null)
                {
                    var newLevel = new ShopifyInventoryLevel();
                    newLevel.ParentMonsterId          = variant.MonsterId;
                    newLevel.ShopifyInventoryItemId   = shopifyLevel.inventory_item_id;
                    newLevel.ShopifyLocationId        = shopifyLevel.location_id;
                    newLevel.ShopifyAvailableQuantity = shopifyLevel.available ?? 0;
                    newLevel.LocationMonsterId        = location.MonsterId;
                    newLevel.DateCreated = DateTime.UtcNow;
                    newLevel.LastUpdated = DateTime.UtcNow;

                    _inventoryRepository.InsertInventory(newLevel);
                }
                else
                {
                    existingLevel.ShopifyAvailableQuantity = shopifyLevel.available ?? 0;
                    existingLevel.LastUpdated = DateTime.UtcNow;

                    _inventoryRepository.SaveChanges();
                }
            }
        }