public long UpsertProductAndInventory(Product product) { long productMonsterId; using (var transaction = _inventoryRepository.BeginTransaction()) { var existing = _inventoryRepository.RetrieveProduct(product.id); if (existing == null) { var data = new ShopifyProduct(); data.ShopifyProductId = product.id; data.ShopifyTitle = product.title ?? ""; data.ShopifyProductType = product.product_type; data.ShopifyVendor = product.vendor; data.DateCreated = DateTime.UtcNow; data.LastUpdated = DateTime.UtcNow; _executionLogService.Log(LogBuilder.DetectedNewProduct(data)); _inventoryRepository.InsertProduct(data); _inventoryRepository.SaveChanges(); productMonsterId = data.MonsterId; } else { existing.ShopifyTitle = product.title ?? ""; existing.ShopifyProductType = product.product_type; existing.ShopifyVendor = product.vendor; existing.LastUpdated = DateTime.UtcNow; _inventoryRepository.SaveChanges(); productMonsterId = existing.MonsterId; } _shopifyJsonService.Upsert(ShopifyJsonType.Product, product.id, product.SerializeToJson()); transaction.Commit(); } // Write the Product Variants UpsertProduct(productMonsterId, product); // Flags the missing Variants ProcessMissingVariants(productMonsterId, product); // Pull and write the Inventory PullAndUpsertInventory(productMonsterId); return(productMonsterId); }
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(); } } }